'How to use ${currentBuild.result} to indicate "SUCCESS" not "null"

My Jenkins declarative pipeline has the following post action:

mail to: '<snip>',
        subject: "Status of pipeline: ${currentBuild.fullDisplayName}",
        body: "${env.BUILD_URL} has result ${currentBuild.result}"

When the build succeeds the content of the email body is:

<job name> has result null

I understand that the value of ${currentBuild.result}" is null when the job succeeds, but this isn't convenient for the user. What is the recommended way of printing "SUCCESS" (or "FAILURE" etc) in the body message?



Solution 1:[1]

Use ${currentBuild.currentResult} instead.

currentResult

typically SUCCESS, UNSTABLE, or FAILURE. Will never be null.

The syntax of the available global variables is available at ${YOUR_JENKINS_URL}/pipeline-syntax/globals. See more info about globals in Jenkins documentation.

Also see https://issues.jenkins.io/browse/WEBSITE-364

Solution 2:[2]

You can add mail step inside post step in pipeline as below :

pipeline {
agent any
stages {
    stage('Example Test') {
        steps {
            echo 'Hello, JDK'
        }
       }
     }
post {
 success {
     echo "${env.BUILD_URL} has result success"
      }
 failure {
     echo "${env.BUILD_URL} has result fail"
      }
     }
 }

Solution 3:[3]

CurrentBuild contains the following property. You can use them according to your need.

_class, actions, artifacts, building, description, displayName, duration, estimatedDuration, executor, fullDisplayName, id, keepLog, number, queueId, result, timestamp, URL, changeSets, culprits, nextBuild, previousBuild, number

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Pankaj Khali
Solution 3 Naren Chejara