I have a series of stages that perform quick checks. I want to perform them all, even if there are failures. For example:
stage('one') { node { sh 'exit 0' } } stage('two') { node { sh 'exit 1' // failure } } stage('three') { node { sh 'exit 0' } }
Stage two
fails, so by default stage three
is not executed.
Ordinarily this would be a job for parallel
, but I want to display them in the stage view. In the mock up below:
two
fails so three
does not run.two
fails and is displayed as such, but three
still runs. The real Jenkins would probably display the entire Build #6 tinged slightly red, which is of course fine.Alternatively you can call error(String message) step to stop the pipeline and set its status to FAILED . For example, if your stage 1 calls error(msg) step like: stage("Stage 1") { steps { script { error "This pipeline stops here!" } } }
Restart from a Stage. You can restart any completed Declarative Pipeline from any top-level stage which ran in that Pipeline. This allows you to rerun a Pipeline from a stage which failed due to transient or environmental considerations, for example. All inputs to the Pipeline will be the same.
Jenkins Pipeline allows you to compose multiple steps in an easy way that can help you model any sort of automation process. Think of a "step" like a single command which performs a single action. When a step succeeds it moves onto the next step. When a step fails to execute correctly the Pipeline will fail.
This is now possible. Below is an example of a declarative pipeline, but catchError
works for scripted pipelines as well.
pipeline { agent any stages { stage('1') { steps { sh 'exit 0' } } stage('2') { steps { catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { sh "exit 1" } } } stage('3') { steps { sh 'exit 0' } } } }
In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:
As you might have guessed, you can freely choose the buildResult
and stageResult
, in case you want it to be unstable or anything else. You can even fail the build and continue the execution of the pipeline.
Just make sure your Jenkins is up to date, since this is a fairly new feature.
EDIT: You need "Pipeline: Basic Steps" 2.16 (May 14, 2019)
I had the same concern. I was able to resolve it doing this.
Second stage will show in red and be marked as failed while the rest of the stages will keep running. You can set a flag and at the end of the stages check the flag and inform the status of the whole build.
node { def build_ok = true stage('one') { sh 'exit 0' } try{ stage('two') { sh 'exit 1' // failure } } catch(e) { build_ok = false echo e.toString() } stage('three') { sh 'exit 0' } .... if(build_ok) { currentBuild.result = "SUCCESS" } else { currentBuild.result = "FAILURE" } }
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