[英]change job parameter value in Groovy System Script
My parameterized Freestyle job got one string parameter. 我的参数化Freestyle作业有一个字符串参数。 MAIL_PARAM
with the default value FREESTYLE_ERROR
. MAIL_PARAM
,默认值为FREESTYLE_ERROR
。
I am able to print the value with: 我可以打印值:
println "MAIL_PARAM=$Mail_Param"
Inside an Groovy execute Script. 在Groovy内部执行脚本。 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. 我必须改变它,因为基于我的groovy脚本的一些结果,我需要在我的参数内部使用不同的字符串。
After the groovy script I need to pass this parameter to the next job. 在groovy脚本之后,我需要将此参数传递给下一个作业。 This works fine. 这很好用。 But I only get the default value. 但我只得到默认值。
If I understand correctly, replaceAction should do the trick (there is also addOrReplaceAction): 如果我理解正确, replaceAction应该做的伎俩(还有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". 编辑:如果您收到错误“当前版本没有任何参数”,请尝试使用“build.addOrReplaceAction”代替“build.replaceAction”。
modify from setBuildParameters: http://jenkins-ci.361315.n4.nabble.com/Modifying-a-builds-parameters-in-a-system-Groovy-script-td4636966.html 从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");
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.