How to Continue a Pipeline After a Failure With catchError in Jenkins
catchError runs a block and records a failure as a chosen result instead of immediately aborting the pipeline.
Wrap risky steps in catchError(buildResult: '...', stageResult: '...'). The block can fail without stopping the run, so cleanup, reporting, or notification stages still execute.
Steps
- Wrap the failing-prone steps in a
catchError(...)block. - Set
buildResult(e.g. UNSTABLE) andstageResult(e.g. FAILURE). - Put report or notify stages after it so they still run.
Jenkinsfile
Jenkinsfile
pipeline {
agent any
stages {
stage('Test') {
steps {
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
sh 'npm test'
}
}
}
stage('Publish report') {
steps {
junit 'reports/*.xml'
}
}
}
}Gotchas
- For non-fatal cleanup,
warnError('message')is a shorter form that marks the build unstable. - A swallowed failure can hide real regressions; scope catchError narrowly.
Related guides
How to Mark a Build Unstable With warnError in JenkinsDowngrade a non-critical Jenkins step failure to UNSTABLE with the warnError step or catchError, separating y…
How to Set a Timeout on a Single Stage in JenkinsBound just one Jenkins stage with the timeout step so a hung deploy or flaky test is aborted without capping…