Saturday, January 15, 2022

Jenkins : Importing Your Custom Environment Variables



You are working on Jenkins and you want setup and work with some of your own custom Environment Variables and you discover that Jenkins cannot pickup your machine environment variables. Today we are going to look at how you can load your own custom variables that you want to use inside your Jenkins Scope.



Start by creating a new Job that just prints out the value from any of your machine environment variables, I have JBOSS_HOME. You will notice that the value from your machine environment variable did not print. So it’s not you it’s just the behaviour of Jenkins. 


echo 'The System ENV variable in my bash profile is : ${JBOSS_HOME}"

Simple Jenkins Print Out Shot

I am guessing that’s why there’s a reason we have a special plugin we can use.


Head over to the plugins section of Jenkins Manage Jenkins > Plugin Manager and search for the plugin named "Environment Intjector"

Environment Injector Plugin Installation


Restart Jenkins when the installation is complete and then let’s configure the variables next. 



Now that the plugin is installed we can start configuring it. In our current case we want to add a random simple Environment Variable and then we will print it out. To do start by heading to the configurations section in Jenkins as follows : Manage Jenkins > Configure System

Manage System Configurations

Once you are on the Configuration page then, look for the heading or section, “Global properties”. You will notice a check box labelled, “Prepare jobs environment”. Check that box where a text box will show at the bottom labelled, “Properties File Path”.


Things to note well here are :

  • One more thing to note very well is that the text box label states clearly that it expects a .properties file path. This means that you must create a .properties file which will be the one that contains your variables.
  • The path you specify must be an absolute path, you cannot use the your OS short cuts like ~/ and ${HOME}/ . so for example I have a path named tools/jenkins/ in my home directory


So modify your .properties file and add a key = value par line with a key and value of your choice, I have my first name in the file as follows : ENV_MY_FIRST_NAME = Thabo which I will be printing in Jenkins shortly.

The next thing is to configure the text box path to where your file is : /Path/to/your/environment/


Jenkins Environment Variables File Configuration

Click the “Apply” and then the “Save” buttons. The last thing to do now it to restart Jenkins so that it becomes aware of the new Environment Variables.



So now we have configured all we wanted to configure. Let’s look into creating or updating a Job that will perform a simple variable printout, that’s all. So remember that my key = value pair is : ENV_MY_FIRST_NAME = Thabo so this means that I can now reference the key of the value I want to use. Check out my Job Shell Script below :

echo 'The System ENV variable in my bash profile is : ${ENV_MY_FIRST_NAME}"

Jenkins Shell Referencing Custom Environment Variable

Click the “Apply” and then the “Save” buttons.


The moment we have been waiting for, you may now run your Job and checkout the console logs. Checkout the image below for my results :

Jenkins Environment Variable Printing Results


This is how you can configure Jenkins to use your own custom environment variables. I hope this is useful to your Jenkins usage. Let me know what you think. Cheers!

No comments:

Post a Comment