繁体   English   中英

在Groovy系统脚本中更改作业参数值

[英]change job parameter value in Groovy System Script

我的参数化Freestyle作业有一个字符串参数。 MAIL_PARAM ,默认值为FREESTYLE_ERROR

我可以打印值:

println "MAIL_PARAM=$Mail_Param"  

在Groovy内部执行脚本。 现在我想根据一些条件更改此参数的值。 但我无法改变它。 我试过了:

MAIL_PARAM = 'String'
$MAIL_PARAM ='String'
${MAIL_PARAM} ='String'
def params = new StringParameterValue('MAIL_PARAM', 'String') 

还有一些,但没有一个在起作用。 我必须改变它,因为基于我的groovy脚本的一些结果,我需要在我的参数内部使用不同的字符串。

在groovy脚本之后,我需要将此参数传递给下一个作业。 这很好用。 但我只得到默认值。

如果我理解正确, 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))

编辑:如果您收到错误“当前版本没有任何参数”,请尝试使用“build.addOrReplaceAction”代替“build.replaceAction”。

从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");

如果您需要根据原始值进行修改,这是一个更完整的示例:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM