What is the branch name variable for Jenkins multibranch pipelines?

JenkinsJenkins PluginsJenkins Pipeline

Jenkins Problem Overview


I need to know which branch is being built in my Jenkins multibranch pipeline in order for it to run steps correctly.

We are using a gitflow pattern with 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.

In a standard build, I have access to the $GIT_BRANCH variable to know which branch is being built, but that variable isn't set in my 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.

Jenkins Solutions


Solution 1 - Jenkins

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.)

Solution 2 - Jenkins

There is not a dedicated variable for this purpose yet (JENKINS-30252). In the meantime you can take advantage of the fact that the subproject name is taken from the branch name, and use

env.JOB_NAME.replaceFirst('.+/', '')

This has now been resolved, see Krzysztof Krasoń's answer.

Solution 3 - Jenkins

There are 2 branches to consider in a Jenkins multibranch pipeline job:

  1. The Jenkins job branch - env.BRANCH_NAME. This may have the same name as a git branch, but might also be called PR-123 or similar
  2. The git branch - env.GIT_BRANCH. This is the actual branch name in git.

So a job might have BRANCH_NAME=PR-123 and GIT_BRANCH=my-scm-branch-name

Solution 4 - Jenkins

Jenkins documentation has a list of all the env variable for your perusal here

Solution 5 - Jenkins

Another way is using the git command to obtain the branch name on the current jenkins pipeline. For example, you can add the following snippet to print the branch name in your Jenkinsfile.

...
script {
    def BRANCH = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
    echo ${BRANCH}
}
...

Solution 6 - Jenkins

I found this stackoverflow post example useful: https://stackoverflow.com/questions/35554983/git-variables-in-jenkins-workflow-plugin

sh '//...
    git rev-parse --abbrev-ref HEAD > GIT_BRANCH'
    git_branch = readFile('GIT_BRANCH').trim()
    echo git_branch
    //...
   '

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristian RigdonView Question on Stackoverflow
Solution 1 - JenkinsKrzysztof KrasońView Answer on Stackoverflow
Solution 2 - JenkinsJesse GlickView Answer on Stackoverflow
Solution 3 - JenkinsDave BowerView Answer on Stackoverflow
Solution 4 - JenkinsinquisitiveView Answer on Stackoverflow
Solution 5 - JenkinsVolkan KumbasarView Answer on Stackoverflow
Solution 6 - Jenkinsjus4kikzView Answer on Stackoverflow