Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you edit Build Parameters in Jenkins Workflow?

I know that you can access build parameters directly in the Jenkins Workflow. I have a parameter called BRANCH_REVISION which I need to update so that a call to the xml api will show the new value instead of the original value. This is something I was doing in a non-workflow script using the following groovy snippet:

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)

However, it appears that this does not work in Workflow since the build object is not accessible. Thanks in advance for any help!

like image 246
Josh Avatar asked Oct 31 '15 15:10

Josh


People also ask

How do I change the build parameters in Jenkins?

To execute the Jenkins job from Jenkins web interface first go to respective project workspace and then click on “Build with Parameters” option from left panel. After that, you will be asked to choose/set parameter value (In my project I have set BROWSER parameter.)

How do I get Jenkins build parameters?

If you are trying to get all parameters passed to Jenkins job you can use the global variable params in your groovy pipeline to fetch it.


1 Answers

See <Workflow job configuration> → WorkflowSnippet Generator → Global variables → Variable: 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.

Use currentBuild.build() instead of build in the code in your question according to org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper which is the type of currentBuild.

like image 160
Gerold Broser Avatar answered Nov 15 '22 05:11

Gerold Broser