Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 594
DavidA Avatar asked Oct 02 '18 08:10

DavidA


People also ask

How do I get build status in Jenkins?

Navigate to the Jenkins dashboard and select Build History. This timeline displays your build history. This table displays a list of your past builds, time since build and the status of each build.

What is currentBuild in Jenkins?

currentBuild is a global variable that may be used to refer to the currently running build.

How do I stop pipeline Jenkins?

Pipeline jobs can be stopped by sending an HTTP POST request to URL endpoints of a build. BUILD ID URL/stop - aborts a Pipeline. BUILD ID URL/term - forcibly terminates a build (should only be used if stop does not work).


2 Answers

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

like image 141
PKo Avatar answered Sep 20 '22 15:09

PKo


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"
      }
     }
 }
like image 45
Pankaj Khali Avatar answered Sep 20 '22 15:09

Pankaj Khali