I am using Branch Specifier option of Jenkins Git plugin (v2.0) to run build on specific branch, e.g. 1.4.
${GIT_BRANCH} in this case conta
Based on this SO answer: How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?
I used this, and finally was able to get it to work (the below example assumes a branch variable of $Branch):
stage('Checkout') {
GIT_BRANCH_LOCAL = sh (
script: "echo $Branch | sed -e 's|origin/||g'",
returnStdout: true
).trim()
echo "Git branch: ${GIT_BRANCH_LOCAL}"
git branch: "${GIT_BRANCH_LOCAL}", credentialsId: '...', url: 'git@github.com:......git'
}