【问题标题】:How to invoke groovy templates in the Jenkins email-ext plugin如何在 Jenkins email-ext 插件中调用 groovy 模板
【发布时间】:2020-08-12 01:06:09
【问题描述】:

我想在 Jenkins 的 email-ext 插件中使用 Groovy 脚本功能,但我是新手,似乎有很多假设的知识。就像一个人如何首先调用这些模板之一。

这个问题的答案可能很明显,但我有点迷茫,希望能指出正确的方向。

【问题讨论】:

    标签: groovy jenkins continuous-integration email-ext


    【解决方案1】:

    这个例子是基于官方的email-ext documentation,遗憾的是没有提供任何具体的例子来说明如何在Pipeline中使用$SCRIPT这行代码。如果您希望使用 HTML 模板作为电子邮件的正文,那么您需要:

    1. 创建一个名为 my-email.template 或任何你喜欢的模板文件 - 你可以找到一些模板示例 here

      <body>
        <h3>Using "build" environment variables:</h3>
        <p>
          <a href="<%= build.absoluteUrl %>"><%= build.fullDisplayName %></a>
        </p>
        <h3>List of all available "build" environment variables:</h3>
        <div>
          <% println build.properties.collect{it}.join('<br />') %>
        </div>
      </body>
      
    2. 让您的 Jenkins 管理员将 my-email.template 文件放在 Jenkins 机器上的 $JENKINS_HOME\email-templates 目录中 - 确保用户 jenkins 拥有该目录及其内容(即模板文件)

    3. 在管道中加载 my-email.template 作为正文内容:

      stage('Send email') {
          def mailRecipients = "jenkins-user@example.com"
          def jobName = currentBuild.fullDisplayName
      
          emailext body: '''${SCRIPT, template="my-email.template"}''',
          subject: "[Jenkins] ${jobName}",
          to: "${mailRecipients}",
          replyTo: "${mailRecipients}",
          recipientProviders: [[$class: 'CulpritsRecipientProvider']]
      }
      

    【讨论】:

    • 我知道这是一篇旧帖子,但我正在尝试相同的方法(通过电子邮件发送 cppcheck 报告),我发现我想尝试这个模板,我刚刚在 $JENKINS_HOME\email-templates 创建了一个 file.template然后将这样的阶段添加到我的管道emailext( mimeType: 'text/html', body: '${SCRIPT, template="my_email.template"}', subject: "[Jenkins] ${jobName}", to: "${mailRecipients}"),但我没有收到模板,而是收到了字符串${SCRIPT, template="my_email.template"}
    • @JesusFernandez 你试过用 3 个单引号括起来吗,就像上面的例子一样?
    • 是的,我有,但我刚刚意识到我正在尝试发送果冻脚本而不是 html,所以我想这对我不起作用(这是我第一次使用这个插件......)
    • 如何在这个中使用条件,这对我不起作用if ( "${env.MY_ENV}" == "true" ) { %&gt;
    【解决方案2】:

    这是一个相当古老的线程,但这是我利用内置模板所做的工作

        stage('Send Email')
        {
            steps
            {
                emailext body: '${JELLY_SCRIPT,template="html"}',recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: 'Build Successful ${JOB_NAME}',mimeType: 'text/html'
            }
                    
        }
    

    插件文档指出 html Jelly 脚本是内置的,因此您可以轻松使用它。

    https://plugins.jenkins.io/email-ext/

    【讨论】:

      【解决方案3】:

      事实上,答案是显而易见的,并且包含在 email-ext 文档中:

      ${SCRIPT, template="groovy-text.template"}
      

      【讨论】:

      • 从人们提出的问题数量来看,文档写得不好。没有关于如何在 Jenkinsfile 中使用此行的示例。简单输入此行会导致“unexpected token: SCRIPT”错误,请问您能否举例说明如何使用此行?
      • 恐怕这是很久以前的事了,我什至不记得这是针对哪个项目的,但我模糊地记得在框中使用它来获取您通过 Jenkins 网络访问的电子邮件内容用户界面。抱歉,我不能提供更多帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2015-11-05
      • 2021-04-27
      相关资源
      最近更新 更多