Access variables in subsequent steps in Jenkins Job

Problem:

How to carry over environment variables to subsequent build steps?

It is possible by making use of a Jenkins plugin(which is not installed by default

Solution:

Go to http://your-jenkins-address/pluginManager/available and install the plugin.

Determine which variable you want to be used in the subsequent build steps (In my case I’m trying to use a variable called ipAddress which I want to pass it on to subsequent steps)

Create a properties file in your jenkins box

  • Login as a jenkins user
  • sudo -su jenkins
  • Create an empty properties file and save it. I have used /var/lib/
  • jenkins@ip-:/var/lib/jenkins$ vim your_file_name.properties

Lets go back to Jenkins Console -> Job

In the Execute Shell, write this step

  • echo "ipAddress=your_value" > ~/your_file_name.properties

You should be able to add a Inject environment variables step only when plugin installation is success.

Pass on the properties file path, this will pull all the variables from the properties file and allow it to be used in subsequent steps:

Try to read the value of $ipAddress from a subsequent execute shell step

Run the jenkins job.

  • You will be able to see this step printing the ipAddress value.
  • When you cross check in the jenkins box, go into:
  • jenkins@ip-:/var/lib/jenkins$ cat your_file_name.properties
  • ipAddress=http://127.0.0.1:8080