How to Set Build Result in Jenkins Pipeline
In Jenkins Pipeline, you set the build result by assigning a value to
currentBuild.result. For example, use currentBuild.result = 'SUCCESS' or 'FAILURE' to mark the build status explicitly.Syntax
Use the currentBuild.result property to set the build status in a Jenkins Pipeline script. The value must be a string representing the build state.
currentBuild.result: The property to set the build status.- Possible values:
'SUCCESS','FAILURE','UNSTABLE','ABORTED', ornull(default means success).
groovy
currentBuild.result = 'SUCCESS' // Mark build as successful currentBuild.result = 'FAILURE' // Mark build as failed currentBuild.result = 'UNSTABLE' // Mark build as unstable
Example
This example shows a simple Jenkins Pipeline that sets the build result to FAILURE if a command fails, otherwise it sets it to SUCCESS.
groovy
pipeline {
agent any
stages {
stage('Example') {
steps {
script {
try {
sh 'exit 1' // Simulate failure
currentBuild.result = 'SUCCESS'
} catch (err) {
currentBuild.result = 'FAILURE'
echo "Build marked as FAILURE due to error: ${err}"
}
}
}
}
}
}Output
[Pipeline] sh
+ exit 1
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Build marked as FAILURE due to error: script returned exit code 1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // pipeline
Finished: FAILURE
Common Pitfalls
Common mistakes when setting build result include:
- Not using
currentBuild.resultinside ascriptblock in declarative pipelines. - Setting
currentBuild.resultto lowercase strings like'failure'instead of uppercase'FAILURE'. - Expecting the build to stop immediately after setting the result; it only marks the status but does not abort the build.
groovy
pipeline {
agent any
stages {
stage('Wrong') {
steps {
// This will NOT work because it's outside script block
// currentBuild.result = 'FAILURE'
echo 'This will not set build result correctly'
}
}
stage('Right') {
steps {
script {
currentBuild.result = 'FAILURE' // Correct usage
}
}
}
}
}Quick Reference
| Build Result Value | Meaning |
|---|---|
| SUCCESS | Build completed successfully |
| FAILURE | Build failed |
| UNSTABLE | Build completed with warnings or test failures |
| ABORTED | Build was manually aborted |
| null | Default state, treated as SUCCESS |
Key Takeaways
Set build status in Jenkins Pipeline by assigning to currentBuild.result with uppercase strings.
Use a script block in declarative pipelines to set currentBuild.result.
Setting currentBuild.result changes the build status but does not stop the pipeline execution.
Common values are SUCCESS, FAILURE, UNSTABLE, and ABORTED.
Always handle errors with try-catch to set build result explicitly on failures.