Today I wanted to share some fun I've been having with customizing the emails sent from my Jenkins builds. This was inspired by Narendra's comment on a previous post.
So, getting to it, I took a quick look after reading the comment and found the Email-ext plugin that enables customization of the emails that are sent to administrators and code committers (offenders?) for failing or unstable builds.
Out of the box, this plugin sends an email that lists the failing tests that were encountered. While this is nice, it doesn't pass muster in my book. Why?
It is very much catered to JUnit test results by default. These aren't JUnit results #forgoodnesssakes! We are talking MATLAB code here! I want the CI system configuration to be production grade just like the code it is integrating, which means we don't accept calling MATLAB test results JUnit test results!
Showing the failed tests is nice but I also want to see the diagnostics. If we have a chance to know immediately what the problem is then I don't want to have to click through to have to figure this out. Having the diagnostics in the email might mean I know exactly why the build fails when I get the email on my phone while riding the T on my way home. The remaining ride then has the benefit of valuable brain cycles already figuring out the solution. If I have to click through? No dice.
Make it MATLAB
OK, my strategy here is to pull the basic format from the default html email template because the customization API involves writing jelly scripts and I bet not many people have extensive experience writing in jelly (I certainly haven't). You can see the default jelly script for an html email is listed here, so for a first cut lets take a simplified version of that and make it our own.
If you analyze the default html template you can see the jelly script has this basic format:
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
<!-- Define CSS Styling -->
<!-- Produce html content for email -->
Within the main script body there is a node for styling and a node for the main body. For now lets just keep the styling that matches the default template, with one notable difference in that we define another class to account for filtered tests.
For the body content why don't we prune out what we aren't working with and just keep what we are interested in at the moment. So I'll keep the general job information, the changeset information from your SCM system, and the artifacts:
Still with me? OK good this next part gets interesting because I want to see the test results in a similar way to how MATLAB presents them to me as an array of TestResults. So rather than looping over all of the packages, we are going to print the total number of passed/failed/filtered tests and then create a section for failed tests and a section for filtered tests.
Note that what we have done here is analyzed information in the jenkins TestResult instances that contain the information included in the JUnit style xml produced by MATLAB's XMLPLugin (remember?) With this information we are looping over these results three times, once to count the totals, once to print the failed tests, and once to print the filtered tests.
Now we can show the log tail for posterity and close out the BODY and jelly tags.
Does it work? Well not yet. First we need to save this jelly script in a location that allows us to configure Jenkins to use this script when creating our emails. To do this, you need to save the script into the $JENKINS_HOME/email-templates/ folder in order to be picked up by the plugin. If you are not the jenkins administrator this may take some cajoling of said administrator. Once that cajoling is successful however, you can configure the plugin to use this script on a project by project basis. For me this looks like this:
With this script applied, I now get this email from Jenkins:
Woot woot! That email speaks MATLAB! Next step - include some diagnostics.
What's the diagnosis, doc?
This is actually quite easy with a simple modification to the failures portion of the jelly script. We just need to add another table row as preformatted text along with a call to getErrorStackTrace, which is where MATLAB's diagnostics are placed.
With this simple change Jenkins now includes the diagnostic information in the email as well.
The Best of Both Worlds
Alright, the cat is out of the bag. This can be done for the subset of email clients that support CSS hover selectors! For others, you can still send the full email content you just won't be able to see the dynamic behavior. My client happens to support the hover CSS selector so it works great for me. Here's what we need to do. First update the style section of the document with a few more style classes and the hover behavior which sets the font size of the diagnostic to zero percent when we are not hovering over the test name and sets the size to 100% when we are. This looks like the following
Here we add a few classes for table rows, and mark the test result row as light gray when we hover over it. At the same time, we also change the styling of the output row immediately following the row being hovered over to be 100%. Trickery!
This also requires a few changes to the test results portion of the jelly script as well, mainly just to opt into these different style classes.
How'd we do? Well at least in my email client, we have a nice concise email that gives me my full diagnostic information on a per test basis when I hover over a test of interest.
Here is the final jelly script. Please take this and make it better and let me know how you fare in the comments:
要发表评论，请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。