XebiaLabs | Continuous Delivery and Agile DevOps Tools

XebiaLabs Blog

Subscribe to XebiaLabs Blog: eMailAlertsEmail Alerts
Get XebiaLabs Blog via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: Intel XML, XML Magazine

Blog Feed Post

Capture Jenkins Job Environment Variables in XL Release

One of our most important goals here at XebiaLabs is to make sure our customers get the most out of their investments, not just with respect to our products, but also to other tools in the DevOps tool chain. Today, I’d like to share with you how you can effectively use your Maven POM metadata (GAV Definition) and other environment variables in XL Release by propagating them through Jenkins.

Currently Jenkins task in XL Release only returns back BUILD NUMBER and BUILD STATUS as output properties that you can capture in variables. But what if you want to capture all the environment variables that are available in a build job including Maven POM Variables? Here’s how to do it.

Let’s first look at a Jenkins task inside XL Release:

5https://blog.xebialabs.com/wp-content/uploads/2016/02/5.png 686w" sizes="(max-width: 686px) 100vw, 686px" />

Let’s now look at a detailed view of task:

6https://blog.xebialabs.com/wp-content/uploads/2016/02/6-768x595.png 768w, https://blog.xebialabs.com/wp-content/uploads/2016/02/6-1024x793.png 1024w" sizes="(max-width: 1024px) 100vw, 1024px" />

Changes for Capturing Environment Variables

Let’s move on to the changes you’ll need to make in Jenkins to capture environment variables:

  • In your Jenkins instance, go to Manage Plugins and install this plugin EnvInject Plugin.
  • Select a candidate job that you want to trigger using XL Release Jenkins task. In this case, we’re using a petclinic job that builds the sample spring-petclinic GitHub project,  which is available online using Java and Maven.
  • After creating a new job, go to Configure Job, and set up basic things like build commands, GitHub repo, and so on.
  • In the General section, check the option Prepare an environment for the run, then check the options for Keep Jenkins Environment Variables and Keep Jenkins Build Variables. These allow you to capture all the standard environment variables.
  • To capture the POM variables that in turn will allow you to capture the GAV Definition, do the following:
    • In Pre-Steps, add 3 steps in the following order (considering you’re running on Linux)
      • Execute Shell echo POM_VERSION=$POM_VERSION > mvn.props
      • Inject Environment Variables ( provide mvn.props as the value in Properties File Path property)
      • Execute Shell rm mvn.props
    • When you’re done with the above, save the job.

How to effectively use Maven POM metadata by propagating them through Jenkinshttps://blog.xebialabs.com/wp-content/uploads/2016/02/7-768x478.png 768w, https://blog.xebialabs.com/wp-content/uploads/2016/02/7-1024x637.png 1024w, https://blog.xebialabs.com/wp-content/uploads/2016/02/7.png 2230w" sizes="(max-width: 1024px) 100vw, 1024px" />

With the EnvInject Plugin, the XL Release task will be modified to access the environment variables through the expose REST API by this plugin.

Sample :  http://localhost:8080/jenkins/job/petclinic/14/injectedEnvVars/api/json

Changes in XL Release

Next we’ll make changes to XL Release. Here we’ll perform a type and script modification to add extra functionality to Jenkins task. To prepare for this, we first need to get the python script that’s currently executed as part of Jenkins task. Take the following steps:

  • Go to the box where XL Release is installed.
  • Go to XL Release Home folder and then inside lib folder.
  • Look for the server-****.jar file.
  • Now using either an unzip utility or the jar command, unzip to extract the following file: jenkins/Build.py sample jar command :  jar -xvf server-5.0.0.jar jenkins/Build.py
  • You’ll see a folder named Jenkins containing a Build.py, which is created in the lib folder
  • Now make the changes outlined below.
    • Edit the XLR_HOME/ext/synthetic.xml and type the following:
        <type-modification type="jenkins.Build">
                 <property name="properties" category="output" kind="map_string_string" required="false" description="All environment properties"/>
    • Move the jenkins folder under XLR_HOME/ext/ such that it looks like XLR_HOME/ext/jenkins/Build.py
    • Edit that file and look out for this section:
        while True:
            # now we can track our builds
            response = request.get(jobContext + str(buildNumber) + '/api/json', contentType = 'application/json')
            buildStatus = JsonPathResult(response.response, 'result').get()
            duration = JsonPathResult(response.response, 'duration').get()
            if buildStatus and duration != 0:
    • Underneath this section, add the following lines (be careful of indentation as these lines are not inside the while loop but at the same level):
        envResponse = request.get(jobContext + '/' + str(buildNumber) + '/injectedEnvVars/api/json', contentType = 'application/json')
        properties = JsonPathResult(envResponse.response,'envMap').get()
    • Save Build.py. You have now overridden the Jenkins functionality to add a new field that’s of type string key-value map and will be populated by the environment variables from Jenkins.
    • Restart XL Release.

Testing Changes in the XL Release GUI

You’ll now notice that your previous Jenkins task shows another extra field in output properties:

How to effectively use Maven POM metadata by propagating them through Jenkinshttps://blog.xebialabs.com/wp-content/uploads/2016/02/8-768x515.png 768w, https://blog.xebialabs.com/wp-content/uploads/2016/02/8-1024x686.png 1024w" sizes="(max-width: 1024px) 100vw, 1024px" />

Once the Jenkins job completes, you can run a release and look at the task:

How to effectively use Maven POM metadata by propagating them through Jenkins.https://blog.xebialabs.com/wp-content/uploads/2016/02/9-768x468.png 768w, https://blog.xebialabs.com/wp-content/uploads/2016/02/9-1024x624.png 1024w" sizes="(max-width: 1024px) 100vw, 1024px" />

Using the Captured Values

To create a variable as a map full of values, you can specify the variable in the properties field. Let’s call that variable props.

  • To use the value in a script task or precondition, use it as follows: print releaseVariables['props']['POM_VERSION'] where props is the map variable and POM_VERSION is the key whose value need to be captured.
  • To use the value in a manual task, email body, or anywhere else, you need to create a variable per value that you want to capture and then use a script task to set them with the right values. For detailed instructions see: https://docs.xebialabs.com/xl-release/how-to/get-a-value-from-a-map-variable.html


Continue learning how Jenkins and XL Release work together by visiting our Jenkins Solutions Page.

The post Capture Jenkins Job Environment Variables in XL Release appeared first on XebiaLabs.

Read the original blog entry...

More Stories By XebiaLabs Blog

XebiaLabs is the technology leader for automation software for DevOps and Continuous Delivery. It focuses on helping companies accelerate the delivery of new software in the most efficient manner. Its products are simple to use, quick to implement, and provide robust enterprise technology.