Jenkins schedule builds one after another - jenkins

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

Related

Jenkins parallel jobs - trigger another job if atleast one success (like successFast) - don't wait for all job's result

I am able to run the parallel Jenkins jobs using parallel syntax and able to get the result back of each child job using wait: true and propagate: false.
example -
pipeline {
stages {
stage('Parallel Jobs'){
steps{
script{
def result = parallel(
"JobAKey":{
build job: "job-A", wait: true, propagate: false, parameters: [string(name: 'param1', value: val1)]
},
"JobBKey":{
build job: "job-B", wait: true, propagate: false, parameters: [string(name: 'param1', value: val1)]
}
)
print(result['JobAKey'].result)
print(result['JobBKey'].result)
if (result['JobAKey'].result == 'SUCCESS' || result['JobBKey'].result == 'SUCCESS') {
build job: "job-C", wait: false, parameters: [string(name: 'param2', value: val2)]
build job: "job-D", wait: false, parameters: [string(name: 'param2', value: val2))]
}
}
}
}
}
}
I want to run job-C and job-D if any of one job job-A or job-B returns SUCCESS.
If job-B returns SUCCESS quickly then I don't want to wait for job-A to complete (SUCCESS / FAILURE) or vice versa. I don't know which job is going to finish quickly and going to return SUCCESS.
With "wait: True", parallel waits for both jobs to finish and then it starts job-C and job-D.
Is there something like successFast (like failFast) ?
Condition is, any one job from list of parallel jobs should return SUCCESS on completion so that we can start next jobs (job-C and job-D).
Thanks in advance!

When doing multiple jobs in a pipeline Jenkinsfile, how do I capture the logs for the given build

As the title states, I want to capture the logs for all the stages in my build, which looks like this:
pipeline {
agent any
stages {
stage('Build First Repo') {
steps {
build job: 'jobOne', parameters: [string(name: 'branch', value: "${params.branch}")], quietPeriod: 1
}
}
stage ('Build Second Repo') {
steps {
build job: 'jobTwo', parameters: [string(name: 'branch', value: "${params.someOtherBranch}")], quietPeriod: 1
}
}
stage ('Deploy') {
steps {
build job: 'jobThree', parameters: [string(name: 'buildEnvironment', value: "${params.environment}")], quietPeriod: 1
}
}
stage ('Remote Build') {
steps {
build job: 'jobFour', parameters: [string(name: 'Hosts', value: "${params.hosts}")], quietPeriod: 1
}
}
}
post {
always {
mail to:me#mydomain.com, subject: "${currentBuild.currentResult} - ${currentBuild.fullDisplayName}", body:"...${currentBuild.rawBuild.getLog(100)}"
}
}
}
Currently, I can only get the pipeline build log (which I am e-mailing in the post/always section), which is helpful but not sufficient; I'd like to get the logs from each of the stages. I thought of maybe capturing them per stage and creating an environment variable or something but I'm not sure how to even access the logs for the build of those jobs. Can someone point me in the right direction on how to capture the logs for those jobs?
You can add a post section after an any stage.
And you can setup sendind emails there.
To send single email from the build you can use stash/unstash command. You can stash log at each section and finally unstash them and send an email.

Jenkins Pipeline Downstream Jobs - continue with next job at specific stage

Background
We have multiple declarative pipeline jobs that can be selected and built in order as downstream jobs from a Master pipeline job. This will wait for each triggered job to complete before starting the next one inline.
Each of the downstream jobs has an input stage where it will wait for user input for artifact promotion before continuing on.
The Problem
Is it possible to have the Master Job continue and start the next job in line once the current job hits the input stage?
i.e. not to wait for the user input before starting the next job inline
From what I see the only options here are wait: true/false AND quietPeriod: to put in a delay. The example below shows these options but neither are suitable for our scenario.
build (job: 'myJob1', parameters: [booleanParam (name: 'startServer', value: false)], quietPeriod: 10, wait: true)
build (job: 'myJob2', parameters: [booleanParam (name: 'startServer', value: false)], quietPeriod: 10, wait: true)
Checkout Parallel Stages, it's exactly what you're looking for.
parallel one: {
stage ('Starting Test')
{
build (job: 'myJob1', parameters: [booleanParam (name: 'startServer', value: false)], quietPeriod: 60, wait: false)
}
}, two: {
stage ('Starting Test2')
{
build (job: 'myJob2', parameters: [booleanParam (name: 'startServer', value: false)], quietPeriod: 60, wait: false)
}
}

How to pass a ${BUIL_NUMBER} of one job to job inside job in Jenkins pipeline

I am new to Jenkins pipeline.
I have a jobA and jobB inside jobA how to send the latest ${BUILD_NUMBER} of jobA to jobB
stage('Run')
{
steps
{
dir ('/path')
{
build job: 'abc'; <How to send the ${BUILD_NUMBER} here>
build job: 'cde';
build job: 'rrr';
}
}
You can achieve it as follows:
build job: 'abc',
parameters: [
string(name: 'build_number', value: String.valueOf(BUILD_NUMBER))
]

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