Get Environment Variable From Last Jenkins Build

Using Groovy scripting and the Groovy Plugin you can do this:

def job = Hudson.instance.getJob('CoolJob')  
def run = job.getLastBuild()
  
println run.getEnvironment()["SVN_REVISION"]

If you just println run.getEnvironment(), you’ll see the whole list of environment variables in the build. These include any parameters for a parameterized job. To test this, I used the script console. I plan on using this to tag a successful milestone or release (ivy “status” that is) build in Jenkins. Now, I’m sure there is a way to get the SCM download url used for the Subversion checkout, that post will hopefully come soon.

Download PDF
This entry was posted in Jenkins and tagged . Bookmark the permalink.

2 Responses to Get Environment Variable From Last Jenkins Build

  1. Richard Cross says:

    This is great, but there is a limitation: it doesn’t include environment variables that were injected using the EnvInject Plugin.

  2. K says:

    Thanks for this tip Aris!

Leave a Reply

Your email address will not be published. Required fields are marked *