无法访问电子邮件正文中的环境变量 [英] Unable to access environment variable in email body

查看:91
本文介绍了无法访问电子邮件正文中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Jenkins声明性脚本中的一个环境变量 MODEL .但是我无法在电子邮件正文中使用此 MODEL 变量.虽然在电子邮件主题中可以正常工作!

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: 'myemail@company.com’,
                    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:

也经历了如下所示的几个链接,但仍然没有得到深入的了解.请提出一些建议.

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

推荐答案

emailext 插件管道方法将使用Groovy变量.因此,您需要从 env 对象: env.MODEL 中将环境变量作为键访问.此外,您需要在JP/Groovy解释器中插入字符串,这需要使用双引号进行解析,而不是将其转换为文字字符串并在 shell 步骤中使用shell解释器进行解析.方法.此外,使用 $ {} 语法来精确定义用于插值的变量名称更为安全.

The emailext plugin pipeline method will expect Groovy variables. Therefore, you would need to access environment variables as keys from within the env object: env.MODEL. Additionally, you would need to interpolate the string within the JP/Groovy interpreter, which would require resolving with the double quotes, as opposed to casting as a literal string and resolving with the shell interpreter in a shell step method for example. Additionally, it is safer to use the ${} syntax to precisely define the variable name for interpolation.

有趣的是,您为 subject 参数正确地完成了这两项操作,但是更改了 body 的语法,因此它无法正常工作.

Interestingly enough, you are doing both of these correctly for the subject argument, but have changed your syntax for the body, and therefore it is not working.

结合这些修补程序:

body: """<html>
  <p><b>Regression Test Report</b></p>
  <p>${env.MODEL}</p>
  <p>Build URL: ${env.BUILD_URL}</p>
  <p>Build Status: ${env.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>"""

这篇关于无法访问电子邮件正文中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆