Reputation: 15
I have a job that has N (N representing the number of choice parameters there is) choice parameters. I want to pass each choice parameter to a pipeline. The pipeline needs to build the job with each choice parameter (e.g. Name of parameter is 'Project' with choices {A,B,C}. Pipeline needs to build the job for every choice provided in the project parameter).
I have researched endlessly on google/stackoverflow and haven't come up with a good solution to this.
Upvotes: 1
Views: 1359
Reputation: 1334
There is quite some fiddling that needs to be done in order to get the choices from another job. It could be easier to just hard code them. If you want to access it dynamically you could do something like this:
def choices = Jenkins.instance.getItemByFullName("myJob")
.getProperty(hudson.model.ParametersDefinitionProperty)
.getParameterDefinition("parameterName").getChoices()
choices.each{choice ->
build job: "myJob", parameters: [string(name: "parameterName", value: choice)]
}
Upvotes: 2
Reputation: 1334
So let's say the job with the choices is pipeline B and the one calling it with all the options is A.
In job A you would either hardcode the different options or just have a textfield parameter where you can enter the desired choices. Somewhere in your pipeline (within a script
-block if you use declarative style) you need to do something like this:
//for every selected choice
params.choices.split("\n").each{ choice ->
build job: "myJob", parameters: [string(name: "nameOfChoiceParam", value: choice)]
}
As you can see, internally choice params are nothing more than string parameters.
Edit:
If you want to build this in parallel you need to use scripted pipelines (I think, maybe it also works in declarative ones). The parallel
function expects a Map<String, Closure>
and there are many ways to generate it, but one would be:
def steps = params.choices.split("\n").collectEntries{ choice ->
[(choice): {
build job: "myJob", parameters: [string(name: "nameOfChoiceParam", value: choice)]
}]
}
parallel steps
If you want to do this for all available nodes, you need to generate a list of all the possibilities first (the combiniations
would help) or you could use the good old nested for loop.
When there are 30 choices, you might not gain as much through parallelization and there are a lot of factors (how many executers are there per node, etc.). It would be faster in general to just put the code there directly instead of launching a separate pipeline, especially if we are talking about choices * nodes = 30 * 4 = 120
executions.
Upvotes: 1