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

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))]
]

Related

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")

Jenkins - Unable to access the parameter sent from one build pipeline in another build pipeline using "build job" command

I have a pipeline project say "A" which is string parameterized, and I am trying to call another build say "B" which is also string parameterized, using the command as follows :
build job: 'B', parameters: [[$class: 'StringParameterValue', name: 'tagg', value: "$env.tag"]]
The target is used to pass the parameter which was taken as input from A and assign use it in B.I tried to receive the parameter echo "$env.tagg" which gave null, echo "$tagg" gave no such parameter found error.
So how do I receive the parameters sent from A in B.
I think you need to let Jenkins know what env.tag is, try something like:
build job: 'B', parameters: [[$class: 'StringParameterValue', name: 'tagg', value: String.ValueOf($env.tag)]]

How to pass choice parameter to call a job inside jenkins pipeline

How can I pass choice parameters for the downstream job when called inside a stage in the jenkins pipeline?
I tried the below solutions but none worked:
stage('build job') {
steps{
script{
build job: 'test',
parameters: [
choice(choices: "option1\noption2\noption3\n", description: '', name: 'choiceParam')
]
}
}
}
fails with java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is using symbol ‘choice’
Tried these as well:
parameters:
[
[$class: 'ChoiceParameterValue', name: 'choiceParam', value: "1\n\2\n3\n"],
]
fails with java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is named ChoiceParameterValue
I actually want to pass the choice parameter as a build parameter like "$choiceParam" for value so that I can just update the jenkins job configuration instead of always updating the values in the pipeline script
Can someone please help me with this
Thanks
When you are building a job via the Build step, you are kicking it off so you need to have "selected" a value.
In this instance you would pass in the desired 'String' choice. Not a list of choices. i.e. "1"
We create our list of params and then pass that in. So: our current job has these input params:
choice(name: 'ENV', choices: product, description: 'Env'),
choice(name: 'ENV_NO', choices: envParams(product), description: 'Env No'),
We pass these downstream by setting them:
List<ParameterValue> newParams = [
new StringParameterValue('ENV', params.ENV),
new StringParameterValue('ENV_NO', params.ENV_NO),
]
build(job: "job", parameters: newParams, propagate: false)

Jenkins: MatrixCombinationsParameterValue from a pipeline

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.

jenkins pipeline def new string parameter

I have a parameterized job with pipeline.
for example: Predefined String Parameter: IP
I'm trying to define a new String in the pipeline in order to use it as a new parameter when I'm calling to another "build job"
I have tried the following method:
import hudson.model.*
node('master'){
if(ipaddr =='192.168.1.1'){
def parameter = new StringParameterValue("subnet", '255.255.255.0') //not working
echo parameter //not working
}
stage ('Stage A'){
build job: 'jobA', parameters:
[
[$class: 'StringParameterValue', name: 'ip', value: ip],
[$class: 'StringParameterValue', name: 'subnet', value: subnet] //not working
]
}
}
this way it's not working and I get the error:
Scripts not permitted to use new hudson.model.StringParameterValue
after changing the line:
def parameter = new StringParameterValue("subnet", '255.255.255.0')
to:
subnet = '255.255.255.0'
I got the error:
groovy.lang.MissingPropertyException: No such property: subnetmask for
class: groovy.lang.Binding.
I can't call to a new job with the predefined parameter ip and the new parameter subnet
without the subnet it's working
any idea of how can I define new String parameter in the pipeline?
jenkins version: 2.19.4
You can have it working if you just avoid instantiating StringParameterValue because as David M. Karr mentionned pipelines sandbox is pretty restrictive. Instead, just use your simple variable when calling your job, like this :
def subnet = ""
if(ipaddr == '192.168.1.1') {
subnet = '255.255.255.0'
echo subnet
}
stage ('Stage A'){
build job: 'jobA', parameters:
[
[$class: 'StringParameterValue', name: 'ip', value: ipaddr],
[$class: 'StringParameterValue', name: 'subnet', value: subnet]
]
}
It's pretty simple, StringParameterValue params expect String to be passed, so as long as you pass string values you should be just fine !
By default, the sandbox the pipeline job executes in is very restricted. You have to override the security restrictions when you find them. Go to "Manage Jenkins" and "In-process Script Approval". You should see in the list the last violation that occurred. Select it for approval and rerun your script.

Resources