Automatic Email — Jenkins Results in HTML Table
Configure a Jenkins job with Execute Groovy script as a build step and copy the below groovy script.
Join the DZone community and get the full member experience.
Join For FreeWe often have a requirement to send all consolidated test results of nightly runs (with more than one job configured for test executions) to the team in an easily readable email.
This document outlines the process of achieving the same with a groovy script that used markup language to implement HMTL styles and Extended E-mail Notification plugin to automatically send email notifications.
In the below example, all the test results of the Jenkins jobs are pushed to a time-based database and the latest run results are extracted to a JSON format using rest API.
Configure a Jenkins job with Execute Groovy script as a build step and copy the below groovy script. This script takes regressiontestresults.json which is a JSON file, created from the API response with the latest test results of all the test executions and converts into HTML.
x
import groovy.json.JsonSlurper
import groovy.json.JsonSlurper
def inputFile = new File("C:\\IX\\jenkins\\service\\results\\regressiontestresults.json")
def InputJSON = new JsonSlurper().parseText(inputFile.text)
def writer = new StringWriter() // html is written here by markup builder
def markup = new groovy.xml.MarkupBuilder(writer) // the builder
String id1 = InputJSON.get("Results")
def id2 = InputJSON.get("Results")
markup.html {
head {
style(type: "text/css", ''
'
.failedtest {
color: red;
text - align: center;
}
''
')
}
markup.body {
p "Hi All,"
p "Please analyze the failures and add your analysis in the remarks column."
p "Note: This is the first version of the automatic email, we shall keep enhancing the format for automatic color coding and grouping of data."
} {
markup.table(border: "1") {
markup.thead(bgcolor: "blue") {
markup.tr {
markup.th("Team")
markup.th("Profiles")
markup.th("FrameWork")
markup.th("Total Tests")
markup.th("Passed")
markup.th("Failed")
markup.th("Skipped")
markup.th("Results URL")
markup.th("Date")
markup.th("Remarks")
} // tr
} // thead
def dateinhtml
def teamName
markup.tbody {
markup.tr {
for (def data: id2) {
markup.tr {
teamName = data.pyTextValue.toString()
markup.td(align: "left", teamName.substring(1, teamName.length() - 1))
markup.td(align: "left", data.Profile)
markup.td(align: "left", data.ToolRunner)
markup.td(align: "center", data.TotalTests)
markup.td(align: "center", data.Passed)
if (data.Failed.toInteger() == 0) {
markup.td(align: "center", data.Failed)
} else {
markup.td('class': 'failedtest', data.Failed)
}
markup.td(align: "center", data.Skipped)
markup.td(align: "left", data.ResultsURL)
// markup.td(align:"right",data.DateTime)
dateinhtml = data.DateTimeValue.toString()
markup.td(align: "right", dateinhtml.substring(1, 11))
} // foreach
} // td
} // tr
} //tbody
} // table
}
def newFile = new File("C://IX//jenkins//service//results//regressiontestresults.html")
newFile.write(writer.toString())
The created HTML file from the groovy script (regressiontestresults.html) should be given as an input to Extended E-mail Notification.
Email received/Output: When the job is executed successfully we will receive an email in the below format.
Sample JSON file snippet, regressiontestresults.json which is given to the groovy file as input.
Recommendations:
- This job can be configured as a downstream project of your nightly regression jobs or it can also schedule this job to send the test results in a formatted email at regular intervals of time
- Configuring Extended E-mail Notification plugin is outlined here
- Clear the folder where the results are stored in a pre-build step
- The below plugin versions for the above solution are tested on Jenkins 2.121.3
-
Groovy: 2.5.5 -
Extended-Email Notification: 2.6.6
-
Opinions expressed by DZone contributors are their own.
Comments