24

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?

DavidA
  • 2,053
  • 6
  • 30
  • 54

3 Answers3

56

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

PKo
  • 927
  • 5
  • 14
  • 1
    Thanks @celsiys for pointing that out. Updated the answer accordingly. – PKo Jun 18 '20 at 11:41
  • i added it to my pipeline script but it didn't help (the goal- if the tests failed the build status will be SUCCESS if the tests passed the status will be FAILED and if its mixed pass+failed, the status will be SUCCESS: stage('Tests') { steps { script{ //running the tests sh "${mvnHome}/bin/mvn clean test -e -Dgroups=categories.knownBug" currentBuild.currentResult = "SUCCESS" } } – Noy M Nov 21 '22 at 13:38
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"
      }
     }
 }
0

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

Naren Chejara
  • 1,124
  • 10
  • 7