簡體   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