How to set current build result in Pipeline?

Issue

  • Whenever I print out the field: currentBuild.result it is null. When does this get set or am I supposed to set it?

Environment

  • CloudBees Jenkins Enterprise
  • Pipeline

Resolution

You are supposed to set it yourself. You can use a try catch block for this.

Example that forces a failure then prints result:

node {
    try {
        // do something that fails
        sh "exit 1"
        currentBuild.result = 'SUCCESS'
    } catch (Exception err) {
        currentBuild.result = 'FAILURE'
    }
    echo "RESULT: ${currentBuild.result}"
}

Output:

Started by user anonymous
[Pipeline] Allocate node : Start
Running on master in /tmp/example/workspace/test
[Pipeline] node {
[Pipeline] sh
[test] Running shell script
+ exit 1
[Pipeline] echo
RESULT: FAILURE
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
Finished: FAILURE

Example that doesn’t fail then prints result:

node {
    try {
        // do something that doesn't fail
        echo "Im not going to fail"
        currentBuild.result = 'SUCCESS'
    } catch (Exception err) {
        currentBuild.result = 'FAILURE'
    }
    echo "RESULT: ${currentBuild.result}"
}

Output:

Started by user anonymous
[Pipeline] Allocate node : Start
Running on master in /tmp/example/workspace/test
[Pipeline] node {
[Pipeline] echo
Im not going to fail
[Pipeline] echo
RESULT: SUCCESS
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
Finished: SUCCESS

See also How to iterate through the done build and rerun only steps that failed in Workflow for how this applies to stages.

Have more questions? Submit a request

3 Comments

  • 0
    Avatar
    Kenneth Lamb

    Where can I find a list of valid values for currentBuild.result? I thought it was just a string, but it appears to only allow certain values.

    For example, this code:
       echo "currentBuild.result1: '${currentBuild.result}'"
       currentBuild.result = 'FAILEDZZZZ'
       echo "currentBuild.result1: '${currentBuild.result}'"

    Will yield this:   

    currentBuild.result1: 'null'
    [Pipeline] echo
    currentBuild.result1: 'FAILURE'

     

  • 1
    Avatar
    Allan Burdajewicz

    Hi Kenneth,

    You are right. currentBuild.result is not a simple String. It is a String representation of hudson.model.Result

    The reason why you see FAILURE when the String does not match a known value is because of this method.

    Regards,

    Edited by Allan Burdajewicz
  • 0
    Avatar
    Kenneth Lamb

    Just what I was looking for. Thank you very much!

Please sign in to leave a comment.