My current task at work is to implement the Gitflow branching model for all our projects. So, each Git repository has a branch for develop, release, and production, as well as a few feature branches. Of course, all these branches should be built by our CI server.
Since most branches have a very short lifetime, I want the work to be done by a *single* CI job that is unaware of the branch it is currently working on. And this is harder than it may seem at first…

First try: plain Git plugin

The Jenkins Git plugin supports wildcards as branch names. So, just enter "**" as branch specifier and activate the "Poll SCM" trigger. Now Jenkins triggers a build if it detects changes on any branch.

Issues:

  • It is not possible to manually start the job for a given branch (if you start the job, an arbitrary branch will be processed).
  • There is only one overall build result, not a result per branch. So, a green job just means that the last processed branch is ok – but we don’t know if *all* branches are ok.
  • Jenkins checks out a commit rather than the HEAD of a branch - this can be a problem with some post-build actions (at least the ${GIT_BRANCH} var tells us the name of the branch)

Improvement: parameterized build

To start a job manually, you could introduce a build parameter called "Branch" with default value "**" and set the branch specifier to "${Branch}". Now you may enter the branch name when starting a build.
Unfortonately htis doesn't work: The "Poll SCM" trigger does no variable expansion, so builds won't be triggered (or, even stranger, the build is triggered but no checkout is performed). See e.g. JENKINS-10580  - not an exact match, but the closest I could find.

Improvement: branch overview

An extension of the Git plugin that shows the per-branch build status is available on Github. Since this is based on a older plugin version, you have to checkout the plugin source code, merge in these changes, and build your own version.
You can then access a branch overview using the "GitBuild Data" link on a build page, or with URL "http://[hostname]/job/[jobname]/lastBuild/git/".