Jenkins Multibranch pipeline: What is the branch name variable?

Christian Rigdon picture Christian Rigdon · Sep 25, 2015 · Viewed 84.4k times · Source

I'm new to the Jenkins Pipeline plugin, and especially new to the multibranch pipeline plugin. In a standard build I have access to the $GIT_BRANCH variable to know which branch is being built. However that variable isn't set in the multibranch pipeline. I have tried env.GIT_BRANCH too, and I tried to pass $GIT_BRANCH as a parameter to the build. Nothing seems to work. I assumed that since the build knows about the branch being built (I can see the branch name at the top of the console output, that there is something that I can use, I just can't find any reference to it.

Here is an example of why this is important. We are using a gitflow pattern. We have dev, release, and master branches that all are used to create artifacts. The dev branch auto deploys, the other two do not. Also there are feature, bugfix and hotfix branches. These branches should be built, but not produce an artifact. They should just be used to inform the developer if there is a problem with their code. I need to know which branch is being built in order to run the correct steps.

Any help would be appreciated.

Answer

Krzysztof Krasoń picture Krzysztof Krasoń · May 7, 2016

The env.BRANCH_NAME variable contains the branch name.

As of Pipeline Groovy Plugin 2.18, you can also just use BRANCH_NAME (env isn't required but still accepted.)