0
0
Jenkinsdevops~5 mins

HTML reports publishing in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your tests or builds create HTML reports that show results in a web page format. Jenkins can save and show these reports so you and your team can easily see test results or coverage without searching files.
When you run automated tests that generate HTML reports and want to see results inside Jenkins.
When you want to share build or test reports with your team through the Jenkins web interface.
When you want to keep historical HTML reports for builds to track progress over time.
When you want to avoid manually opening report files on your computer after each build.
When you want to add a visual summary of test coverage or code quality to your Jenkins job.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the project'
        // Simulate build steps
      }
    }
    stage('Test') {
      steps {
        echo 'Running tests'
        // Simulate test steps that generate HTML report in 'reports/html'
      }
    }
  }
  post {
    always {
      publishHTML(
        target: [
          allowMissing: false,
          alwaysLinkToLastBuild: true,
          keepAll: true,
          reportDir: 'reports/html',
          reportFiles: 'index.html',
          reportName: 'Test HTML Report'
        ]
      )
    }
  }
}

This Jenkinsfile defines a simple pipeline with build and test stages.

The publishHTML step in the post section tells Jenkins to collect the HTML report files from the reports/html folder.

It sets the main report file as index.html and names the report 'Test HTML Report' in Jenkins UI.

The flags mean: allowMissing: false fails the build if report is missing, alwaysLinkToLastBuild: true shows the latest report, and keepAll: true keeps reports from all builds.

Commands
This command triggers the Jenkins pipeline named 'my-pipeline' to start the build and test process, which will generate the HTML report.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1 Waiting for build to complete... Finished: SUCCESS
This command shows the console output of build #1 for the 'my-pipeline' job to verify the build and test steps ran successfully.
Terminal
jenkins-cli console my-pipeline #1
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the project [Pipeline] echo Running tests [Pipeline] publishHTML Publishing HTML report [Pipeline] End of Pipeline Finished: SUCCESS
This command fetches the published HTML report from the Jenkins web server for build #1 to verify the report is accessible.
Terminal
curl http://jenkins-server/job/my-pipeline/1/Test%20HTML%20Report/
Expected OutputExpected
<!DOCTYPE html> <html> <head><title>Test HTML Report</title></head> <body> <h1>Test Results</h1> <p>All tests passed.</p> </body> </html>
Key Concept

If you remember nothing else from this pattern, remember: Jenkins can automatically collect and display HTML reports from your builds using the publishHTML step.

Common Mistakes
Not specifying the correct report directory or file in the publishHTML step.
Jenkins will not find the report files and will fail to publish the report or show an error.
Ensure the reportDir and reportFiles match the actual location and name of your generated HTML report files.
Running publishHTML outside of a post block or after the build finishes.
The report may not be published if the step is not run after the report files are generated.
Use the publishHTML step inside the post { always { } } block to guarantee it runs after all build steps.
Setting allowMissing to true and missing the report files silently.
You may think reports are published but they are missing, causing confusion.
Set allowMissing to false during initial setup to catch missing report issues early.
Summary
Use the publishHTML step in Jenkinsfile to collect and show HTML reports from your build.
Run your pipeline to generate the report files in a known directory.
Verify the report is published by checking the Jenkins job page or fetching the report URL.