This content is part of the Essential Guide: Master Git basics and branch into DVCS

Essential Guide

Browse Sections
News Stay informed about the latest enterprise technology news and product updates.

Jenkins Git environment variables list: A working shell script by example

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.

Using Jenkins Git environment variables in a shell script

Jenkins Git plugin configuration in the freestyle project.

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 [email protected]

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.


Become a Git power user

Want to become a Git power user? Take a look at the following Git articles and tutorials

 

 

 

 

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close