Maksim Kabakou - Fotolia
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 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
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.
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.
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.
Enter the code below to inject Jenkins environment variables into the build script:
echo "BUILD_NUMBER" :: $BUILD_NUMBER
echo "BUILD_ID" :: $BUILD_ID
echo "BUILD_DISPLAY_NAME" :: $BUILD_DISPLAY_NAME
echo "JOB_NAME" :: $JOB_NAME
echo "JOB_BASE_NAME" :: $JOB_BASE_NAME
echo "BUILD_TAG" :: $BUILD_TAG
echo "EXECUTOR_NUMBER" :: $EXECUTOR_NUMBER
echo "NODE_NAME" :: $NODE_NAME
echo "NODE_LABELS" :: $NODE_LABELS
echo "WORKSPACE" :: $WORKSPACE
echo "JENKINS_HOME" :: $JENKINS_HOME
echo "JENKINS_URL" :: $JENKINS_URL
echo "BUILD_URL" ::$BUILD_URL
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.
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
"BUILD_DISPLAY_NAME" :: #2
"JOB_NAME" :: Jenkins Environment Variables List
"JOB_BASE_NAME" :: Jenkins Environment Variables List
"BUILD_TAG" :: jenkins Environment Variables List 2
"EXECUTOR_NUMBER" :: 0
"NODE_NAME" :: master
"NODE_LABELS" :: master
"WORKSPACE" :: C:\tutorial\.jenkins\workspace\Jenkins Environment Variables List
"JENKINS_HOME" :: C:\tutorial\.jenkins
"BUILD_URL" :: %
"JOB_URL" :: %
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.