Anyone automating their Jenkins pipelines through batch jobs or shell scripts knows the value of referencing entries on the Jenkins environment variables list. But not everyone realizes that once you start using the Jenkins Git integration plugin, a vast array of of new Jenkins Git environment variables become available to your builds. Okay, calling it a vast array may be overstating things. To be precise, there are eleven new Jenkins Git environment variables that become available to your builds when the Jenkins Git plugin is installed.
Jenkins Git plugin environment variables list
Here are the eleven Jenkins Git environment variables:
- GIT_COMMIT – a reference to the current Git commit’s secure hash algorithm (SHA)
- GIT_COMMITTER_NAME or GIT_AUTHOR_NAME – the name used when new Git commits are issued
- GIT_COMMITTER_EMAIL or GIT_AUTHOR_EMAIL – the email address used when new Git commits are issued
- GIT_URL – the base name of the remote GIT repository
- GIT_URL_N – if you are working with more than one remote Git repository (i.e n number of Git repositories) , this will list them all numerically
- GIT_BRANCH – the name of the current Git branch the Jenkins Git plugin is operating upon
- GIT_LOCAL_BRANCH – the name of the local Git branch when the “checkout to specific local branch” Jenkins Git plugin option is selected
- GIT_PREVIOUS_COMMIT – the id of the previous commit on the current branch
- GIT_PREVIOUS_SUCCESSFUL_COMMIT – this variable will output the hash of the commit of the last successful build
Testing Jenkins Git plugin variables
Before throwing these environment variables around in a Jenkins Pipeline or a Maven build, I always like to just print them out in a simple Jenkins freestyle project and see what types of values get returned. (If you’re unfamiliar with creating freestyle projects, here’s a quick Jenkins tutorial to get you started.) Simply echoing the value of each environment variable will prove out exactly what each Jenkins Git environment variable does. The following is the batch full batch command I provided as part of the Jenkins freestyle project:
@echo off echo GIT_COMMIT %GIT_COMMIT% echo GIT_BRANCH %GIT_BRANCH% echo GIT_LOCAL_BRANCH %GIT_LOCAL_BRANCH% echo GIT_PREVIOUS_COMMIT %GIT_PREVIOUS_COMMIT% echo GIT_PREVIOUS_SUCCESSFUL_COMMIT %GIT_PREVIOUS_SUCCESSFUL_COMMIT% echo GIT_URL %GIT_URL% echo GIT_URL_N - %GIT_URL_N% echo GIT_AUTHOR_NAME %GIT_AUTHOR_NAME% echo GIT_COMMITTER_EMAIL %GIT_COMMITTER_EMAIL%
Jenkins Git plugin configuration
The following shows the Jenkins Git plugin configuration for the freestyle project, along with the content of the batch script. Note the Jenkins Git option to add a custom user name and email address was selected. The values used here will become part of the output when the build is run.
Jenkins Git environment variables output
Here is the console output that was generated from the running Jenkins build job:
GIT_COMMIT b9f02da6bf795bdb74a81cdc96ae8186583e75bb GIT_BRANCH origin/master GIT_LOCAL_BRANCH GIT_PREVIOUS_COMMIT b9f02da6bf795bdb74a81cdc96ae8186583e75bb GIT_PREVIOUS_SUCCESSFUL_COMMIT b9f02da6bf795bdb74a81cdc96ae8186583e75bb GIT_URL https://github.com/cameronmcnz/rock-paper-scissors.git GIT_URL_N - GIT_AUTHOR_NAME Git Plugin User GIT_COMMITTER_EMAIL firstname.lastname@example.org
As you can see, there were no major surprises when it came to viewing the output of the Jenkins build job, which is exactly what you want when you’re utilizing Jenkins Git environment variables in your build jobs.
Learn Git fast
New to Git and distributed version control? Here are some Git examples and tutorials designed to help you master the popular source code versioning tool.
- The five basic Git commands beginners need to master
- Undo a commit and manipulate commit history with this git reset –hard example
- Learn to git revert a commit with the bash shell
- Use the git cherry-pick command across Git branches
- Change the Git editor to Notepad++
- Get familiar with the Git configuration files are stored
- What happens when you git reset –hard and push to GitHub?