Reputation: 539
Let's say I have the following Jenkins pipeline script:
node {
stage('Job A') {
build job: 'Job A', propagate: false
}
stage('Post job') {
build job: 'Cleanup', propagate: false
}
stage('Job B') {
build job: 'Job B', propagate: false
}
stage('Post job') {
build job: 'Cleanup', propagate: false
}
}
Is there a better way to have 1 post job that gets executed after every stage job instead of duplicating it? I also want the next job to wait for the post job to finish.
Upvotes: 2
Views: 1272
Reputation: 12910
I am considering that the cleanup steps required after each job is same otherwise you won't be asking the question.
The easy way is to create your own function which builds a given job and does cleanup.
For waiting unless job is complete, you can set wait
parameter to true
if you are using a scripted pipeline
you can use the following code.
Scripted Pipeline
node {
stage('build_job_a'){
build_and_clean('job_a')
}
stage('build_job_b'){
build_and_clean('job_b')
}
stage('build_job_c'){
build_and_clean('job_c')
}
}
def build_and_clean(job_name){
try{
echo "building a job: ${job_name}"
build job: ${job_name}, propagate: false, wait: true
echo "successfully build a job: ${job_name}"
}catch(e){
echo "error occurred while building job: ${job_name}"
}finally{
echo "Cleaning up"
}
}
Upvotes: 1