38

We have a lot of developers creating feature branches that I would like to build. Nightly we run a code quality tool that needs to run on every branch. I also would not like a static configuration because the number of branches changes every few weeks.

babsher
  • 1,016
  • 2
  • 8
  • 11

4 Answers4

42

In Git configuration there is a field 'Branch Specifier (blank for default): ' if you put there ** it will build all branches from all remotes.

having that you can use an environment variable ${GIT_BRANCH} e.g. to set a title for the build using https://wiki.jenkins-ci.org/display/JENKINS/Build+Name+Setter+Plugin or for other purposes

Tomasz Bartczak
  • 597
  • 5
  • 6
  • This will build all branches when they are committed to. However, I want to build all branches at a specific time. – babsher Mar 12 '13 at 14:14
  • Hmm so you want to build the branch that... there was no change in it? This is not a very typical thing to do, as we should make the same code work the same the next day. You may be succesfull selecting "Wipe out workspace before build". Jenkins will not keep track of previous build, maybe it will try to build all branches, like it does for a fresh job. – Tomasz Bartczak Mar 15 '13 at 14:32
  • 13
    Even if there are no code changes, building the same branch day after day will often catch time related bugs - e.g. when they manifest on the first of every month, or whenever the moon is waning gibbous. – mkirk Apr 20 '13 at 13:46
  • 4
    Not just that, making sure all branches still build against maybe changed external dependencies, that build environment including toolchains and base systems has not rotten, that integration tests including externally evolving data still holds etc.Periodic builds just to be sure things always build when you need them are not a bad idea – kert Oct 02 '15 at 06:01
  • This will build only one branch which you push – KunMing Xie Feb 26 '18 at 02:01
7

I had the same problem to be solved. Specifically, make a zip file of all branches and offer those as artifacts to be used in different test jobs.

In "Branches to build", put "**"

Then, Execute shell:

while read -ra ITEM; do
  for i in "${ITEM[@]}"; do
    git checkout $i
    <do your stuff>
  done
done <<< $(git branch -r | grep -v "HEAD ->" | xargs -L 1 | cut -d'/' -f2)

This reads the list of branches, checkouts each of them separately and allows to do stuff in each of them. The <<< command converts this output:

  origin/HEAD -> origin/master
  origin/branch1
  origin/master
  origin/secondbranch

into checkout usable list:

branch1
master
secondbranch
Squrppi
  • 95
  • 1
  • 7
2

Old question but somewhat more fitting answer. The multi-branch plugin below allows you to create a build item type that fans out sub-projects with branches, syncing config automatically from top level to sub-projects

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

UPDATE 2021 This plugin is marked DEPRECATED

This plugin is deprecated. Please move to the Multibranch Pipeline job type.

For a somewhat more involved approach, Seed plugin gives you a lot of flexibility defining sub-jobs

https://github.com/jenkinsci/seed-plugin/wiki

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
kert
  • 2,161
  • 21
  • 22
  • 1
    I like this plugin when it works. However, it is officially superseded by the [Pipeline Multibranch Plugin](https://wiki.jenkins.io/display/JENKINS/Pipeline+Multibranch+Plugin). Personally, I think Jenkinsfile "pipeline as code" are overhyped and frustrating to work with. – Mike D May 08 '19 at 16:43
2

The ** for branch specifier will run against all branches AND all tags. If you just want branches, use a branch specifier of refs/heads/*

Delta Echo
  • 33
  • 1
  • 5