Jenkins : Parameterized System Groovy script

This script will demonstrate how to get parameters in a system groovy script.


If you are using the Groovy plugin and want to leverage parameters for your system script, here are some tips.

import hudson.model.*

// get current thread / Executor
def thr = Thread.currentThread()
// get current build
def build = thr?.executable


// get parameters
def parameters = build?.actions.find{ it instanceof ParametersAction }?.parameters
parameters.each {
   println "parameter ${it.name}:"
   println it.dump()
   println "-" * 80
}


// ... or if you want the parameter by name ...
def hardcoded_param = "FOOBAR"
def resolver = build.buildVariableResolver
def hardcoded_param_value = resolver.resolve(hardcoded_param)


println "param ${hardcoded_param} value : ${hardcoded_param_value}"

this produces the output:

Started by user dnozay
parameter RUNparam:
<hudson.model.RunParameterValue@62042521 runId=broker_debug#1 name=RUNparam description=>
--------------------------------------------------------------------------------
parameter CHOICEparam:
<hudson.model.StringParameterValue@c4a75e7b value=foo name=CHOICEparam description=>
--------------------------------------------------------------------------------
parameter FOOBAR:
<hudson.model.StringParameterValue@dde4de51 value=<3 lolcats name=FOOBAR description=>
--------------------------------------------------------------------------------
param FOOBAR value : <3 lolcats
Finished: SUCCESS