Get git branch name in Jenkins Pipeline/Jenkinsfile

GitJenkinsBranchJenkins Pipeline

Git Problem Overview


I've create a jenkins pipeline and it is pulling the pipeline script from scm.
I set the branch specifier to 'all', so it builds on any change to any branch.

How do I access the branch name causing this build from the Jenkinsfile?

Everything I have tried echos out null except

sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()

which is always master.

Git Solutions


Solution 1 - Git

Use multibranch pipeline job type, not the plain pipeline job type. The multibranch pipeline jobs do posess the environment variable env.BRANCH_NAME which describes the branch.

In my script..

stage('Build') {
    node {
        echo 'Pulling...' + env.BRANCH_NAME
        checkout scm
        
    }
}

Yields...

Pulling...master

Solution 2 - Git

If you have a jenkinsfile for your pipeline, check if you see at execution time your branch name in your environment variables.

You can print them with:

pipeline {
    agent any

    environment {
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
    }

    stages {
        stage('Build') {
            steps {
                sh 'printenv'
            }
        }
    }
}

However, PR 91 shows that the branch name is only set in certain pipeline configurations:

Solution 3 - Git

A colleague told me to use scm.branches[0].name and it worked. I wrapped it to a function in my Jenkinsfile:

def getGitBranchName() {
    return scm.branches[0].name
}

Solution 4 - Git

For pipeline:

pipeline {
  environment {
     BRANCH_NAME = "${GIT_BRANCH.split("/")[1]}"
  }
}

Solution 5 - Git

For me this worked: (using Jenkins 2.150, using simple Pipeline type - not multibranch, my branch specifier: '**')

echo 'Pulling... ' + env.GIT_BRANCH

Output:

Pulling... origin/myBranch

where myBranch is the name of the feature branch

Solution 6 - Git

This is for simple Pipeline type - not multibranch. Using Jenkins 2.150.1

environment { FULL_PATH_BRANCH = "${sh(script:'git name-rev --name-only HEAD', returnStdout: true)}" GIT_BRANCH = FULL_PATH_BRANCH.substring(FULL_PATH_BRANCH.lastIndexOf('/') + 1, FULL_PATH_BRANCH.length()) }

then use it env.GIT_BRANCH

Solution 7 - Git

Switching to a multibranch pipeline allowed me to access the branch name. A regular pipeline was not advised.

Solution 8 - Git

Just getting the name from scm.branches is not enough if you've used a build parameter as a branch specifier, e.g. ${BRANCH}. You need to expand that string into a real name:

scm.branches.first().getExpandedName(env.getEnvironment())

Note that getEnvironment() must be an explicit getter otherwise env will look up for an environment variable called environment.

Don't forget that you need to approve those methods to make them accessible from the sandbox.

Solution 9 - Git

To get git branch name in Jenkins, If it is a multibranch pipeline then we can easily use the env.GIT_BRANCH. But if it is a normal pipeline then we can use the SCM plugin object to retrieve the branch name.

The below code works for both normal as well as multibranch pipelines.

def branch_nem = scm.branches[0].name
if (branch_nem.contains("*/")) {
    branch_nem = branch_nem.split("\\*/")[1]
    }
echo branch_nem

Solution 10 - Git

FWIW the only thing that worked for me in PR builds was ${CHANGE_BRANCH}

(may not work on master, haven't seen that yet)

Solution 11 - Git

This is what worked for me for non-multi-branch pipeline with either setting SCM Branch Specifier (blank for 'any') to */branch_name or setting it to just branch_name:

BRANCH_NAME = "${GIT_BRANCH.split('/').size() > 1 ? GIT_BRANCH.split('/')[1..-1].join('/') : GIT_BRANCH}"

Solution 12 - Git

I might want to use env.CHANGE_BRANCH to get real branch name instead of jenkins job / PR name.

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
QuestionAlex YurkowskiView Question on Stackoverflow
Solution 1 - GitJamesView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitAttila123View Answer on Stackoverflow
Solution 4 - GitHungView Answer on Stackoverflow
Solution 5 - GitalexView Answer on Stackoverflow
Solution 6 - GitCyrexLtView Answer on Stackoverflow
Solution 7 - GitAlex YurkowskiView Answer on Stackoverflow
Solution 8 - Git0neelView Answer on Stackoverflow
Solution 9 - GitJay ReddyView Answer on Stackoverflow
Solution 10 - GithwjpView Answer on Stackoverflow
Solution 11 - GitGeorge L. YermulnikView Answer on Stackoverflow
Solution 12 - GitAlexey AntonenkoView Answer on Stackoverflow