Child pages
  • Parameterized System Groovy script
Skip to end of metadata
Go to start of metadata

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
  • No labels

3 Comments

  1. Nice script. This kind of stuff should definitely be on the plugin Wiki (getting/setting parameters or environment variables).

    From the code I wonder how it can work safely with multiple nodes.

  2. Not working for non-system groovy script

  3. This script is great for showing the parameters passed to the build.  Is it possible to use Groovy to list all of the options, not the final result, that were available for the param?

    For example, Rather than get the returned value for "AppVersion", I want to have Groovy gather all choices from the ArrayList:

    <hudson.model.ParametersDefinitionProperty>
    <parameterDefinitions>
        <hudson.model.ChoiceParameterDefinition>
            <name>AppVersion</name>
            <description></description>
            <choices class="java.util.Arrays$ArrayList">
                <a class="string-array">
                    <string>Version_1</string>
                    <string>Version_2</string>
                    <string>Version_3</string>
                    <string>ALL</string>
                </a>
            </choices>
        </hudson.model.ChoiceParameterDefinition>
    </parameterDefinitions>