{{announcement.body}}
{{announcement.title}}

Automatic Email — Jenkins Results in HTML Table

DZone 's Guide to

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.

· DevOps Zone ·
Free Resource

We 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. 

Groovy
 




x
1
80


 
1
import groovy.json.JsonSlurper
2
import groovy.json.JsonSlurper
3
 
          
4
def inputFile = new File("C:\\IX\\jenkins\\service\\results\\regressiontestresults.json")
5
def InputJSON = new JsonSlurper().parseText(inputFile.text)
6
def writer = new StringWriter() // html is written here by markup builder
7
def markup = new groovy.xml.MarkupBuilder(writer) // the builder
8
 
          
9
String id1 = InputJSON.get("Results")
10
def id2 = InputJSON.get("Results")
11
 
          
12
markup.html {
13
  head {
14
   style(type: "text/css", ''
15
    '
16
    .failedtest {
17
     color: red;
18
     text - align: center;
19
    }
20
    ''
21
    ')
22
   }
23
   markup.body {
24
 
          
25
    p "Hi All,"
26
 
          
27
    p "Please analyze the failures and add your analysis in the remarks column."
28
 
          
29
    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."
30
 
          
31
   } {
32
    markup.table(border: "1") {
33
     markup.thead(bgcolor: "blue") {
34
      markup.tr {
35
       markup.th("Team")
36
       markup.th("Profiles")
37
       markup.th("FrameWork")
38
       markup.th("Total Tests")
39
       markup.th("Passed")
40
       markup.th("Failed")
41
       markup.th("Skipped")
42
       markup.th("Results URL")
43
       markup.th("Date")
44
       markup.th("Remarks")
45
      } // tr
46
     } // thead
47
 
          
48
     def dateinhtml
49
     def teamName
50
     markup.tbody {
51
      markup.tr {
52
       for (def data: id2) {
53
        markup.tr {
54
         teamName = data.pyTextValue.toString()
55
         markup.td(align: "left", teamName.substring(1, teamName.length() - 1))
56
         markup.td(align: "left", data.Profile)
57
         markup.td(align: "left", data.ToolRunner)
58
         markup.td(align: "center", data.TotalTests)
59
         markup.td(align: "center", data.Passed)
60
         if (data.Failed.toInteger() == 0) {
61
          markup.td(align: "center", data.Failed)
62
         } else {
63
          markup.td('class': 'failedtest', data.Failed)
64
         }
65
 
          
66
         markup.td(align: "center", data.Skipped)
67
         markup.td(align: "left", data.ResultsURL)
68
         //  markup.td(align:"right",data.DateTime)
69
         dateinhtml = data.DateTimeValue.toString()
70
         markup.td(align: "right", dateinhtml.substring(1, 11))
71
 
          
72
        } // foreach
73
       } // td
74
      } // tr
75
     } //tbody
76
    } // table
77
   }
78
 
          
79
   def newFile = new File("C://IX//jenkins//service//results//regressiontestresults.html")
80
   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 notifications

Email received/Output: When the job is executed successfully we will receive an email in the below format.

Email received/Output

Sample JSON file snippet, regressiontestresults.json which is given to the groovy file as input.

JSON file snippet

Recommendations:

  1. 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
  2. Configuring Extended E-mail Notification plugin is outlined here
  3. Clear the folder where the results are stored in a pre-build step
  4. 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
Topics:
api ,devops ,email ,html ,jenkins scripts ,json

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}