44

Here is my current Jenkins setup for a project:

  • one job runs all development branches
  • one job runs all pull requests
  • one job runs only the master branch
  • one job makes the automated release only when master passes

This setup allows me to have continuous automated delivery as well as constant feedback during development. The first 3 jobs also run all tests and coverage reports.

The problem is that I could not find a way to exclude the master branch from the "all development branches" job. It unnecessarily builds master twice every time I merge a pull-request.

Does anybody know how to exclude one branch from the job in Jenkins ?

ps: I am using the Git and the Github plugins. My project is stored on Github.

Carneiro
  • 3,479
  • 5
  • 24
  • 36
  • See working solution: http://stackoverflow.com/questions/17155249/jenkins-gerrit-trigger-on-all-branches-except-master – Rylander Jun 08 '16 at 18:20

4 Answers4

65

You can choose "Inverse" strategy for targeting branches to build.

Check out Jenkins job configuration,

  • "Source Code Management" section (choose "Git")
  • Additional Behaviours
  • click "Add" button
  • choose "Strategy for choosing what to build"
  • select "Inverse" strategy in combo box.

(Don't forget to fill in "Branches to build" text field by "master")

See also attachment screenshot image:


enter image description here

kyanny
  • 1,231
  • 13
  • 20
  • 2
    I wonder, did this work for you? Because, apparently, the [inverse strategy is broken in the github plugin](http://stackoverflow.com/questions/16323749/jenkins-github-plugin-inverse-branches). – marc.guenther Mar 22 '15 at 18:56
  • 1
    @marc.guenther Of course it worked for me at that time. – kyanny Mar 23 '15 at 08:18
  • @kyanny it still not works for me. Did you ever get a solution for that? – Fabian Köbel Jan 26 '16 at 15:33
  • @FabianKöbel I'm sorry but I guess it's because of setup of your Jenkins. Since Jenkins has lots of plugins, it sometimes behave strange, or just not working well, due to collision of plugins. I can't know what is your situation, so I'd recommend you to start very vanilla Jenkins in new server of your local machine. Once you got succeeded at fresh environment, align existent Jenkins's assets to succeeded one. Hope you gets by with Jenkins. – kyanny Jan 27 '16 at 07:08
  • Here with Jenkins 1.648 and GitPlugin 2.4.2, inverse does not work. All branches are still built. This is a Grails build freestyle project. – Bertl Mar 10 '16 at 14:34
  • Hi @Bertl I've tested with Jenkins 1.648 & GitPlugin 2.4.2 on my Mac, it still works fine for me - it builds **all branches except Branch to build, you specified**. Note that if your repo does not have any newer branch, Jenkins won't build, with this message. "No new revisions were found; the most-recently built branch will be built again." I'd like to suggest you to check build log of job. – kyanny Mar 11 '16 at 17:40
  • Not an option in pipelines – pingu Jan 16 '18 at 18:15
20

You can use :^(?!.*master).*$ as the branch specifier in Jenkins and all branches except master will be built. See answer: https://stackoverflow.com/a/18709097/444639

Community
  • 1
  • 1
Rylander
  • 19,449
  • 25
  • 93
  • 144
7

@Mike's answer would work for most, but note that :^(?!.*master).*$ would match anything that doesn't contain master, i.e. a branch called feature/add-remaster-functionality won't be built as well. A better way would be :^(?!.*^master$).*$ which matches only master, or even :^(?!.*^(master|origin/master)$).*$ which would match master or origin/master.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 1
    Yeah i like this one, the only thing i had to add to get it to work on jenkins was to escape the "/" character and remove the ":" character, so my regex ended up being: ```^(?!.*^(master|origin\/master)$).*$``` – BLang Dec 01 '20 at 22:26
1

You can use Repository, filter by name(wildcard). Here just provide :^(?!.master).$. skipmaster1

or you can use filter branch using wildcard and exclude master there:skipmaster2 as it successfully scans but later removes from organisation folder scan in case you are using github or bitbucket team project. enter image description here

hi.nitish
  • 2,732
  • 2
  • 14
  • 21