93

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.

2
  • Could you please update the correct answer now that it has been resolved for people who come here via search?
    – Dan Mandle
    Commented Dec 12, 2016 at 21:02
  • 1
    NB: In declarative pipelines, you probably want to use when to control which stages are executed on which branches/tags.
    – Raphael
    Commented Feb 25, 2019 at 15:40

6 Answers 6

142

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

5
  • 1
    Couldn't get this to work at all in a script {} block at the top of the steps {} block. By printing the env using echo env.getEnvironment() I found that ${env.BRANCH} is available and that works! Commented May 31, 2017 at 12:13
  • 4
    Interesting, none of these work for me. Dumped everything under env - there was neither BRANCH nor BRANCH_NAME. When I tried to refer to BRANCH_NAME I get "groovy.lang.MissingPropertyException: No such property: BRANCH_NAME for class: groovy.lang.Binding" Pipeline Groovy: 2.32, Jenkins 2.32, BitBucket 4.6.2, BitBucket Server Webhook for Jenkins 3.0.1 Commented Jan 4, 2018 at 10:24
  • 2
    Bizarrely, I find that this works on certain repositories and not others. I have two multi-branch pipelines set up with the same line in their jenkinsfiles: if(env.BRANCH_NAME == "master") and it works as expected in one, in the other produces RejectedAccessException: unclassified field java.lang.String BRANCH_NAME. Very strange.
    – Jansky
    Commented Jan 12, 2018 at 15:29
  • 2
    @Vroomfundel if you were executing a Pipeline job that is not a multibranch Pipeline job, that is (apparently) the expected condition, due to JENKINS-47226 - Branch name not available in single Pipeline build.
    – Mark Waite
    Commented Nov 8, 2018 at 14:36
  • In my opinion its CHANGE_BRANCH at least for PullRequest Commented Mar 4 at 17:36
11

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.

8
  • 1
    Thanks, that got me pointed in the right direction. Here is a little more information that helped me to get what I needed. issues.jenkins-ci.org/browse/JENKINS-30252 is an issue logged against the workflow plugin for this exact thing. @jesse-glick answered in more detail there. To summarize: ${env.JOB_NAME.replaceFirst('.+/', '')} will give you the branch name. Commented Sep 28, 2015 at 19:55
  • Thanks, incorporated into answer. Commented Sep 29, 2015 at 12:57
  • Also, making it non-greedy might be helpful for those who put slashes in their branch names. env.JOB_NAME.replaceFirst('.+?/', '') Commented Sep 29, 2015 at 20:04
  • 8
    You can now use ${env.BRANCH_NAME} (as JENKINS-30252 has been resolved)
    – scrutari
    Commented Jul 27, 2016 at 11:06
  • 2
    Or simply $BRANCH_NAME in sufficiently new versions of workflow-cps. Commented Jan 10, 2017 at 2:26
11

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

2
3

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

1
  • Outdated please update Commented Apr 5 at 6:54
1

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}
}
...
2
  • Welcome to StackOverflow! Could you maybe edit your answer to clarify, where this method (which comes with additional overhead) has its advantage over the built-in, ready-to-use variable as described in the accepted answer? Commented Dec 27, 2019 at 22:40
  • Note that this only works after the repository has already been cloned and checked out successfully. It will not work before that.
    – Kissaki
    Commented Sep 14, 2020 at 16:42
0

I found this stackoverflow post example useful: Git Variables in Jenkins Workflow plugin

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.