2

I have a Jenkins Pipeline job which is triggered of a github webhook, so on a commit to Github the build gets triggered. However, I only want the build to be triggered when a commit is made to the master branch. Currently any commit triggers a build even if the commit is made on a feature branch.

Is there any way to achieve this without resorting to a multibranch job?

I can't see any way to filter on branch (for the trigger) in the Jenkins config, the jenkinsfile or in the Github repo's webhook.

The only branch specific information I can find is which branch the code will build from but a commit to any other branch will still 'trigger' a build.

Can anyone help?

Craig
  • 341
  • 2
  • 9
  • 18
  • See the answer to [GitHub WebHooks triggered globally instead of per branch](https://stackoverflow.com/questions/46140233/github-webhooks-triggered-globally-instead-of-per-branch) for the GitHub webhook side of your question. Maybe it will give you some other ideas. – dmulter Jun 29 '18 at 15:47
  • 1
    What's your problem with the multi branch job type? – StephenKing Jun 29 '18 at 20:01
  • @StephenKing I have tried creating a multi branch job as a resolution. However in that case I struggled to get the webhook to trigger a build at all. So I ended up with a Jenkins file where I could include stages that were conditional on the branch I’d pushed to (which was ideal) however I had to manually trigger the build. I have the multi branch config set to use a webhook. And have the webhook created in my repo. But pushing to any branch fails to trigger the build. ( I will caveat all this with I’m pretty new to all this so may well have made a rookie mistake somewhere!) – Craig Jun 30 '18 at 07:23
  • 1
    I suggest that you try to fix the webhook then instead of what you're trying to solve with this question. – StephenKing Jun 30 '18 at 08:20
  • So I'm trying out the multibranch build but still seem to have an issue with the build not triggering. In the Branch Sources I have set up a source of 'GIT' as advised by out tooling team. However from this link it seems this should be GitHub https://support.cloudbees.com/hc/en-us/articles/115003019232-GitHub-Webhook-Pipeline-Multibranch Does anyone know if I can use either? Or must it be github to have auto trigger on a push to github? – Craig Jul 02 '18 at 15:48

2 Answers2

1

So the more I looked into this, the more i realised that Stephen was right and I should be using the multibranch pipeline approach. Indeed, in the newer Blue Ocean UI, all pipelines are multibranch by default, further suggesting this is the way to go.

As to the second issue around triggering the build, this did seem to be related to the branch source that has been set up. This was originally using the git branch source. Once this was changed to a github branch source and the github server configured correctly in jenkins then the trigger worked as expected.

Craig
  • 341
  • 2
  • 9
  • 18
  • 3
    How did you do that? There seems to be a frustrating lack of examples on pipelines in general. – stricq May 13 '19 at 01:14
0

You can change the mode of which branch will 'trigger' a build by followed steps

Location: Jenkins --> new item --> pipeline --> build triggers --> advanced --> Allowed branches --> Filter branches by ***

zintown
  • 1
  • 1