Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Created February 13, 2018 23:02
Show Gist options
  • Save t3knoid/80b762ce203d3b8c6e709d84eceb5137 to your computer and use it in GitHub Desktop.
Save t3knoid/80b762ce203d3b8c6e709d84eceb5137 to your computer and use it in GitHub Desktop.
Jenkins Groovy script to inject the current build environment variables into current environment.
// Use the following Groovy script to inject into the current build context
// Install the EnvInject plugin, https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin.
// In your job, Enable the Inject environment variables to the build process and
// Enter in the Evaluated Groovy script field. Make sure to keep the Use Groovy Sandbox
// unchecked. This script returns a hashmap of all environment variables including
// build specific ones into the current environment.
//
// By doing this, Jenkins specific environment variables can be accessed later on
// in a non=system Groovy script with a simple way. For example, to get the value of the
// JENKINS_URL is done this way:
//
// def env = System.getenv()
// println env['JENKINS_URL']
//
def thr = Thread.currentThread()
def config = new HashMap()
def build = thr?.executable
def env = build.parent.builds[0].properties.get("envVars")
config.putAll(env)
return config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment