4

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

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

4

3 回答 3

11

这个例子是基于官方的email-ext 文档,遗憾的是没有提供任何关于如何使用$SCRIPTPipeline 中的代码行的具体例子。如果您希望使用 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>
    
  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']]
    }
    
于 2017-02-18T01:18:43.140 回答
1

这是一个非常古老的线程,但这是我利用内置模板所做的事情

    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/

于 2020-08-11T17:06:09.870 回答
-1

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

${SCRIPT, template="groovy-text.template"}
于 2015-06-23T11:59:06.700 回答