How can we get last run status of a Jenkins pipeline and, if it was failed then re-start it?

390 Views Asked by At

I have Scheduled pipeline that runs everyday at 6 AM, I want that pipeline to get executed at any cost.

I want some automated solution to check the pipeline execution status and, it should get restarted only if it got failed because of some reason.

I want automated solution, no manual/UI intervension.

2

There are 2 best solutions below

0
MoonLight On

You can use the Naginator plugin in Jenkins to setup a post build action step that reschedules or restarts your job/pipeline upon status such as unstable or failure.

Jenkins Plugin Naginator

0
Tamy Ak On

At first you need to make curl to that job is successful or not with this sh command:

  final String BuildJobStatus= sh(script:"curl -X GET http://your user name:your credential@the jenkins server address/jenkins/job/jobName/lastBuild/api/xml?depth=0 | grep -Po '(?<=<${result}>).*?(?=</${result}>)'| head -1",returnStdout:true).trim()

then you should check BuildJobStatus If is Success with another curl you can start your favourite job.