0

I currently have a Jenkins job which is triggered from GitHub.

How do I checkout the branch which triggered the Jenkins job using Jenkins Pipelines?

I've been trying to use something like:

checkout([$class: 'GitSCM', 
branches: [[name: env.BRANCH_NAME]], 
doGenerateSubmoduleConfigurations: 
false, extensions: [[$class: 'RelativeTargetDirectory', 
relativeTargetDir: 'mytargetdirectory']], userRemoteConfigs: [[credentialsId: 'id-123', 
url: 'https://github.com/my/repourl']]])

In the example above, env.BRANCH_NAME is evaluated as null.

However, I'm not sure what the value in branches should be to checkout the branch which triggered the job.

The payload from GitHub contains:

  "ref": "refs/heads/branchname",

Is there a way to extract this information from the payload to get the branchname?

Any help on this would be greatly appreciated!

fuzzi
  • 1,967
  • 9
  • 46
  • 90

1 Answers1

0

Try to use GIT_BRANCH instead of BRANCH_NAME

Renato
  • 1
  • 1
  • 1
  • Thanks @Renato, I tried env.GIT_BRANCH, but this still evaluated as branches=[{name=null}] I also tried without the env, with just GIT_BRANCH and this evaluated with No such property: GIT_BRANCH. Would there be a way for me to see all of the set environment variables to determine what the correct variable would be? – fuzzi Feb 18 '19 at 20:11
  • Here you can see a way to get all environment variables: https://stackoverflow.com/questions/37083285/how-to-list-all-env-properties-within-jenkins-pipeline-job. Give another try using env.BRANCH. – Renato Feb 18 '19 at 20:39
  • Thanks, I tried this, there were no variables related to git branch. – fuzzi Feb 19 '19 at 19:10