【问题标题】:Adding Git Commit Hashes to Email-ext Plugin in Jenkins在 Jenkins 中将 Git 提交哈希添加到 Email-ext 插件
【发布时间】:2014-03-10 21:14:07
【问题描述】:

我希望使用 Email-ext 插件 + groovy 文本模板将 Jenkins 构建的结果输出到电子邮件中。

通过稍微调整默认的groovy template(即没有代码编辑),我得到的输出如下所示:

* [] Testing default post receive hook script | Author: Dev One
  - File: README [Change type > edit]

* [] Script enabled | Author: Dev Two
 - File: README [Change type > edit]

* [] Custom Email Text tweaking | Author: Dev Three
 - File: README [Change type > edit]

我想在电子邮件中添加以下内容:

  • 为每个更改/提交添加来自 git 的哈希
  • git pull origin <branch>的输出

我已经查看了 Jenkins API 和 Email-ext 插件,但是对于 Jenkins 的内部结构来说还很新,我不清楚我应该如何查看或查看什么。任何和所有的指针都非常感谢!

【问题讨论】:

    标签: git email groovy jenkins


    【解决方案1】:

    在获取提交 ID 方面,我已经取得了一些进展。代码如下,希望对其他人有所帮助。

    Build Info:
    
      * Build Result: ${build.result}
      * Build Project: ${project.name}
      * Build URL: ${rooturl}${build.url}
      * Build Date: ${it.timestampString}
      * Build Duration: ${build.durationString}
    
    <%
    
    def changeSet = build.changeSet
    if(changeSet != null) {
      def hadChanges = false %>
    Changes in this build:
    <% changeSet.each() { cs -> hadChanges = true %>
      <%= cs.metaClass.hasProperty('commitId') ? cs.commitId : cs.metaClass.hasProperty('revision') ? cs.revision : cs.metaClass.hasProperty('changeNumber') ? cs.changeNumber : "" %>
        <% cs.affectedFiles.each() 
          {p -> %> [<%= cs.commitId[0..6] %>]: <%= cs.msgAnnotated %> | <%= cs.author %> | File: <%= p.path %> | Change type: <%= p.editType.name %>      
        <%}   
       }     
        if(!hadChanges) { %>
          No changes
        <% }     
    } %>
    
    <% if(build.result==hudson.model.Result.FAILURE) { %>
    CONSOLE OUTPUT
    <%      build.getLog(200).each() { line -> %> ${line}
    <%      }
       } %>
    

    示例输出为:

    [030bce6]: Ready for template v1 | Dev One | File: README | Change type: edit
    
    [d4a310c]: Testing git rev-list formatted email | Dev Two | File: githook | Change type: edit
    [d4a310c]: Testing git rev-list formatted email | Dev Two | File: README | Change type: edit
    

    配置步骤如下:

    • $JENKINS_HOME 中,创建一个名为email-templates 的目录——这是Email-ext 插件查找脚本或模板的目录。
    • 使用上面的代码在这个目录中创建一个名为my-text-template.groovy的文件
    • 在构建的“配置”选项>“添加构建后操作”>“可编辑电子邮件通知”和“默认内容”中,输入${SCRIPT,template="my-text-template.groovy"}

    如果/当我发现有关将git pull origin &lt;branch&gt; 的输出添加到此电子邮件中的文章时,我将在此处发布。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多