如何在Jenkins email-ext插件中调用groovy模板

Spa*_*rky 3 groovy continuous-integration jenkins email-ext

我想在Jenkins的email-ext插件中使用Groovy脚本功能,但我是新手,并且似乎有很多假设的知识.就像一开始就调用其中一个模板一样.

对此的答案可能非常明显,但我感到有些失落,并希望被指向正确的方向.

Gor*_*sic 6

此示例基于官方的email-ext文档,遗憾的是,它没有提供有关如何$SCRIPT在Pipeline中使用代码行的任何具体示例.如果您希望使用HTML模板作为电子邮件的正文,则需要:

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

    <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>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 让Jenkins管理员将my-email.template文件$JENKINS_HOME\email-templates放在Jenkins机器上的目录中 - 确保用户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']]
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 我知道这是一篇旧帖子,但我正在尝试同样的操作(通过电子邮件发送 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"}` (2认同)