Skip to content

Instantly share code, notes, and snippets.

@boynoiz
Created January 29, 2021 07:19
Show Gist options
  • Save boynoiz/5a8c19e775dd8c27a49d89cc5f8b1de2 to your computer and use it in GitHub Desktop.
Save boynoiz/5a8c19e775dd8c27a49d89cc5f8b1de2 to your computer and use it in GitHub Desktop.
Custom Jenkins Email Template
<!DOCTYPE html>
<html lang="en">
<head>
<title>Jenkins build report</title>
<style type="text/css">
body, table, td, th, p {
font-family: Calibri, Verdana, Helvetica, sans serif;
font-size: 12px;
color: black;
}
.console {
font-family: Courier New, sans serif;
}
table.section {
width: 100%;
border: thin black dotted;
}
table.section > tbody > tr.last > td {
padding-bottom: 20px;
}
table.section > tbody > tr.revision, tr.files-edit > td:first-child {
width: 15%;
}
.td-title-main {
color: white;
font-size: 200%;
padding-left: 5px;
font-weight: bold;
}
.td-title {
color: white;
font-size: 120%;
font-weight: bold;
padding-left: 5px;
text-transform: uppercase;
}
.td-title-tests {
font-weight: bold;
font-size: 120%;
}
.td-header-maven-module {
font-weight: bold;
font-size: 120%;
}
.td-maven-artifact {
padding-left: 5px;
}
.tr-title {
background-color: <%= (build.result == null || build.result.toString() == 'SUCCESS') ? '#27AE60' : build.result.toString() == 'FAILURE' ? '#E74C3C' : '#f4e242' %>;
}
.test {
padding-left: 20px;
}
.test-fixed {
color: #27AE60;
}
.test-failed {
color: #E74C3C;
}
</style>
</head>
<body>
<!-- BUILD RESULT -->
<table class="section">
<tr class="tr-title">
<td class="td-title-main" colspan=2>
BUILD ${build.result ?: 'COMPLETED'}
</td>
</tr>
<tr>
<td>URL:</td>
<td><A href="${rooturl}${build.url}">${rooturl}${build.url}</A></td>
</tr>
<tr>
<td>Project:</td>
<td>${project.name}</td>
</tr>
<tr>
<td>Date:</td>
<td>${it.timestampString}</td>
</tr>
<tr>
<td>Duration:</td>
<td>${build.durationString}</td>
</tr>
<tr>
<td>Cause:</td>
<td><% build.causes.each() { cause -> %> ${cause.shortDescription} <% } %></td>
</tr>
</table>
<br/>
<!-- CHANGE SET -->
<%
def changeSets = build.changeSets
if(changeSets != null) {
def hadChanges = false %>
<table class="section">
<tr class="tr-title">
<td class="td-title" colspan="2">CHANGES</td>
</tr>
<% changeSets.each() {
cs_list -> cs_list.each() {
cs -> hadChanges = true %>
<tr class="revision">
<td>
Revision <%= cs.metaClass.hasProperty('commitId') ? cs.commitId : cs.metaClass.hasProperty('revision') ? cs.revision : cs.metaClass.hasProperty('changeNumber') ? cs.changeNumber : "" %> by <B><%= cs.author %></B>
</td>
<td>${cs.msgAnnotated}</td>
</tr>
<% cs.affectedFiles.each() { p ->
if (p == cs.affectedFiles.last()) { %>
<tr class="files-edit last">
<td>${p.editType.name}</td>
<td>${p.path}</td>
</tr>
<% } else { %>
<tr class="files-edit">
<td>${p.editType.name}</td>
<td>${p.path}</td>
</tr>
<%
}
}
}
}
if ( !hadChanges ) { %>
<tr>
<td colspan="2">No Changes</td>
</tr>
<% } %>
</table>
<br/>
<% } %>
<!-- ARTIFACTS -->
<%
def artifacts = build.artifacts
if ( artifacts != null && artifacts.size() > 0 ) { %>
<table class="section">
<tr class="tr-title">
<td class="td-title">BUILD ARTIFACTS</td>
</tr>
<% artifacts.each() {
f -> %>
<tr>
<td>
<a href="${rooturl}${build.url}artifact/${f}">${f}</a>
</td>
</tr>
<% } %>
</table>
<br/>
<% } %>
<!-- MAVEN ARTIFACTS -->
<%
try {
def mbuilds = build.moduleBuilds
if ( mbuilds != null ) { %>
<table class="section">
<tr class="tr-title">
<td class="td-title">BUILD ARTIFACTS</td>
</tr>
<%
try {
mbuilds.each() {
m -> %>
<tr>
<td class="td-header-maven-module">${m.key.displayName}</td>
</tr>
<%
m.value.each() {
mvnbld -> def artifactz = mvnbld.artifacts
if ( artifactz != null && artifactz.size() > 0) { %>
<tr>
<td class="td-maven-artifact">
<% artifactz.each() {
f -> %>
<a href="${rooturl}${mvnbld.url}artifact/${f}">${f}</a><br/>
<% } %>
</td>
</tr>
<% }
}
}
} catch(e) {
// we don't do anything
} %>
</table>
<br/>
<% }
} catch(e) {
// we don't do anything
} %>
<!-- JUnit TEMPLATE -->
<%
def junitResultList = it.JUnitTestResult
try {
def cucumberTestResultAction = it.getAction("org.jenkinsci.plugins.cucumber.jsontestsupport.CucumberTestResultAction")
junitResultList.add( cucumberTestResultAction.getResult() )
} catch(e) {
//cucumberTestResultAction not exist in this build
}
if ( junitResultList.size() > 0 ) { %>
<table class="section">
<tr class="tr-title">
<td class="td-title" colspan="5">${junitResultList.first().displayName}</td>
</tr>
<tr>
<td class="td-title-tests">Name</td>
<td class="td-title-tests">Failed</td>
<td class="td-title-tests">Passed</td>
<td class="td-title-tests">Skipped</td>
<td class="td-title-tests">Total</td>
</tr>
<% junitResultList.each {
junitResult -> junitResult.getChildren().each {
packageResult -> %>
<tr>
<td>${packageResult.getName()}</td>
<td>${packageResult.getFailCount()}</td>
<td>${packageResult.getPassCount()}</td>
<td>${packageResult.getSkipCount()}</td>
<td>${packageResult.getPassCount() + packageResult.getFailCount() + packageResult.getSkipCount()}</td>
</tr>
<% packageResult.getPassedTests().findAll({it.getStatus().toString() == "FIXED";}).each{
test -> %>
<tr>
<td class="test test-fixed" colspan="5">
${test.getFullName()} ${test.getStatus()}
</td>
</tr>
<% } %>
<% packageResult.getFailedTests().sort({a,b -> a.getAge() <=> b.getAge()}).each{
failed_test -> %>
<tr>
<td class="test test-failed" colspan="5">
${failed_test.getFullName()} (Age: ${failed_test.getAge()})
</td>
</tr>
<% }
}
} %>
</table>
<br/>
<% } %>
<!-- CONSOLE OUTPUT -->
<%
if ( build.result == hudson.model.Result.FAILURE ) { %>
<table class="section" cellpadding="0" cellspacing="0">
<tr class="tr-title">
<td class="td-title">CONSOLE OUTPUT</td>
</tr>
<% build.getLog(100).each() {
line -> %>
<tr>
<td class="console">${org.apache.commons.lang.StringEscapeUtils.escapeHtml(line)}</td>
</tr>
<% } %>
</table>
<br/>
<% } %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment