Evaluates a Groovy script and injects the results into the environment of every build. This script is powered by the Script Security Plugin; both Approved Script and Groovy Sandbox modes are available. For the new scripts it is recommended to use the Sandbox mode.

Usage

The groovy script must return a Map<String,String> Java object. You can access parameters and other environment variables through variables in the Groovy script. In the scripts you can also use the following variables.

currentJob
Current hudson.model.Job instance.
currentBuild
Current hudson.model.Run instance.
currentListener
Current hudson.model.TaskListener instance, which can be used for logging purposes.
out
Another logging instance as java.io.PrintStream. It is recommended to use currentListener instead of this variable when possible.

All listed variables can be used in both script modes. In the Sandbox mode the access to particular fields and methods may require an additional approval.

Example 1

Injects a new environment variable to all builds, called NEW_PROPERTY

    // build a map
    def map = [: ]
    // add the property
    map['NEW_PROPERTY'] = 'somevalue'

    // show what is being injected
    map.each { key, value ->
        out.println(String.format("Injected %s: %s", key, value))
    }

    return map
        

Example 2

Explodes the contents of the PROPERTIES_TO_INJECT and inject its key=value. For this example to work, create a multiline string parameter in any job with some key=value entries


    if (!binding.hasVariable('PROPERTIES_TO_INJECT')) {
        out.println("Nothing to inject globally. You could add a multiline string parameter and inject pairs of key=value")
        return
    }

    // build a map
    def map = [: ]
    PROPERTIES_TO_INJECT.split('\n').each {
        key = it.split('=')[0]
        value = it.split('=')[1]
        map[key] = value
    }

    // show what is being injected
    map.each { key, value ->
        out.println(String.format("Injected %s: %s", key, value))
    }

    return map