Lost_DM
Lost_DM

Reputation: 951

setting parameters in upstream job

I have a multijob structure of the form:

JobA JobB JobC

I want that part of its run JobB will set parameters in JobA so after JobB finishes JobA will be able to pass it to JobC.

I've tried to look at the multijob plugin itself but didn't find any way to do it. Also tried googling various searches on the subject with or without groovy but didn't find anything useful.

Upvotes: 1

Views: 1199

Answers (2)

Lost_DM
Lost_DM

Reputation: 951

I've solved the problem by using a groovy script:

def my_run = Thread.currentThread().executable
def my_parent_run = my_run.getCause(hudson.model.Cause.UpstreamCause).getUpstreamRun()

def preExistingAction = my_parent_run .getAction(hudson.model.ParametersAction.class)
my_parent_run.getActions().remove(preExistingAction)
def params = preExistingAction ?. iterator() ?. toList() ?: []
params << new hudson.model.StringParameterValue('TEST_PARAM', 'FOO')
my_parent_run.addAction(new hudson.model.ParametersAction(params))

It's important that the script will be a "system" using script ("Execute System Groovy Scripts" build step) so that the script with run in the context on the Jenkins JVM and will be able to import the Build object.

So JobB runs this script, and then when it finishes this build step, JobA now has a parameter called "TEST_PARAM" with value "FOO", which it can easily pass to JobC in any of the standard ways.

Note that I'm using fully qualified names, but you can import hudson.model.* and get rid of the all hudson.modle. in the script.

I aim to write and publish a plugin that will do just that, as I think it's a useful feature in many cases.

Upvotes: 4

robjohncox
robjohncox

Reputation: 3665

I don't know a way of doing it with your current job structure, however if you change the jobs so that JobB triggers JobC, then you can achieve what you want using the Parameterized Trigger Plugin. Using this you would configure your jobs as follows:

JobA

In Post-Build Actions, setup to Trigger Parameterized Build of JobB. Under Add Parameters select Predefined Parameters and enter the parameters you want to pass on.

JobB

In Post-Build Actions, setup to Trigger Parameterized Build of JobC. Then there are a number of ways you may want to pass parameters (note that you can combine these different approaches to passing parameters).

  • To pass on the parameters already defined by JobA, under Add Parameters select Current Build Parameters.
  • To have JobB programatically determine parameters to pass to JobC, have JobB output them to a properties file, and then pass these to JobC by selecting Parameters from properties file under Add parameters, and enter the file you stored them in.
  • To manually enter further parameters in the job configuration, under Add Parameters select Predefined Parameters and enter the parameters you want to pass on.

Upvotes: 1

Related Questions