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
git.exe checkout -f 33b531b2f1caaf8b64d968e437306f39d2dba1da
That would make the git repo enter a detached HEAD mode, which, by its very nature, has no branch.
From Jenkinsfile:
The checkout step will checkout code from source control;
scm
is a special variable which instructs the checkout step to clone the specific revision which triggered this Pipeline run.
So the ${env.BRANCH_NAME}
is null.
As mentioned in "Jenkins Workflow Checkout Accessing BRANCH_NAME and GIT_COMMIT", you can get the SHA1 you just checked out with the groovy syntax (to be adapted in a Jenkins pipeline DSL):
sh 'git rev-parse HEAD > commit'
def commit = readFile('commit').trim()