0
0
JenkinsHow-ToBeginner · 3 min read

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', or null (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.result inside a script block in declarative pipelines.
  • Setting currentBuild.result to 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 ValueMeaning
SUCCESSBuild completed successfully
FAILUREBuild failed
UNSTABLEBuild completed with warnings or test failures
ABORTEDBuild was manually aborted
nullDefault 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.