This plugin makes it possible to execute a groovy script at the start of every job
Features:
- 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
- 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
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:
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
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")
return
}
// 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
Create a freestyle job and add a multi-line string parameter
And an 'Execute shell' build step that will show the environmental variables injected
Build with Parameters filling the multi-line string parameter with this information
And finally, verify the results in the Console Output
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]( http://javadoc.jenkins-ci.org/hudson/model/Job.html) instance.
- currentBuild
- Current [hudson.model.Run]( http://javadoc.jenkins-ci.org/hudson/model/Run.html) instance.
- currentListener
- Current [hudson.model.TaskListener]( http://javadoc.jenkins.io/hudson/model/TaskListener.html) instance, which can be used for logging purposes.
- out
- Another logging instance as [java.io.PrintStream]( https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html). 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)