How-to’s and Support

Publishing HTML Reports in Pipeline

Liam Newman

October 25, 2016

15 minutes to read

Most projects need more than just JUnit result reporting. Rather than writing acustom plugin for each type of report, we can use the HTML Publisher Plugin.

Let's Make this Quick

I've found a Ruby project, hermann, I'd like to build using Jenkins Pipeline. I'dalso like to have the code coverage results published with each build job. I couldwrite a plugin to publish this data, but I'm in a bit of a hurry and the build already creates an HTML report file using SimpleCov when the unit tests run.

Simple Build

I'm going to use the HTML Publisher Plugin to add the HTML-formatted code coverage report to my builds. Here's a simple pipeline for building the hermann project.

LANG:code
stage 'Build'
node {
// Checkout
checkout scm
// install required bundles
sh 'bundle install'
// build and run tests with coverage
sh 'bundle exec rake build spec'
// Archive the built artifacts
archive (includes: 'pkg/*.gem')
}

NOTE:

  • This pipeline expects to be run from a Jenkinsfile in SCM.
  • To copy and paste it directly into a Jenkins Pipeline job, replace the checkout scm step with
  • git 'https://github.com/reiseburo/hermann.git'

Simple enough, it builds, runs tests and archives the package.

blog/2016-pipeline-series-run-1.png
blog/2016-pipeline-series-run-1.png

Now I just need to add the step to publish the code coverage report.I know that rake spec creates an index.html file in the coverage directory.I've already installed the HTML Publisher Plugin. How do I add the HTML publishing step to the pipeline? The plugin page doesn't say anything about it.

Snippet Generator to the Rescue

Documentation is hard to maintain and easy to miss, even more so in a systemlike Jenkins with hundreds of plugins with each potentially having one or moregroovy fixtures to add to the Pipeline. The Pipeline Syntax "Snippet Generator" helps users navigate this jungle by providing a way to generate a code snippet for any step usingprovided inputs.

It offers a dynamically generated list of steps, based on the installed plugins. From that list I select the publishHTML step:

blog/2016-pipeline-series-snippet-generator-1.png
blog/2016-pipeline-series-snippet-generator-1.png

Then it shows me a UI similar to the one used in job configuration. I fill inthe fields, click "generate" and it shows me snippet of groovy generated fromthat input.

blog/2016-pipeline-series-snippet-generator-2.png
blog/2016-pipeline-series-snippet-generator-2.png

HTML Published

I can use that snippet directly or as a template for further customization.In this case, I'll just reformat and copy it in at the end of my pipeline. (I ran into a minor bug in the snippet generated for this plugin step. Typing error string in my search bar immediately found the bug and a workaround.)

LANG:code
/* ...unchanged... */
// Archive the built artifacts
archive (includes: 'pkg/*.gem')
// publish html
// snippet generator doesn't include "target:"
// https://issues.jenkins-ci.org/browse/JENKINS-29711.
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'coverage',
reportFiles: 'index.html',
reportName: "RCov Report"
])
}

When I run this new pipeline I am rewarded with an RCov Report link on left side,which I can follow to show the HTML report.

blog/2016-pipeline-series-run-2.png
blog/2016-pipeline-series-run-2.png
blog/2016-pipeline-series-rcov.png
blog/2016-pipeline-series-rcov.png

I even added the keepAll setting to let me go back and look at reports on old jobs asmore come in. As I said to begin with, this is not as slick as what Icould do with a custom plugin, but it is much easier and works with any staticHTML.

Links

This is the second in a series of posts showing ways to use Jenkins Pipeline. Follow along with this entire series through the links below:

Subscription confirmed

You'll now receive our latest news, releases, and event updates by email.