Unable to access environment variable in email body

1.2k views Asked by At

Here's one environment variable MODEL in my Jenkins declarative script. But I am not able to use this MODEL variable into the email body. Though it is working fine in the email subject!

pipeline {
  agent any
  stages {
    stage(‘Test’) {
        environment {            
            MODEL = “Some ML Model v1.2.3”
        }
        steps {
          ...
        }
        post {
            always {
                emailext attachLog: true,
                    attachmentsPattern: ‘**/reports/*.html’,
                    mimeType: “text/html”,
                    to: '[email protected]’,
                    subject: “Test - ${env.MODEL} Build [${env.BUILD_NUMBER}]“,
                    body: '''<html>
                        <p><b>Regression Test Report</b></p>
                        <p>$MODEL</p>
                        <p>${MODEL}</p>
                        <p>"${MODEL}"</p>
                        <p>"${env.MODEL}"</p>
                        <p>""'${MODEL}'""</p>
                        <p>""'${env.MODEL}'""</p>
                        <p>"""${env.MODEL}"""</p>
                        <p>${ENV,var="MODEL"}</p>
                        <p>"${ENV,var="MODEL"}"</p>
                        <p>Build URL: $BUILD_URL</p>
                        <p>Build Status: $BUILD_STATUS</p>                        
                        <br />
                        <p><b>Console Output</b></p>
                        <pre>${BUILD_LOG_REGEX, regex="^.*test session starts.*$", showTruncatedLines=false}</pre>
                        <pre>${BUILD_LOG_EXCERPT, start="^.*test.*==$", end="^.*in.*==$"}</pre>
                        ...
                        </html>'''
                }
            }
        }
    }
}

I have tried multiple things as shown, but this script is just producing following email body:

enter image description here

Also went through several links like the following but still did not get the drill. Please suggest something.

2

There are 2 answers

0
Mr. V.K. On

For such cases you need to use triple double quotes when defining template:

body:"""
  var: ${var}
  envvar: ${env.VAR}
"""
1
Cau Pham On

I tryed '''+VAR+''' and it's work fine!