Maksim Kabakou - Fotolia

Jenkins environment variables list for shell script build jobs

The Jenkins environment variables list outlines the various properties that developers can inject into advanced Jenkins shell scripts and batch programs.

A basic Jenkins installation is pretty boring. Without any Jenkins plug-ins installed, there's really not much you can do with the tool. With the Jenkins Maven plug-in, you can run JUnit tests and package Java applications. With the Jenkins Git plug-in installed, you can pull code from a remote GitHub repository. But without any plug-ins, all you can do is create a Jenkins freestyle project, perhaps parameterize a build and maybe create a batch program or a shell script that references something from the Jenkins environment variables list.

However, having Jenkins inject environment variables into a build job is a good way to learn about some of the underpinnings of the tool. It's definitely not a bad idea to have an intimate knowledge of the various entries in the Jenkins environment variables list, which is the focus of this continuous integration tutorial. The Jenkins environment variables often come in handy when you need to write some advanced shell scripts. Furthermore, if you know how to inject environment variables into the Jenkins build process, it can open up a whole new world of technical opportunities, as it'll give you access to some of the software's internals.

The path to become a Jenkins expert

Part 1 -- Jenkins installation and configuration tutorial 

Part 2 -- Create a simple Jenkins freestyle job

Part 3 -- Injecting Jenkins environment variables into builds (this one)

Part 4 -- Fix any Jenkins plugin download errors

Part 5 -- Learn the importance of Maven Jenkins integration

An easy way to obtain the Jenkins environment variables list from your local installation is to append env-vars.html to the server's URL. For a locally hosted Jenkins server, the URL would be: http://localhost:8080/env-vars.html.

The easiest way to see how these Jenkins environment variables work is to create a freestyle job, echo each entry in the list and see the value Jenkins assigns to each property.

Jenkins variables
The complete Jenkins environment variables list.

Step 1: Create a Jenkins freestyle project

From the Jenkins console, create a new freestyle project with the name Jenkins-Environment-Variables-List. There should be no spaces in the name, as blank characters can cause problems with the shell script. Click OK to enter the build job's configuration page.

Jenkins freestyle
Creating the Jenkins Environment Variables List freestyle job.

For the description of the item, enter: Job to inject Jenkins environment variables into the build process.

Step 2: Add a build step

Scroll down to the Build section of the build job's configuration page, click the Add build step drop-down box and choose the Execute shell option.

Jenkins shell script
Add a Jenkins shell script to inject environment variables into the build process.

Enter the code below to inject Jenkins environment variables into the build script:

echo "BUILD_ID" :: $BUILD_ID
echo "JOB_NAME" :: $JOB_NAME
echo "JOB_URL" :: $JOB_URL

Jenkins inject environment variables

Note that, on a windows machine, the same echo commands can be used, but the Jenkins environment variable should be bookended with percentage signs, not preceded with a dollar sign as in the shell script.

Batch commands
The syntax for batch commands is to use two percentage signs rather than a leading dollar sign as with shell scripts.

Step 3: Run the build job

With the Jenkins environment variables list shell script entered and the build job saved, simply run the job, and examine the output.

C:\tutorial\.jenkins\workspace\ jenkins inject environment variables>
"BUILD_ID" :: 2
"JOB_NAME" :: Jenkins Environment Variables List
"JOB_BASE_NAME" :: Jenkins Environment Variables List
"BUILD_TAG" :: jenkins Environment Variables List 2
"NODE_NAME" :: master
"NODE_LABELS" :: master
"WORKSPACE" :: C:\tutorial\.jenkins\workspace\Jenkins Environment Variables List
"JENKINS_HOME" :: C:\tutorial\.jenkins
"BUILD_URL" :: %
"JOB_URL" :: %
Finished: SUCCESS

This console output was captured the second time the Jenkins build job was run, so the BUILD_ID, BUILD_DISPLAY_NAME and BUILD_TAG variables include the number two in them. Also, notice that the URL-related tags are all blank. That is because this job has not been configured as a remotely accessible job.

Without any Jenkins plug-ins installed, there are only a few ways to demonstrate the tool's capabilities. Printing out the Jenkins environment variables list is one such way. An awareness that these variables exist, along with an ability to use them, can play an important role when the time comes to inject these listed environment variables into advanced Jenkins shell scripts.

Dig Deeper on DevOps-driven, cloud-native app development

App Architecture
Software Quality
Cloud Computing