Skip to content

Instantly share code, notes, and snippets.

@bat9r
Created July 17, 2019 08:21
Show Gist options
  • Save bat9r/e26cab9010e27929361fb362344db383 to your computer and use it in GitHub Desktop.
Save bat9r/e26cab9010e27929361fb362344db383 to your computer and use it in GitHub Desktop.
Example of injecting parameters
// Import classes
// To work with hudson.model we need have access to System Groovy
// it can be provided by Groovy plugin
import hudson.model.*
import java.util.Map;
// Create directly object of class CascadeChoiceParameter
// which will be parameter
CascadeChoiceParameter CCPCluster = new CascadeChoiceParameter(
"ClusterName",
"ClusterDesc",
new GroovyScript(
new SecureGroovyScript(
"return ['dev', 'stageb']",
true,
[]
),
new SecureGroovyScript(
"return ['Error']",
true,
[]
)
),
"PT_SINGLE_SELECT",
"",
false
)
// Init function for injecting parameters into model
def custom_parameters(Map all_parameters) {
def new_parameters = new ArrayList<StringParameterValue>();
new_parameters.add(CCPCluster)
return new_parameters;
}
// Init variables needed to inject
// namely get current Thread (need to import hudson.model)
def thr = Thread.currentThread()
def build = thr.executable
def all_parameters = build.getBuildVariables();
def new_parameters = custom_parameters(all_parameters);
// Inject the new parameters into the existing list
def modified_parameters = null
def old_parameters = build.getAction(ParametersAction.class)
if (old_parameters != null) {
build.actions.remove(old_parameters)
modified_parameters = old_parameters.createUpdated(new_parameters)
} else {
modified_parameters = new ParametersAction(new_parameters)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment