简体   繁体   English

在Jenkins中将Git Commit哈希添加到Email-ext插件中

[英]Adding Git Commit Hashes to Email-ext Plugin in Jenkins

I'm looking to output the result of a Jenkins build into an email using Email-ext plugin + a groovy text template. 我希望使用Email-ext插件+一个groovy文本模板将Jenkins构建的结果输出到电子邮件中。

By tweaking the default groovy template slightly (ie, no code edits), I have gotten the output to look like: 通过略微调整默认的groovy模板 (即,没有代码编辑),我得到的输出看起来像:

* [] 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]

I would like to add the following to the email: 我想在电子邮件中添加以下内容:

  • add the hash from git for each change/commit 为每个更改/提交添加来自git的哈希
  • the output of git pull origin <branch> git pull origin <branch>的输出

I've looked at the Jenkins API and the Email-ext plugin, but being fairly new to the internals of Jenkins, I'm not clear on how or what I should be looking at. 我看过Jenkins API和Email-ext插件,但对于Jenkins的内部人员来说还是比较新的,我不清楚我应该怎么看或者应该看什么。 Any and all pointers are much appreciated! 任何和所有指针都非常感谢!

I've made some progress in this regard with getting the commit IDs. 我在获取提交ID方面取得了一些进展。 Code is as follows, and hopefully this helps others. 代码如下,希望这有助于其他人。

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}
<%      }
   } %>

Sample output is: 示例输出是:

[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

Steps to configure this are: 配置它的步骤是:

  • in $JENKINS_HOME , create a directory called email-templates -- this is the directory where the Email-ext plugin looks for scripts or templates. $JENKINS_HOME ,创建一个名为email-templates的目录 - 这是Email-ext插件查找脚本或模板的目录。
  • Create a file called my-text-template.groovy in this directory with the code above 使用上面的代码在此目录中创建一个名为my-text-template.groovy的文件
  • In the build's "Configure" option > "Add post-build action" > "Editable Email Notifications" and in "Default Content", enter ${SCRIPT,template="my-text-template.groovy"} 在构建的“配置”选项>“添加构建后操作”>“可编辑的电子邮件通知”和“默认内容”中,输入${SCRIPT,template="my-text-template.groovy"}

If/when I figure out the piece about adding the output of git pull origin <branch> into this email, I shall post it here. 如果/当我找到关于将git pull origin <branch>的输出添加到此电子邮件中的文章时,我将在此处发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM