【问题标题】:Environment variables added in pipeline not accessible in email-ext plugin在 email-ext 插件中无法访问管道中添加的环境变量
【发布时间】:2017-04-21 23:56:54
【问题描述】:

在我的管道脚本中,我添加了一些环境变量,但无法在 email-ext 插件中访问它

env.MYVAR = 'HELLO'

emailext body: '${ENV,var="MYVAR"}', subject: 'subject', to: 'hello@world.com'

由于我正在从文件中读取电子邮件正文,我也尝试替换字符串,但在 org.codehaus.groovy.runtime.StringGroovyMethods.replaceAll 出现 java.lang.NullPointerException

def myvariable = 'HELLO WORLD'
def content = readFile 'template.html'
content = content.replaceAll('MYVAR',myvariable)
...

有什么想法吗?谢谢!

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-email-ext


    【解决方案1】:

    尝试使用双引号,例如:

    emailext body: "${env.MYVAR}", subject: 'subject', to: 'hello@world.com'
    

    【讨论】:

      【解决方案2】:

      经过几个小时的尝试,我使用了字符串连接:

        pipeline {
          agent any
          stages {
             ....
        }
        post {
          always {
            script {
              env.LAST_COMMIT = sh (
                  script: 'git log -n 3',
                  returnStdout:true
              ).trim()
            }
            echo "Last 3 commits: $LAST_COMMIT"
            emailext (
                attachLog: true,
                body: '''$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS:
                         Build Url: $BUILD_URL
                         Last 3 commits: '''
                         + env.LAST_COMMIT,
                postsendScript: '$DEFAULT_POSTSEND_SCRIPT',
                presendScript: '$DEFAULT_PRESEND_SCRIPT', 
                replyTo: '$DEFAULT_REPLYTO', 
                subject: '*$BRANCH_NAME - $BUILD_STATUS* - $DEFAULT_SUBJECT', 
                to: '''
                  name@domain.com, name@domain.com
                  ''',
                from: 'jenkins@domain.com'
            )
          }
        }
      }
      

      【讨论】:

        【解决方案3】:

        您可以为电子邮件正文指定一个单独的 Groovy 脚本文件,如下所示:

        emailext body: '${SCRIPT, template="build-result.groovy"}', subject 'subject', to: 'hello@world.com'
        

        模板文件需要位于 Jenkins 安装的 email-templates 文件夹中。

        然后,您可以在 Groovy 脚本中使用以下内容来访问管道中设置的环境变量:

        <% 
        def envOverrides = it.getAction("org.jenkinsci.plugins.workflow.cps.EnvActionImpl").getOverriddenEnvironment()
        if (envOverrides.containsKey("MYVAR"))
        {
            println "myvar = " + envOverrides["MYVAR"]
        }
        %>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-05-05
          • 2012-01-30
          • 2018-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-11
          • 1970-01-01
          相关资源
          最近更新 更多