KISS

Be Among the 20% of the Best!

Jenkins: Attaching JUnit Trend Graph to Emails

| comments

Jenkins comes with a good support for analyzing JUnit tests results and producing reports, for instance, a trend graph — number of tests executed over builds:

It would be nice to include the test trend graph into email notifications.

The email-ext plugin allows you to easily attach files, but we need more. The thing is the image is rendered when you access the URL like http://jenkins.local:8080/job/myjob/test/trend. So we need to upload an image from the URL. I’d need to write a Groovy script for that.

There is an easier way: download the image to a file and attach it:

1
curl -o test-trend.png "${BUILD_URL}/testReport/history/durationGraph/png?start=0&end=25"

Oops, “Authentication required”. In my setup, anonymous doesn’t have access to the jobs. So I created a user job and had to give her Overall.Read and Job.Read access.

Here is a little script to download the file with authorization:

1
2
3
4
5
6
# build PROJECT_URL out of BUILD_URL
# RTFM: http://tldp.org/LDP/abs/html/refcards.html#AEN22828
PROJECT_URL=$( expr "$BUILD_URL" : '\(.*/\).*/' )
curl "${JENKINS_URL}/j_acegi_security_check" -c - -H 'Content-Type: application/x-www-form-urlencoded' \
    --data-binary 'j_username=job&j_password=job_pass&from=%2F&json=%7B%22j_username%22%3A+%22job%22%2C+%22j_password%22%3A+%22job_pass%22%2C+%22remember_me%22%3A+false%2C+%22from%22%3A+%22%2F%22%7D&Submit=log+in' | \
curl -b - -o test-trend.png "${PROJECT_URL}/test/trend"

Briefly, this shell build step gets a URL to the job itself w/o the current build number (http://jenkins.local:8080/job/csdk/7/ => http://jenkins.local:8080/job/csdk/), then logs in as the job user, and download the image. Piping between the two curl commands allows to use the auth cookies to get the image (curl … --cookie-jar - | curl --cookie - …). Then attach your test-trend.png in an “Editable Email Notification” post-build action.

By using the Any Build Step Plugin, you can run this shell build step as a post-build action after publishing JUnit report (http://stackoverflow.com/a/25370055/635603).

Great, it worked! Almost… The image didn’t contain test results of the current build, even though the “Publish JUnit test result report” step has been executed before. I think this is the intended behavior not to include the latest build until it’s finished, but I couldn’t find any workaround to include it. To sum up, the script works great, email has an image, but without the current build.

Comments