Jenkins: MatrixCombinationsParameterValue from a pipeline - jenkins

I'm want to start a matrix-build from a pipeline job but I want to build only one Axis.
I tried with this:
build job: "Build_Android_Matrix", propagate: false, wait: true,
parameters: [[$class: 'StringParameterValue', name: 'branch', value: "$branch"],
[$class: 'BooleanParameterValue', name: 'production', value: true],
[$class: 'BooleanParameterValue', name: 'beta', value: false],
[$class: 'MatrixCombinationsParameterValue', name: 'paramFilter', description: null, combinations: ['buildType=Release']]]
I have 2 Axes, flavor and buildType, and paramFilter is the matrix combinations parameter.
The matrix-build starts with all the job parameters but it builds nothing because the matrix combinations selection is empty.
I've also tried with ['buildType==Release'] and ['buildType=="Release"'] but I always get the same result.
I've also tried with:
build job: "Build_Android_Matrix", propagate: false, wait: true, parameters: [
new hudson.plugins.matrix_configuration_parameter.MatrixCombinationsParameterValue
("paramFilter",
null,
['buildType=Release'])
]
but it fails because RejectedAccessException: Scripts not permitted to use new.
I'm almost sure that I'm not providing the combinations in the right way but I don't know what else I can try.
Update
After Christopher Orr answer I tried to set the parameters like this:
[$class: 'MatrixCombinationsParameterValue', name: 'paramFilter', description: null, combinations: ['buildType=Release,flavor=Italy']]]
with this as my Axes:
flavor: Germany Italy Mexico UnitedStates
buildType: Debug Release
And was not working because I forgot that I have also a Slaves Axis and that must be specified as well.
So this is what worked for me:
[$class: 'MatrixCombinationsParameterValue', combinations: ["buildType=Release,flavor=Italy,label=android"], description: '', name: 'paramFilter']

When you use the Matrix Combinations plugin from the web UI, you need to explicitly specify all of the combinations that you want to run. So in Pipeline you need to do the same, for example:
combinations: ['buildType=Release,flavor=beta',
'buildType=Release,flavor=production']
Order of parameters matters.

Related

How to pass Jenkins 'file' parameter value to downstream pipeline job

I am looking for solution to pass a 'file' (.csv) parameter value to downstream job. I have tried with below code but its not working.
build job: "DownstreamJobName",
parameters: [
string(name: 'Releases', value: "1.2.9"),
[$class: "FileParameterValue", name: "test.csv", file: new FileParameterValue.FileItemImpl(new File(env.WORKSPACE/env.filepath))],
string(name: 'UserEmail', value: "testemail")
]
When I got researched got below link that there is an existing defect with file for Jenkins pipeline, dont know whether it got fixed or not. https://issues.jenkins.io/browse/JENKINS-27413
I am able to solve this like below
propertiesFilePath = "${env.WORKSPACE}/test.csv"
parameters: [
[$class: "FileParameterValue", name: "test.csv", file: new FileParameterValue.FileItemImpl(new File(propertiesFilePath))]
]

Missing parameters from downstram jobs after Jenkins upgrade

I upgraded my jenkins recently from 2.164.3 to 2.249.3 version and also upgraded the build pipeline plugin in addition to many other plugins.
and since than my E2E pipe broke on parameters that are passed to downstran job from upstream job.
whay my pipeline does is something like that:
UpstreamJob
stage('Job_1') {
stage('Create') {
steps {
//whatever the job_1 does
}
}
}
stage('Job_2'){
steps {
script {
try {
create_cluster_build_id = Job_1.getId()
} catch (err) {
println "No id was found"
}
down_stream_job_2 = build job: 'Job_2', wait: true, propagate: true, parameters: [
[$class: 'StringParameterValue', name: 'Branch', value: branch_Name],
[$class: 'StringParameterValue', name: 'Docker_Tag', value: docker_tag],
[$class: 'StringParameterValue', name: 'upstream_name', value: env.JOB_NAME],
[$class: 'StringParameterValue', name: 'upstream_build_id', value: env.BUILD_NUMBER],
[$class: 'StringParameterValue', name: 'upstream_build_url', value: env.BUILD_URL],
[$class: 'StringParameterValue', name: 'Job_1_id', value: Job_1_id],
]
}
}
and the error I get inside job2 is
groovy.lang.MissingPropertyException: No such property: Job1_id for class: groovy.lang.Binding
Now...
1)Iv'e found a documentation for similar issue - JENKINS-34871
2)as WA I tried to add the parameter manually in the job2 configuration (in old jenkins configuration it was not part of the job parameters) , it worked but... after several runs the parameter was gone again although I saved it in the job2 configuration.
So my question to the jenkins experts outthere are:
How can it be that I add parameter to a job, save and apply, and after few runs it (the parameter) dissappears?
Found a solution in this thread - Pre-Defined parameters no longer passed to child job
Copy this line to Script console
System.setProperty("hudson.model.ParametersAction.keepUndefinedParameters", "true")

Parameterized pipeline build jenkins always use last build parameters. How to fix this?

I am using Jenkins pipeline with multibranch setup. I set a build parameter for "Build with parameters" using Jenkinsfile. Issue is it always picks up the previous build parameters. Here is the code:
properties([[$class: 'JobRestrictionProperty'],
parameters([
string(defaultValue: 'https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#build-parameters', description: 'File-Location', name: 'File_Location')
]), [$class: 'ThrottleJobProperty', categories: [], limitOneJobWithMatchingParams: false, maxConcurrentPerNode: 0, maxConcurrentTotal: 0, paramsToUseForLimit: '', throttleEnabled: false, throttleOption: 'project'], pipelineTriggers([])])
I am doing a reset of this variable(making it empty) after every run. But for the next build, it always picks the last one(which is empty), not the new one defined in the Jenkins file.
I think this is by design, parametrized pipelines prompts for parameters only the first time and then use those parameters by default

Jenkins parameterized trigger plugin : launch scripts in order

how can I tell Jenkins to run my scripts in order : run script1, if script1 is finished run script2 and same for script3.
In my image, Jenkins doesn't run them in order (script3 can be launched first..).
I am using parameterized trigger plugin because each script needs parameters.
https://i.stack.imgur.com/AcFHb.png
If you are using a Pipeline, you can call the jobs serially from your "main" pipeline and pass parameters to each job.
Taken from this example:
node {
paramAValue = "paramAValue"
paramBValue = "paramBValue"
build job: 'script1', parameters: [[$class: 'StringParameterValue', name: 'ParamA', value: paramAValue], [$class: 'StringParameterValue', name: 'ParamB', value: paramBValue]]
build job: 'script2', parameters: [[$class: 'StringParameterValue', name: 'ParamA', value: paramAValue], [$class: 'StringParameterValue', name: 'ParamB', value: paramBValue]]
build job: 'script3', parameters: [[$class: 'StringParameterValue', name: 'ParamA', value: paramAValue], [$class: 'StringParameterValue', name: 'ParamB', value: paramBValue]]
}
To ensure that one completes before another runs, you could put them in different stages.

Run Jenkins Pipelines jobs in parallel from closure

I have a Jenkins server using the Pipeline plugin. In this, I want to launch several builds in parallel, and wait for the builds to complete before moving on to the next stage of my pipeline.
I am able to do this successfully, if I write out the build jobs explicitly, like so:
parallel 'one': {
build job: 'job1',
parameters: [
[$class: 'StringParameterValue', name: 'CONFIG', value: "foo"]
],
propagate: false,
wait: true
}, 'two': {
build job: 'job2',
parameters: [
[$class: 'StringParameterValue', name: 'CONFIG', value: "foo"]
],
propagate: false,
wait: true
}, 'three': {
build job: 'job3',
parameters: [
[$class: 'StringParameterValue', name: 'CONFIG', value: "foo"]
],
propagate: false,
wait: true
}
However, in reality, there will be a variable number of jobs that need to be built, so explicitly writing these out is not feasible. I have tried to wrap the builds in a closure like so:
def createParallel = { String parallelName ->
parallelName: {
build job: 'jobX',
parameters: [
[$class: 'StringParameterValue', name: 'CONFIG', value: "foo"]
],
propagate: false,
wait: true
}
}
parallel (
createParallel('one'),
createParallel('two'),
createParallel('three')
)
The problem with this method is the builds do not actually kick off in parallel - They build one at a time, waiting for the previous build to complete. What am I doing wrong?
You are not specifying any build parameters, so Jenkins coalesces the queue items, by design.
Set wait: false and build never wait for another build.But if you need to trigger another stage ,you have to get the result
The short answer is that presently this cannot be done in Jenkins, due to bugs JENKINS-33051 and JENKINS-25979.
What I was doing with the above was essentially creating three parallels, each with a single parameter. What needs to be done is place the closures in a list, and then spread the list as parameters, like so:
def list = [ createParallel('one'), createParallel('two'), createParallel('three') ]
parallel (*list)
Unfortunately, this is not implemented in Jenkins right now.

Resources