Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to separate GitHub webhooks in three categories: master, pull-request and anything-else?

Tags:

github

jenkins

What am I trying to accomplish is the following Jenkins (http://jenkins-ci.org) job configuration:

  1. Have a set of <project>-master jobs there are triggered by

    • A push to the master branch of that particular project.
    • Manually, by clicking on "Build Now"
    • By a script using the REST API.

    I have accomplished this by specifying appropriate refspec, added the GitHub webhook, etc. It was pretty much straightforward.

  2. Have a set of <project>-pr jobs there are triggered by

    • A GitHub PR creation.
    • A comment to the PR which triggers the GitHub Pull Request Builder.
    • A push to the branch which was used for particular PR.

    I have made Jenkins do the first two. But I found no way to do the #3 item from this list because GitHub plugins can't easily find whether the push is to a PR branch or not. Any ideas how this can be done?

  3. Have a set of <project>-branch jobs that are triggered by ANY push to ANY branch. Problem is that I want to exclude pushes to master and to branches that are used for PRs. I've looked on the Internet for possible solution for days and came up with nothing, so any hint will be greatly appreciated.

like image 805
DejanLekic Avatar asked Feb 24 '16 11:02

DejanLekic


People also ask

What is the correct URL for configuring a Webhook in GitHub pick one or more?

Click the Settings tab. In the navigation pane, click Hooks. Click Add Webhook. In the Payload URL field, paste the webhook URL that you copied in Registering and configuring the GitHub repository.

How do GitHub Webhooks work?

Webhooks allow you to build or set up integrations, such as GitHub Apps or OAuth Apps, which subscribe to certain events on GitHub.com. When one of those events is triggered, we'll send a HTTP POST payload to the webhook's configured URL.

What is secret in GitHub Webhook?

Secret. Setting a webhook secret allows you to ensure that POST requests sent to the payload URL are from GitHub. When you set a secret, you'll receive the X-Hub-Signature and X-Hub-Signature-256 headers in the webhook POST request.


1 Answers

  1. For a job to only be triggered by changes to the master branch, you don't need to mess with the github webook. You can just use the git plugins branch specify to designate that this job should only run for the master branch.
  2. Setting up a job to use the Github PUll Request Builder plugin as recommend, it will only trigger for a PR, for the 3 conditions you listed.

  3. the more difficult one.. As far as I know, there is no easy way for jenkins to know whether a branch has a pull request or not, as pull requests are specific to github, and jenkins branch detection is just using git.

However, from my experience, for this third option, I've setup a <project>-feature job, and configured it to match any branch prefixed with a f/. This way, if a developer wanted tests to automatically execute against their branch, but did not want to open a pull request against it, they could create there branch like f/add_a_thing, and it will automatically trigger tests on pushes. For this to work, I would set the branch specifier to f/* in the job configuration.

Alternatively, the git plugin allows for a regular expression parameter to the branch specifier. You can use a regular expression to specifically ignore the master branch. However the only way to ignore pull requested branches, is by having your developers to use a naming pattern, such as pr/add_a_thing, to identify that this branch will have a pull request with it.

like image 96
Jeff Avatar answered Sep 27 '22 20:09

Jeff