My parameterized Freestyle job got one string parameter. MAIL_PARAM
with the default value FREESTYLE_ERROR
.
I am able to print the value with:
println "MAIL_PARAM=$Mail_Param"
Inside an Groovy execute Script. Now I want to change the value of this parameter based on some conditions. But I am not able to change it. I tried:
MAIL_PARAM = 'String'
$MAIL_PARAM ='String'
${MAIL_PARAM} ='String'
def params = new StringParameterValue('MAIL_PARAM', 'String')
and some more, but none of them are working. I have to change it because based on some results my groovy script has, I need different Strings inside of my parameter.
After the groovy script I need to pass this parameter to the next job. This works fine. But I only get the default value.
Here's a more complete example if you need to modify based in the original value:
import hudson.model.ParametersAction
import hudson.model.ParameterValue
import hudson.model.StringParameterValue
def transformJobParameter(Closure transform) {
build.getActions(ParametersAction).each { paramAction ->
List<ParameterValue> overrides = []
paramAction.each { param ->
// Transformation takes a ParameterValue object but returns only its new value object, if any.
def newValue = transform(param)
if (newValue != null && newValue != param.value) {
// Create whatever the original object type was, but with a new value.
def newParam = param.class.newInstance([param.name, newValue, param.description] as Object[])
overrides << newParam
println("INFO - Transformed ${param.name} parameter from '${param.value}' to '$newValue'.")
}
}
if (!overrides.empty) {
def mergedParamAction = paramAction.merge(new ParametersAction(overrides))
build.replaceAction(mergedParamAction)
}
}
}
transformJobParameter { param ->
if (param instanceof StringParameterValue) {
def value = param.value.trim()
if (param.name == 'MAIL_PARAM') {
'String'
} else {
value
}
}
}
If I understand correctly, replaceAction should do the trick (there is also addOrReplaceAction):
import hudson.model.ParametersAction
import hudson.model.ParameterValue
import hudson.model.StringParameterValue
def newMailParameter = new StringParameterValue('MAIL_PARAM', '...')
build.replaceAction(new ParametersAction(newMailParameter))
Edit : if you get error "current build does not have any parameter" then please try "build.addOrReplaceAction" in place of "build.replaceAction".
modify from setBuildParameters: http://jenkins-ci.361315.n4.nabble.com/Modifying-a-builds-parameters-in-a-system-Groovy-script-td4636966.html
def addOrReplaceParamValue = { String name,String value ->
def build = currentBuild.getRawBuild();
def npl = new ArrayList<StringParameterValue>()
def pv = new hudson.model.StringParameterValue(name,value);
npl.add(pv);
def newPa = null
def oldPa = build.getAction(hudson.model.ParametersAction.class)
if (oldPa != null) {
build.actions.remove(oldPa)
newPa = oldPa.createUpdated(npl)
} else {
newPa = new hudson.model.ParametersAction(npl)
}
build.actions.add(newPa);
};
addOrReplaceParamValue("P1","p1");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With