JE

Jenkins Error Handling

Pipeline failure strategies, retry logic, and notification patterns

Details

Language / Topic
jenkinsJenkins
Category
Error Handling

Rules

balanced
- Use post blocks: post { always { } success { } failure { } cleanup { } } — always runs regardless of outcome
- Use retry(count) around flaky steps: retry(3) { sh 'npm install' } — combine with sleep for backoff
- Use catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') to continue pipeline on non-critical failures
- Send failure notifications in post { failure { } } with build URL, stage name, and logs
- Combine timeout and retry: timeout(time: 5, unit: 'MINUTES') { retry(3) { sh 'docker push' } }
- Use post blocks at pipeline and stage levels for lifecycle management
- retry(count) for transient failures: retry(3) { sh 'npm install' } — add sleep for backoff
- catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') for non-critical stages
- timeout to prevent hung steps: timeout(time: 15, unit: 'MINUTES') { sh './run-tests.sh' }
- Combine timeout and retry for resilient but bounded operations
- try-catch in script blocks for fine-grained error handling
- Structured failure notifications: BUILD_URL, JOB_NAME, currentBuild.currentResult
- warnError('message') as concise alternative to catchError for simple warnings
- Archive diagnostics on failure: post { failure { archiveArtifacts '**/test-results/**' } }
- waitUntil for polling-based health checks with timeout
- Set currentBuild.result and currentBuild.description in error handlers
- Use options { timestamps() } for diagnosing timing-related failures