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.
You can retry the latest push on the pipeline by going to: CI/CD -> Pipelines -> Run Pipeline -> Select the branch to run.
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.
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)
You can use the error step from the pipeline DSL to fail the current build. error("Build failed because of this and that..")
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With