Global Pre Script

ID: global-pre-script

This plugin makes it possible to execute a groovy script at the start of every job


  • Applies to all jobs/builds run in the server
  • Executes a groovy script when a build starts
  • Injects any number of variables in the build
  • Injects properties based on the content of another property
  • Executes a script when the build starts
  • Very light plugin
  • Failures in the script do not abort the build

Use cases

  • Creating an easy way to pass parameters from one build to another
  • Creating n parameters is not desirable
  • Working with an unknown/big number of parameters
  • Desire to reduce the maintenance of the parameters in the jobs
  • Logging the variables used in the builds
  • Requiring to execute a script that affects all jobs


Add a groovy script in the Global Pre Groovy script box available at Jenkins -> Manage Jenkins -> Configure System If the script fails to be evaluated, you will get error message in the Console Output of the job

Manage Jenkins -> Configure System


Example 1

Injects a new environment variable to all builds, called NEW_PROPERTY Add this script into the Global Pre Groovy script box:

// define a map
def map = [: ]
// add a new 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

Console output:

Example 2:

The next script opens the possibility to define only 1 parameter in every job and pass the information between jobs using only 1 parameter It explodes the contents of the PROPERTIES_TO_INJECT and inject its key=value pairs in the environment

step 1

Add this script into the Global Pre Groovy script box:

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")

// define 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

step 2

Create a freestyle job and add a multi-line string parameter

And an 'Execute shell' build step that will show the environmental variables injected

step 3

Build with Parameters filling the multi-line string parameter with this information

step 4

And finally, verify the results in the Console Output

Groovy script 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.

Current [hudson.model.Job]( instance.
Current [hudson.model.Run]( instance.
Current [hudson.model.TaskListener]( instance, which can be used for logging purposes.
Another logging instance as []( 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.

Jenkins Pipeline Compatibility

There is no need for this plugin when using pipelines and it will not be executed when a pipeline is run


See GitHub Releases for recent releases.

(powered by Script Security Plugin)

(inspired by EnvInject Plugin)

ArchivesGet past versions
Version: 20.02
Requires Jenkins 2.164.1
Installs: 244
This plugin has no labels
Rafa Alonso
Help us improve this page!
To propose a change submit a pull request to the plugin page on GitHub.