6

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!

Josh
  • 137
  • 1
  • 9

1 Answers1

4

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.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • Thanks for your reply. I looked at the currentBuild variable, but I have been unable to figure out how to use it to overwrite the value of an existing parameter. Do you have any further insight into this? – Josh Oct 31 '15 at 19:05
  • @Josh I suggest to try replacing `build` in the code in your question with `currentBuild`. – Gerold Broser Oct 31 '15 at 19:22
  • Unfortunately, as far as I can tell, currentBuild does not have most of the methods that are available to build, including any access to setting actions. – Josh Oct 31 '15 at 19:27
  • Thank you for taking the time to update your answer. Your solution worked, it successfully updated the parameters, but the next step in the code when I try to start a job using the node() command, a 'java.io.NotSerializableException: hudson.model.ParametersAction' error bubbles up and kills the script. I tried putting the parameter changing code into a @NonCPS function, but this did not fix the issue unfortunately. – Josh Nov 01 '15 at 15:17
  • @Josh I think „_the next step in the code_“ deserves an own question. If you find this answer useful, see [Help Center > Answering](http://stackoverflow.com/help/accepted-answer). – Gerold Broser Nov 01 '15 at 15:31
  • Yes, you're absolutely right. Thank you for your time and reasearch, I owe you a pint for sure! – Josh Nov 01 '15 at 15:49