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

标签 groovy jenkins continuous-integration email-ext

我想在 Jenkins 的 email-ext 插件中使用 Groovy 脚本功能,但我对此很陌生,而且似乎有很多假设的知识。就像人们如何首先调用其中一个模板一样。

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

最佳答案

本示例基于官方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']]
    }
    

关于groovy - 如何在 Jenkins email-ext 插件中调用 Groovy 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31000787/

相关文章:

java - 在 Windows7-Jenkins-Slave 上远程运行 Swing-Tests

如果未包含在脚本中,Jenkins 库步骤将失败

docker - 使用 Dockerfile 在 Docker 镜像上运行 GitHub 工作流?

sql-server - 找不到适用于jdbc:sqlserver的驱动程序

html - HTML解析-从.html文件获取和更新数据

bash - Nextflow:我应该如何在第 X 行截断 fastq 文件?进程失败并出现错误 141

grails - 在 grails 3 拦截器中的每个请求上添加一个响应头

java - Jenkins 连接到 LDAP

amazon-web-services - 如何使用 GitLab CI/CD 在 aws 中部署 docker 应用程序

azure - 如何使用 Azure Devops 将 azure GIT 存储库文件从一个文件夹移动到另一个文件夹