Search code examples
jenkinsgroovyjenkins-pipelinepipeline

Unable to get BUILD_STATUS global varaible in jenkinsfile


Im trying to send an email within jenkinsfile, the email subject contain two variable one of them exists on jenkinsfile APP_NAME and the other one is jenkins Global variable BUILD_STATUS

im getting null instead of the actual value for the build status

environment {
    mvnHome = tool name: 'myMvn', type: 'maven'
    mvnCMD = "${mvnHome}/bin/mvn"
    APP_NAME = 'test'
  }



post {
              success {

                  emailext body: '$DEFAULT_CONTENT',
                          to: '$DEFAULT_RECIPIENTS',
                          subject: "${APP_NAME} Health Check: ${env.BUILD_STATUS}",
                          attachmentsPattern: "**/target/${APP_NAME}.jpg"
              }
          }

when i changed the subject in the form below

'$APP_NAME Health Check: $BUILD_STATUS' with single quote i got the actual build status but APP_NAME appears on email $APP_NAME instead of actual name

how i can solve this conflict BUILD_STATUS needs single quote but APP_NAME needs double quote


Solution

  • Solved By creating new variable includes the BUILD_STATUS global variable

      environment {
        DEFAULT_SUBJECT = 'Health Check: $BUILD_STATUS'
      }
    

    Then call this variable as shown below

     emailext body: '$DEFAULT_CONTENT',
                      to: '$DEFAULT_RECIPIENTS',
                      subject: "${APP_NAME} ${DEFAULT_SUBJECT}",
                      attachmentsPattern: "**/target/${APP_NAME}.jpg"
          }