Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement a retry option for failed stages in Jenkins pipelines?

I have a Jenkinsfile with multiple stages and one of them is in fact another job (the deploy one) which can fail in some cases.

I know that I can made prompts using Jenkinsfile but I don't really know how to implement a retry mechanism for this job.

I want to be able to click on the failed stage and choose to retry it. jenkins-pipelines-with-stages

like image 204
sorin Avatar asked Apr 05 '16 09:04

sorin


People also ask

How do you retry pipeline?

You can retry the latest push on the pipeline by going to: CI/CD -> Pipelines -> Run Pipeline -> Select the branch to run.

How do I retry Jenkins job?

To retry a Jenkins job, you can use the Naginator plugin. Simply install the plugin, and then check the Post-Build action "Retry build after failure" on your project's configuration page. If the build fails, it will be rescheduled to run again after the time you specified.

How do you skip failed stage in Jenkins pipeline?

To ignore a failed step in declarative pipeline you basically have two options: Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)

How do you make Jenkins pipeline fail?

You can use the error step from the pipeline DSL to fail the current build. error("Build failed because of this and that..")


1 Answers

You should be able to combine retry + input to do that Something like that

stage('deploy-test') {    try {      build 'yourJob'    } catch(error) {      echo "First build failed, let's retry if accepted"      retry(2) {         input "Retry the job ?"         build 'yourJob'      }    } } 

you could also use timeout for the input if you want it to finish if nobody validates. There is also waitUntil that might be useful but i haven't used it yet

Edit : WaitUntil seems definitely the best, you should play with it a bit but something like that is cleaner :

stage('deploy-test') {    waitUntil {      try {        build 'yourJob'      } catch(error) {         input "Retry the job ?"         false      }    } } 

By the way, there is doc all of the steps here https://jenkins.io/doc/pipeline/steps

like image 163
fchaillou Avatar answered Sep 18 '22 04:09

fchaillou