1
votes

I'm looking to output the result of a Jenkins build into an email using Email-ext plugin + a groovy text template.

By tweaking the default groovy template slightly (i.e., no code edits), I have gotten the output to look like:

* [] 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
  • the output of 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. Any and all pointers are much appreciated!

1

1 Answers

3
votes

I've made some progress in this regard with getting the commit IDs. 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.
  • Create a file called my-text-template.groovy in this directory with the code above
  • In the build's "Configure" option > "Add post-build action" > "Editable Email Notifications" and in "Default Content", enter ${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.