I\'m trying to get the name of my branch for a jenkins groovy script. I cannot get the current branch name. I try the following:
stage(\'Check out code\')
ch
In the pipeline job I'm using env.GIT_BRANCH
which resolves to origin/{BRANCH}
In the case of a multibranch job, env.GIT_BRANCH
resolves to {BRANCH}
(no origin/
).
I had this same issue, but I resolved it by changing
println "${env.BRANCH_NAME}"
to
println "${BRANCH_NAME}"
Note my plugin is also checking out in detached mode:
git checkout -f e10a170e17fb5f9282f903a7b3cd17bd2e181dee
My workaround, Don't know if work for someone else..
def branchName = getCurrentBranch()
echo 'My branch is' + branchName
def getCurrentBranch () {
return sh (
script: 'git rev-parse --abbrev-ref HEAD',
returnStdout: true
).trim()
}
From a simple pipeline you can use the following script:
\\...
stage('Test') {
steps {
script {
branchName = sh(label: 'getBranchName', returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
println branchName
}
}
}
\\...
In Jenkins there is two pipeline options:
env.BRANCH_NAME
return branch null
env.BRANCH_NAME
return branch master or branch name
This variable only works in a multibranch pipline:
BRANCH_NAME For a multibranch project, this will be set to the name of the branch being built, for example in case you wish to deploy to production from master but not from feature branches.
I was testing in a normal pipline