Get git branch name in Jenkins Jenkinsfile

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?

  • git move locally committed changes to the new branch and push
  • Branch setup when features are interrelated
  • Rebase and changing branch is including all merged commits in pull request
  • I really need help understanding how my git server is set up and what my branches are doing
  • Moving all commits beyond initial commit off master
  • Mercurial — revert back to old version and continue from there
  • Everything I have tried echos out null except

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

    which is always master.

  • Auto commit on file modify with SVN (or git)?
  • Can't add new file to repository in EGit
  • Why isn’t eol=crlf honored in .gitattributes
  • Where can I find jenkins restful api reference?
  • Git submodule most recent common commit
  • git status (nothing to commit, working directory clean), however wth changes commited
  • 4 Solutions collect form web for “Get git branch name in Jenkins Jenkinsfile”

    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:

    • Branch Conditional (see this groovy script)
    • parallel branches pipeline (as seen by the OP)

    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

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

    Use multibranch pipeline.. not pipeline

    In my script..

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


    Git Baby is a git and github fan, let's start git clone.