'Get Git Branch Name that Triggered Jenkins Build

I have a Jenkins file and a Git Hook that triggers the Jenkins build whenever a code is committed in any of the branches.

I wish to print the branch name that triggered the Jenkins Build.

In the checkout stage below I tried to print the branch name but it prints "Null" for println git_params["GIT_BRANCH"] instead of the branch name that committed the code and triggered the Jenkins build.

pipeline {
  agent any

  environment {
    GIT_CRED_ID = 'SVC-JENKINS-ADM'

    GIT_REPO = 'https://bitbucket.hmc.com/scm/mh/docker.git'
  }

  stages {
    stage('checkout') {
      steps {
        script {
          def git_params = checkout([$class: 'GitSCM'])

          println(git_params)

          println 'Getting current Branch'

          println git_params['GIT_BRANCH']
        }
      }
    }

    stage('DEV') {
      agent any

      steps {
        script {
          timeout(time: 25, unit: 'MINUTES') {
            waitUntil {
              try {
                node() {
                  timestamps {
                    task 'Build Environment Setup'
                    //Loading  environment variables
                    checkout([
                      $class: 'GitSCM',
                      branches: [
                        [name: '*/develop']
                      ],
                      [$class: 'WipeWorkspace'],
                    ],
                      userRemoteConfigs: [[credentialsId: env.GIT_CRED_ID, url: env.GIT_REPO]]
                    )
                    dir('temp1') {
                      checkout([
                        $class: 'GitSCM',
                        branches: [
                          [name: '*/master']
                        ],
                        userRemoteConfigs: [[credentialsId: env.GIT_CRED_ID, url: env.GIT_REPO]]
                      ])
                    }
                    load "temp1/${APP_NAME}_${env.ENV}_EnvFile"
                    env.APP_VERSION = sh(script: 'temp1/git_version.sh', returnStdout: true).toString().trim()
                    sh 'echo REL#=$APP_VERSION'
                  }
                }

                emailext attachLog: true, body: "PIPELINE SUCCESS - ${JOB_URL}  \n  Check the full log here", subject: "PIPELINE SUCCESS - ${env.ENV.toUpperCase()} - ${currentBuild.fullDisplayName}", recipientProviders: [developers(), requestor()], to: "$EMAIL_DEPLOYERS"

                return true
              }

              catch (error) {
                echo "Failed in stage : ${env.ENV.toUpperCase()}"

                emailext attachLog: true, body: "PIPELINE FAILED - ${JOB_URL}  \n  Check the full log here", subject: "PIPELINE FAILED - ${env.ENV.toUpperCase()} - ${currentBuild.fullDisplayName}", to: "$EMAIL_DEPLOYERS"

                throw error

                return false

                System.exit(0)
              }
            }
          }
        }
      }
    }
  }
}

Note: in the debug logs the branch name gets printed but I cant print it using println git_params["GIT_BRANCH"] explicitly.

Can you please suggest ?



Solution 1:[1]

Have you tried using GIT_BRANCH just like this

println GIT_BRANCH

GIT_BRANCH is injected as a built in environment variable

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Monish Sen