Search code examples
groovyjenkins-workflowjenkins-pipeline

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


Solution

  • 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