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. Currently I find no available variable to access these two parameters.
node {
git git+ssh://git.com/myproject.git
echo "$GIT_COMMIT - $BRANCH_NAME"
}
Is such variables available and in case, where would I find them. I don't mind if they are available through some groovy variables or wherever, just that I can access them.
Maybe I lack the debugging skills in Groovy and this is easy to find, but I just can't find it with my limited skills.
If you want to be able to dynamically give a Git branch to use in a Jenkins build then you'll need to do a couple of things. Then, in your Pipeline configuration, under Branches to build, add your parameter name inside the Branch Specifier box, surrounded by ${} . Jenkins will expand your variable when the job runs.
How does Jenkins integrate with Git? Go to Jenkins dashboard, click on “Manage Jenkins.” Now follow these steps- Manage Plugins -> 'Available' tab -> Enter Git in search bar and filter -> Install required plugin. After the installation, all you need to do is click on “Configure System” and go to the 'GitHub' section.
Globally defined variables can be used in all stages but stage defined variables can only be used within that stage. Environment variables can be defined using NAME = VALUE syntax. To access the variable value you can use these three methods $env.NAME , $NAME or ${NAME} There are no differences between these methods.
Depending on the SCM plugin you are using, the checkout
step may return additional information about the revision. This was resolved in JENKINS-26100. It was released in the 2.6 version of the workflow-scm-step
plugin.
For example, using the Git plugin, you can do something like:
final scmVars = checkout(scm)
echo "scmVars: ${scmVars}"
echo "scmVars.GIT_COMMIT: ${scmVars.GIT_COMMIT}"
echo "scmVars.GIT_BRANCH: ${scmVars.GIT_BRANCH}"
This will vary depending on the plugin you use, so the original answer may work better for you.
Original Answer
With the 2.4 release of the Pipeline Nodes and Processes Plugin, you can simply do:
def gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
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:aaaabbbcccdddeeeefffe0123456789012345678, GIT_PREVIOUS_SUCCESSFUL_COMMIT:aaaabbbcccdddeeeefffe0123456789012345678, GIT_URL:http://my.si.te/my-repository.git]
[Pipeline] echo
0123456789abcdefabcdef0123456789abcdef01
...
More details here Jenkins Pipeline SCM Steps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With