Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Post Build script when a Jenkins job is aborted

Tags:

Is there a possible way / plugin that can run a post build script when a Jenkins job is aborted. I do see that the post build plugin provides an action to execute a set of scripts, but these can be run only on 2 options either a successful job or a failed job.

like image 896
Jose Avatar asked Jul 30 '14 21:07

Jose


People also ask

How do I execute one Jenkins job after the other?

You can follow the below steps to trigger a Jenkins pipeline in another Jenkins pipeline. Select a job that triggers a remote one and then go to Job Configuration > Build section > Add Build Step > Trigger builds on remote/local projects option.


2 Answers

This question is positively answered here.

The Post Build Task plugin is run even if a job is aborted.

Use it to search the log text for "Build was aborted" and you can specify a shell script to run.

Works like a charm. :-)

like image 94
tim Avatar answered Sep 20 '22 15:09

tim


For a declarative Jenkins pipeline, you can achieve it as follows:

pipeline {     agent any      options {         timeout(time: 2, unit: 'MINUTES')   // abort on exceeding the timeout     }      stages {         stage('Echo 1') {            steps {               echo 'Hello World'           }          }         stage('Sleep'){             steps {                 sh 'sleep 180'             }         }         stage('Wake up'){             steps {                 echo "Woken up"             }         }     }     // this post part is executed if job is aborted     post {         aborted {             script{               echo "Damn it. I was aborted!"             }         }     } } 
like image 38
t_sologub Avatar answered Sep 21 '22 15:09

t_sologub