how do you pass string variables to build job parameters in jenkinsfile - jenkins

for example:
build job: '../downstream_job', parameters: "$variables"
how do you pass the variables ?

This is the format:
build job: 'job_name', parameters: [string(name: 'yourStringParameter', value: "$variable_name")]
You can use the pipeline syntax to generate this code.

I created a simple jenkins job "Echo Params Environment and branch" which echos two parameters it takes: Environment and branch
Then I created this pipeline which calls the job and also has the same two parameters . There are a few extra lines as I was playing with variables but you can see that you can either explicitly define the parameters list (job1) or reference list2 to use as the parameters (job2) - don't use "$list2" - that behaves as a string rather than a list if you do that - so just reference the groovy list variable list2 and it should work fine.
stage('STEP # 1 : Echo parameters') {
String env_name = "${params.Environment}"
String branch_name = "${params.branch}"
println "params are branch $branch_name deploy on $env_name"
def list2 = [string(name: 'Environment', value: "$env_name"),
string(name: 'branch', value: "$branch_name")]
println "List2 is $list2"
// This works with explicit parameter list defined
def job1 = build job:'Echo Params Environment and branch',
parameters: [string(name: 'Environment', value: "$env_name"),
string(name: 'branch', value: "$branch_name")]
def result1 = job1.rawBuild.log
println "result1 = ${result1}"
// This also works - note no $ and no " around list2
def job2 = build job:'Echo Params Environment and branch', parameters: list2
def result2 = job2.rawBuild.log
println "result2 = ${result2}"
}

Related

jenkins pipeline script java.lang.NoSuchMethodError

pipeline {
agent none
stages {
stage ('INSTALL_IMAGE') {
steps {
script {
def command = "env.job_file --testbed-file env.testbed_file --image_file env.image_file --mail_to env.mail_to"
build job: 'PYATS_JOB_EXECUTOR', parameters:
[
string(name: 'branch_name', value: env.branch_name),
string(name: 'pyats_job_args', value: ${command}),
]
}
}
}
}
}
Getting this error: java.lang.NoSuchMethodError: No such DSL method '$' found among steps
job_file testbed_file image_file mail_to branch_name are all string parameters defined in the jenkins pipeline project.
You are receiving this error because of the following line: value: ${command}.
The ${} syntax is used for the groovy String Interpolation, and as the name implies can be used only inside double quoted (single line or multi line) strings.
The following should work:
string(name: 'pyats_job_args', value: "${command}"),
However, because your value is already a parameter you do not need the string interpolation at all, you can just use your command parameter directly:
string(name: 'pyats_job_args', value: command),
This is a much more simple and readable approach.

Passing parameters down a Jenkins pipeline

I cannot get Jenkins to pass a string Parameter down the pipeline.
When I run the pipeline, I input the string value for $ServiceName and the job continues but it doesn't pass this param to the first job in the pipe (NEWSERVICE - Add New). In the jenkins file in the 'build' stage I've tried params.ServiceName, $params.ServiceName, env.ServiceName, $env.ServiceName, $env:ServiceName. No luck.
I need to pass the param to a Powershell build process in the NEWSERVICE job (which currently just echos the Param with $env:ServiceName - but it's always empty) Any help would be vastly appreciated.
pipeline {
agent any
parameters{
string(name: 'ServiceName',
defaultValue: '',
description: '',)
}
stages {
stage('Add new Service'){
steps {
build(job: "NEWSERVICE - Add New", parameters: [string(name: 'ServiceName', value: params.ServiceName)])
}
}
}
}
In Pipeline you need to pass string parameters like this:
parameters: [
[$class: 'StringParameterValue', name: 'ServiceName', value: ServiceName]
],
Refer this to understand different type of variable passing while calling a job from Jenkinsfile.
https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
If you are looking to use a Jenkins env var in a powershell script - which i do all the time! - the env var has to be set up as such in Jenkins first. In other words,
env.myVar in a Jenkins context will be visible as $env:myVar in a PowerShell context. But you need to set it as an env var, local Jenkins variables won't be visible to a child script (unless passed in as a parameter). I have a detailed writeup here: https://stackoverflow.com/a/62538778/556078

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)

Pass multiple values to a string parameter in jenkins

Have a Jenkins job with a string parameter(P1), how do i pass multiple values to this parameter(P1=a,b,c..), I have to further pass the all values of P1 to my jenkins build step(a batch script) how can this be done?
stage ('Invoke_pipeline') {
steps {
build job: 'test-docker-image',
parameters: [
string(name: 'ProjectName', value: "atm"),
string(name: 'Product', value:"renojournalman,reno-keymanagement,reno-management,renoalertman,reno-atm-update,reno-eventmonitor,reno-valmediamon"),
]
}

pass values from a freestyle/pipeline job upstream in jenkins2.0

Here is my problem simplified :
I have a main job (pipeline job) and I have x job (freestyle). In my main job I build x job using the following :
code in main job -
res = build job: 'x', parameters: [string(name: 'JOBNAME', value: string(name: 'JIRACHEF', value: "oldvalue")], quietPeriod: 2
Now in this job x I change the value of JIRACHEF parameter and I print to check if it actually changed.:
os.environ["JIRACHEF"] = "newvalue"
print os.environ["JIRACHEF"]
This works in job x console output. I presume as per the solution presented, this updated value should be now available in the main job so I do the following after in main job just after building x:
res = build job: 'x', parameters: [string(name: 'JOBNAME', value: string(name: 'JIRACHEF', value: "oldvalue")], quietPeriod: 2
print "$res.buildVariables"
which should print "newvalue" but prints "oldvalue" thus making me believe it isn't actually passing the value upstream.
Note - I realize my job x is freestyle, but I have tried the above solution by making x pipeline job as well and still getting the same result - 'oldvalue'
Main job - configuration: pipeline job
node {
x = build job: 'test1', quietPeriod: 2
build job: 'test2', parameters: [
string(name: 'aValue1FromX', value: "$x.buildVariables.value1fromx"),
string(name: 'aValue2FromX', value: "$x.buildVariables.value2fromx")
], quietPeriod: 2
}
test1 - configuration: pipeline job
node {
env.value1fromx = "bull"
env.value2fromx = "bear"
}
test2 - configuration: pipeline job, parametrized, two parameters aValue1FromX and aValue2FromX both strings
node {
echo "$env.aValue1FromX"
echo "$env.aValue2FromX"
}

Resources