88

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.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
Christian Rigdon
  • 883
  • 1
  • 6
  • 6
  • Could you please update the correct answer now that it has been resolved for people who come here via search? – Dan Mandle Dec 12 '16 at 21:02
  • 1
    NB: In declarative pipelines, you probably want to use [`when`](https://jenkins.io/doc/book/pipeline/syntax/#when) to control which stages are executed on which branches/tags. – Raphael Feb 25 '19 at 15:40

6 Answers6

136

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

Aphex
  • 7,390
  • 5
  • 33
  • 54
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • 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! – Morgan Christiansson May 31 '17 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 – Vroomfundel Jan 04 '18 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 Jan 12 '18 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](https://issues.jenkins-ci.org/browse/JENKINS-47226) - Branch name not available in single Pipeline build. – Mark Waite Nov 08 '18 at 14: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.

Community
  • 1
  • 1
Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
  • 1
    Thanks, that got me pointed in the right direction. Here is a little more information that helped me to get what I needed. https://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. – Christian Rigdon Sep 28 '15 at 19:55
  • Thanks, incorporated into answer. – Jesse Glick Sep 29 '15 at 12:57
  • Also, making it non-greedy might be helpful for those who put slashes in their branch names. env.JOB_NAME.replaceFirst('.+?/', '') – Christian Rigdon Sep 29 '15 at 20:04
  • No, because that will break inside folders. How branches with `/` in their names actually get handled is a good question—this is not currently tested, and might not work. Probably the slash needs to be URL-encoded in the job name or something like this. – Jesse Glick Sep 30 '15 at 21:47
  • [JENKINS-30744](https://issues.jenkins-ci.org/browse/JENKINS-30744) for more on that. – Jesse Glick Oct 01 '15 at 19:28
  • 8
    You can now use `${env.BRANCH_NAME}` (as [JENKINS-30252](https://issues.jenkins-ci.org/browse/JENKINS-30252) has been resolved) – scrutari Jul 27 '16 at 11:06
  • 2
    Or simply `$BRANCH_NAME` in sufficiently new versions of `workflow-cps`. – Jesse Glick Jan 10 '17 at 02:26
  • Thank you! This is fixed my URL bar issue. Now i can go to full URL: `PIPELINE_URL = "${env.JENKINS_URL}/job/${PIPELINE_NAME}/job/${env.BRANCH_NAME.replaceFirst('/', '%2F')}"` – Dentrax Jan 28 '20 at 07:47
9

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

Dave Bower
  • 3,487
  • 26
  • 28
  • 4
    I am not sure what's so different in our Jenkins setup but in our case also GIT_BRANCH is PR-123. – Blackhex Nov 21 '20 at 15:20
  • 2
    You can find all the env vars configured for your Jenkins instance's at https://your-jenkins-server.example.com/env-vars.html – Dave Bower Jun 13 '22 at 09:18
4

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

inquisitive
  • 3,738
  • 6
  • 30
  • 56
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}
}
...
  • 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? – StephenKing Dec 27 '19 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 Sep 14 '20 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
    //...
   '
Community
  • 1
  • 1
jus4kikz
  • 27
  • 1
  • 2