1

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 ?

1 Answer 1

2

Have you tried using GIT_BRANCH just like this

println GIT_BRANCH 

GIT_BRANCH is injected as an environment variable by the scm plugin, but is not a standard built-in variable. You can find more information about Globals here

${YOUR_JENKINS_URL}/pipeline-syntax/globals

Sign up to request clarification or add additional context in comments.

2 Comments

I think this answer is useful, but I think it would benefit from a pointer to relevant documentation. That would help people in similar, but not identical, situations.
Trying to access GIT_BRANCH in a pipeline build results in the following error: groovy.lang.MissingPropertyException: No such property: GIT_BRANCH for class: groovy.lang.Binding. Maybe the variable is defined in a multibranch pipeline only?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.