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"
}
Related
I need to run 5 Jenkins build starting from midnight. For each build execution time may be different.
Currently, I'm scheduling the builds periodically in the pipeline.
Schedule: 0 0,2,4,6,8 * 10 *
each build takes up to 1:15 hrs some may take less than an hour if some failures occur. I need to run builds after the previous build is completed. How can I achieve this?
Below works everytime and is easy too.
pipeline {
agent any
triggers {
cron 'H 0 * * *'
}
stages {
stage('Test') {
steps {
script{
container('tools') {
build job: 'path/to/job1', parameters: [string(name: 'tag', value: '123')]
build job: 'path/to/job2', parameters: [string(name: 'tag', value: '123')]
build job: 'path/to/job3', parameters: [string(name: 'tag', value: '123')]
build job: 'path/to/job4', parameters: [string(name: 'tag', value: '123')]
build job: 'path/to/job5', parameters: [string(name: 'tag', value: '123')]
failFast: true
}
}
}
}
}
}
Cron is set to trigger the job everyday by midnight. Once the above job runs, it starts running all the other jobs sequentially. Use the above snippet for running 5 different jobs sequentially.
If you want to run the same job 5 times, then replace those 5 lines of build jobs above to below.
for (int i = 0; i < 5; ++i) {
build job: 'path/to/the/job', parameters: [string(name: 'tag', value: '123')]
}
The job seeder creates a pipeline job and sets environment variable using job dsl as shown below, this pipeline job triggers another job(say job2) which in turn triggers another job(say job3). I want the environment variable set in seed job to be accessed in the triggered jobs.
pipelineJob("job1"){
description("job1..")
concurrentBuild(false)
environmentVariables(
globalEnv + [TEMP_ENV1 : 'true',
TEMP_ENV2 : 'true'
]
)
definition {
cps {
script(
"""
job1script()
"""
)
}
}
}
I want to access TEMP_ENV1 and TEMP_ENV2 in job3, but both are null in this job. i have a check in script which job3 executes it and it fails, e.g.
if (env.TEMP_ENV1) { }
You should invoke the job using the build step (https://jenkins.io/doc/pipeline/steps/pipeline-build-step/) and pass these along as parameters:
build(
job: "myjob2",
parameters: [
string(name: 'TEMP_ENV1', defaultValue: TEMP_ENV1, description: 'Temp env var 1')
],
propagate: false
)
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}"
}
Lets assume a scenario where Job A calls Job B:
...
...
...
crID = build (job: "Open Change Request", wait: true, parameters: [
string(name: "assignedTo", value: "${BUILD_USER_EMAIL}"),
string(name: "crType", value: "Upgrade worker nodes"),
string(name: "environment", value: "${region}")]).result
The above code is flawed, as result will return FAILURE, SUCCESS, etc...
What I require is to actually retrieve the value that Job B generates.
Is this at all possible, to retrieve the response of the job that ran as part of a build step?
Possibilities:
Read log from the other job?
Global properties?
I ended up doing so by reading the build log.
In job B print the value to log:
echo "Change Request ID:${crID}"
In job A process the log text to get the printed value:
openCrRawData = build (job: "Open Change Request", wait: true, parameters: [
string(name: "assignedTo", value: "${jobInitiator}"),
string(name: "crType", value: "Upgrade worker nodes"),
string(name: "environmentsForCR", value: "${region}")])
crIDRaw = sh (script: "echo \"${openCrRawData.rawBuild.log}\" | grep \"Change Request ID:\"", returnStdout: true).trim().split(":")
crID = crIDRaw[1]
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"),
]
}