Modifying a builds parameters in a system Groovy script

14,146 views
Skip to first unread message

Reuben Gow

unread,
Aug 3, 2012, 11:52:17 AM8/3/12
to jenkins...@googlegroups.com
Hi,

I have a job that takes a number of parameters (version_number, release_number, branch etc). This build can be built in a number of ways, some need to get the values of these parameters from other locations. the different modes are:

1) Manually - Take the passed in parameters
2) SCM Change - take the parameter values from the last successful build of another job
3) Timer - take the parameter values from the last successful build of another job
4) Upstream Job - take the parameters passed to it from the upstream job.

1 is obviously no problem, as is 4 with the use of the Parameterized build plugin.

I have written a Groovy script to retrieve the parameter values for cases 2 and 3 but can't find a way to apply these values to the variables used in the build step (execute shell ). I can create new parameters and access these as variables but I don't really like this method as it makes my shell script a bit ugly. 

Does anyone know how to overwrite parameter values from within the Groovy script.

Thanks

cjo

unread,
Aug 3, 2012, 12:38:32 PM8/3/12
to jenkins...@googlegroups.com
Have a look at the EnvInject Plugin[1], to see if you are able plug your script into it and get the correct results.
look at "Prepare an environment for the run" section in the job config.

The "Evaluated Groovy script" item might override them correctly.

Reuben Gow

unread,
Aug 6, 2012, 6:43:00 AM8/6/12
to jenkins...@googlegroups.com
Hi Chris,

I've tried out the EnvInject plugin but it doesn't seem to be able to overwrite existing variables.

if I put:

return [ SOME_NEW_VAR: "blah" ]

then I can put the following the in the build step

 echo ${SOME_NEW_VAR}

and get the correct value.

However if I put:

return [ version_number: "blah" ]

where version_number is one of the build parameters the echo command returns the original passed in parameter.

Reuben Gow

unread,
Sep 21, 2012, 5:49:49 AM9/21/12
to jenkins...@googlegroups.com
I found a solution to this.

You have to get the 'Actions' of the build using the getActions() call, find the ParametersAction and remove it, then create a new set of parameters and then add them as an Action.

korm...@gmail.com

unread,
Jul 22, 2013, 1:36:18 PM7/22/13
to jenkins...@googlegroups.com
Following Reuben's comments I came up a util function. Posting here so other people can see a working example.

import hudson.model.*

// sets build parameters based on the given map
// only supports StringParameterValue
def setBuildParameters(map) {
    def npl = new ArrayList<StringParameterValue>()
    for (e in map) {
        npl.add(new StringParameterValue(e.key.toString(), e.value.toString()))
    }
    def newPa = null
    def oldPa = build.getAction(ParametersAction.class)
    if (oldPa != null) {
        build.actions.remove(oldPa)
        newPa = oldPa.createUpdated(npl)
    } else {
        newPa = new ParametersAction(npl)
    }
    build.actions.add(newPa)
}

Samuel Almeida

unread,
Apr 27, 2016, 10:22:02 AM4/27/16
to Jenkins Users
There is now  a replaceAction() where you can just pass a new action (Parameter) with the same name and it will replace it :)

http://javadoc.jenkins-ci.org/hudson/model/Actionable.html#replaceAction%28hudson.model.Action%29
Reply all
Reply to author
Forward
0 new messages