I\'d like to access git variables such as GIT_COMMIT
and GIT_BRANCH
when I have checked out a repository from git further down in the build stream.
Depending on the information you need, there is a very straightforward solution: get the "checkout scm" operation return: it provides GIT_BRANCH, GIT_COMMIT, GIT_PREVIOUS_COMMIT, GIT_PREVIOUS_SUCCESSFUL_COMMIT and GIT_URL.
node {
stage ("Checkout") {
scmInfo = checkout scm
/*...*/
echo "scm : ${scmInfo}"
echo "${scmInfo.GIT_COMMIT}"
}
}
This will output:
...
[Pipeline] echo
scm : [GIT_BRANCH:my-branch, GIT_COMMIT:0123456789abcdefabcdef0123456789abcdef01, GIT_PREVIOUS_COMMIT:aaaabbbcccffffdeeeefffe0123456789012345678, GIT_PREVIOUS_SUCCESSFUL_COMMIT:aaaabbbcccffffdeeeefffe0123456789012345678, GIT_URL:http://my.si.te/my-repository.git]
[Pipeline] echo
0123456789abcdefabcdef0123456789abcdef01
...
More details here Jenkins Pipeline SCM Steps