29

I have a Jenkins job to build the master branch of my GitHub repo. It triggers nicely when I push a commit to master, and I'm very happy about it.

I would also like to have a single Jenkins job that would build any other branch in this repository. I.e. whenever I push a commit to a branch (that is not master), I'd like to have this Jenkins job to build that branch, and only that branch.

Using the GitHub pull-request plugin requires me to create a pull-request, and merges my commit with master. I'd like to do this without having to create a pull-request, and to only build my commit without merging it with master.

Setting up the Git plugin to build all branches means that whenever I push to master, all branches are built. I'd like to only build the one branch that has been modified.

Is this possible to do in Jenkins?

ItayB
  • 10,377
  • 9
  • 50
  • 77
Lóránt Pintér
  • 10,152
  • 14
  • 47
  • 53
  • 1
    By default in Jenkins, you specify the single branch you want to be built. I am unclear what you mean when you say, "whenever I push to master, all branches are built" – Joseph DeCarlo Dec 12 '14 at 09:32
  • have you tried the [Jenkins Github plugin](https://wiki.jenkins-ci.org/display/JENKINS/GitHub+Plugin#GitHubPlugin-TriggerabuildwhenachangeispushedtoGitHub)? – AesSedai101 Dec 12 '14 at 11:18
  • @JosephDeCarlo: Added some clarification to the question: I'd like to have a single job to build the branches (just as I have a single job to build `master`). @AesSedai101: I'm not sure what you mean by using the GitHub plugin. I'm already using it to trigger on push to repo. – Lóránt Pintér Dec 14 '14 at 11:38
  • I have not used jenkins yet but had some success for similar requirement with Travis. If you are interested in knowing about that I will share, In the meantime I will look into jenkins.... – Vishwanath Mar 28 '15 at 16:30
  • @LórántPintér based on the up-votes so far, would you mind marking my answer below as the correct one? – Mig82 Apr 26 '19 at 08:16

6 Answers6

15

I was having the same issue so I set up a dummy project to experiment and found the solution. And found that yes, you can build only the branch that's been pushed into.

The short answer is to use a "Branch Specifier". For instance origin/feature**.

If you're using the GitFlow and all your feature branches are named with a feature/ prefix, then this is the solution.

And if you're curious here's how I figured it out.

From the repository's settings I set the service “Github plugin” with the Jenkins hook https://<jenkin server>/github-webhook/. Note that, at least for me what happened was that for some reason after pressing "test", the message of the payload being sent never changed to confirm it was received/acknowledged or anything. Maybe there’s no reply. It's confusing, but anyway...

I created a new Jenkins job and set the branch specifier to blank -which Jenkins then automatically sets to **.

I created a feature branch feature/foo and pushed into it.

  • Build 1 was fired, but on the master branch.
  • Build 2 was also fired on the feature/foo branch.

So it seems with the ** or blank specifier the plugin will fire builds on all branches of a repo whenever there’s a push to any of them.

Then I tried with pattern refs/heads/feature/foo and pushed another change to the feature/foo branch.

  • Build 3 was fired on the feature/foo branch.
  • No other builds were fired.

Sort of ok, but this is too rigid. It would force us to have one build job for each feature branch. I wanted one build job for all feature branches.

Then I tried a wildcard with pattern refs/heads/feature\*\* and pushed changes to feature/foo.

  • No build was fired.

Then I tried branch specifier refs/heads/feature/\*\* and pushed.

  • No build was fired

Then I saw the help of the "Branch Specifier" field reads this under the "Wildcards" section:

The syntax is of the form: REPOSITORYNAME/BRANCH. In addition, BRANCH is recognized as a shorthand of */BRANCH, * is recognized as a wildcard, and ** is recognized as wildcard that includes the separator /. Therefore, origin/branches* would match origin/branches-foo but not origin/branches/foo, while origin/branches** would match both origin/branches-foo and origin/branches/foo.

So I tried origin/feature**

  • Build 4 was fired on origin/feature/foo.

Eureka! Seems you can’t use wildcards with the references (starting with refs/), but you can with the names of the actual branches (starting with origin/).

Then I created branch feature/bar and pushed to it. To verify that only this one would be built and not other branches also starting with feature/.

  • Build 5 was fired on origin/feature/bar.

Looked almost there. Just needed a few more tests.

Then I pushed another change to origin/master

  • No build fired. Great!

Then I pushed another change to feature/bar, to test that only this branch would be built. Despite the fact that origin/master had also been pushed into.

  • Build 6 was fired on feature/bar.
  • No other builds fired.

Looks good to me.

Mig82
  • 4,856
  • 4
  • 40
  • 63
  • When you say "build," what do you mean? A specific Jenkins job, or the running of one job? So like when you say Build 1 and 2 were fired at first, are you saying the same job was run twice? How could a build for `master` run when you pushed to `feature/foo`? – redOctober13 Apr 25 '19 at 19:23
  • @redOctober13 by *build* I mean a job execution/run. So yes, when I leave the branch specifier blank or set it to `**` and push to any one branch, then the same job runs xN times, once for each branch. This of course is not ideal, hence all the testing that led me to the conclusion that `origin/feature**` is the correct branch specifier to use. – Mig82 Apr 26 '19 at 08:12
  • @Mig82 OK. so how do you build only a single branch (say branch-1235) if you don't have any naming conventions? – Anatolii Stepaniuk Nov 25 '19 at 16:33
4

You can see there plugin of multiple branch configuring with Jenkins. Getting feedback for any branch from Jenkins is possible with that method.

https://wiki.jenkins-ci.org/display/JENKINS/Multi-Branch+Project+Plugin

I hope it may help.

thatway_3
  • 404
  • 1
  • 10
  • 22
1

Found there's an whitelist config listed in Build Trigger section:

click advanced to expand all options

whitelist

There you can specify to build/test which branch the PR will be merged into.

Robert
  • 1,660
  • 22
  • 39
0

Actually, there seems to be an answer to precisely this question on the Jenkins GitHub plugin page :

Trigger a build when a change is pushed to GitHub

LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

As @thatway_3 mentioned, this is possible with a plugin. The Multi-Branch Project Plugin that they linked to is deprecated, but you can use the Pipeline Multibranch Plugin instead.

If you install that plugin and then create a new job of type "Pipeline Multibranch", you can configure that to track a particular repository. It will then (correctly!) build all branches from there.

See also: https://jenkins.io/blog/2015/12/03/pipeline-as-code-with-multibranch-workflows-in-jenkins/

Hope this helps.

Sam
  • 5,997
  • 5
  • 46
  • 66
-4

you can easily specify branch in job configuration:

in section Source Code Management where you have already specified your git repository URL you have also an option to specify branches to build. Simply please a name of your branch instead of master (which is default value).

  • 2
    The idea is to leave this field empty.. and the build should know which branch was pushed – ItayB Mar 25 '15 at 17:16