如何在 Jenkins Workflow 中编辑构建参数?

How can you edit Build Parameters in Jenkins Workflow?

我知道您可以直接在 Jenkins Workflow 中访问构建参数。我有一个名为 BRANCH_REVISION 的参数,我需要对其进行更新,以便对 xml api 的调用将显示新值而不是原始值。这是我在非工作流脚本中使用以下 groovy 片段所做的事情:

def currentParamActions = build.getAction(ParametersAction.class)
def currentParams = currentParamActions.getParameters()

currentParams.each() {
    if ( it.name.equals("BRANCH_REVISION") ) {
        newParams.add( new StringParameterValue("BRANCH_REVISION", newRevision ) )
    }
    else {
        newParams.add( it )
    }
}

build.actions.remove(currentParamActions)
new_param_actions = currentParamActions.createUpdated(newParams)
build.actions.add(new_param_actions)

但是,这似乎在 Workflow 中不起作用,因为构建对象不可访问。在此先感谢您的帮助!

参见<工作流作业配置>→工作流代码段生成器→全局变量 → 变量:currentBuild:

The currentBuild variable may be used to refer to the currently running build. It is an object similar to that documented for the return value of the build step.

根据 org.jenkinsci.plugins.workflow.support.steps.build.RunWrappercurrentBuild.

类型,在您问题的代码中使用 currentBuild.build() 而不是 build