0
0
Jenkinsdevops~25 mins

JUnit test report publishing in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Publishing JUnit Test Reports in Jenkins Pipeline
📖 Scenario: Your team runs Java unit tests using Maven. The tests generate JUnit XML reports, but developers have to SSH into the build server to check results. You want to configure the Jenkins pipeline to automatically publish test reports so the team can view results directly in the Jenkins UI.
🎯 Goal: Build a Jenkins pipeline that runs Maven tests and publishes JUnit XML reports using the junit step, with proper error handling so reports are published even when tests fail.
📋 What You'll Learn
Create a Jenkins pipeline with a pipeline block and agent any
Add a Test stage that runs mvn test
Add a post section with always to publish JUnit reports
Use the junit step with the correct report path pattern
Print a confirmation message after report publishing
💡 Why This Matters
🌍 Real World
Publishing JUnit reports in Jenkins gives teams instant visibility into test health without SSH access, enabling faster feedback loops.
💼 Career
DevOps engineers configure test report publishing as a standard part of CI/CD pipelines for every Java project.
Progress0 / 4 steps
1
Create the pipeline with a Test stage
Create a Jenkins declarative pipeline with agent any and a stages section containing one stage named Test. Inside the Test stage, add a steps block with a shell command mvn test to run the unit tests.
Jenkins
Need a hint?

Start with pipeline { agent any stages { stage('Test') { steps { sh 'mvn test' } } } }.

2
Add environment variable for report path
Add an environment block inside the pipeline block to define a variable REPORT_PATH set to 'target/surefire-reports/*.xml'. This keeps the report path configurable.
Jenkins
Need a hint?

Use environment { REPORT_PATH = 'target/surefire-reports/*.xml' } inside the pipeline block.

3
Add post section to publish JUnit reports
Add a post section inside the pipeline block with an always block. Inside always, use the junit step with "${REPORT_PATH}" to publish the test reports regardless of whether tests pass or fail.
Jenkins
Need a hint?

Use post { always { junit "${REPORT_PATH}" } } after the stages block.

4
Add confirmation echo after report publishing
Inside the always block, after the junit step, add echo 'Test reports published' to confirm reports were processed.
Jenkins
Need a hint?

Add echo 'Test reports published' after the junit step inside always.