如何在Jenkins流水线获取Jacoco覆盖率结果并用于Slack及邮件展示
Hey there! Let’s break down how to solve both your Jacoco coverage requirements in Jenkins—no manual file parsing required, promise.
1. Store Jacoco Coverage Results in Pipeline Variables
The Jacoco Jenkins plugin makes this straightforward. You can generate the report and extract key metrics directly into pipeline variables that you can reuse anywhere else in your workflow.
Here’s a sample pipeline snippet:
pipeline { agent any stages { stage('Build & Run Tests') { steps { // Replace with your actual build/test command (Maven/Gradle) sh 'mvn clean test jacoco:report' } } stage('Capture Coverage Metrics') { steps { script { // Generate Jacoco report and fetch metrics def jacocoReport = jacoco( execPattern: 'target/jacoco.exec', // Adjust path if needed classPattern: 'target/classes', sourcePattern: 'src/main/java' ) // Extract metrics into global pipeline variables globalInstructionCoverage = jacocoReport.getCoverage('INSTRUCTION', 'TOTAL') branchCoverage = jacocoReport.getCoverage('BRANCH', 'TOTAL') lineCoverage = jacocoReport.getCoverage('LINE', 'TOTAL') // Verify the values in console echo "📌 Global Instruction Coverage: ${globalInstructionCoverage}%" echo "📌 Branch Coverage: ${branchCoverage}%" echo "📌 Line Coverage: ${lineCoverage}%" } } } } }
These variables (globalInstructionCoverage, branchCoverage, etc.) will be available in all subsequent stages of your pipeline.
2. Show Coverage in Slack & Email (No File Parsing)
You have two solid options here—either use the variables we just captured for clean, custom messages, or pull the raw console output from Jacoco directly.
Option 1: Use Captured Variables (Customizable Format)
This lets you craft clean, readable messages for your team.
Slack Notification Example
Make sure you have the Slack Notification Plugin installed in Jenkins:
stage('Notify Slack') { steps { slackSend( channel: '#your-team-channel', message: """ 🎯 Build ${currentBuild.result} - Jacoco Coverage Results --- • Global Instruction Coverage: ${globalInstructionCoverage}% • Branch Coverage: ${branchCoverage}% • Line Coverage: ${lineCoverage}% --- Full report: ${BUILD_URL}jacoco/ """.stripIndent() ) } }
Email Notification Example
Use the Email Extension Plugin for formatted emails:
stage('Send Coverage Email') { steps { emailext( to: 'your-team@example.com', subject: "Build ${currentBuild.result}: Jacoco Coverage Report", body: """ <h2>Build ${currentBuild.result} - Jacoco Coverage Summary</h2> <ul> <li>Global Instruction Coverage: ${globalInstructionCoverage}%</li> <li>Branch Coverage: ${branchCoverage}%</li> <li>Line Coverage: ${lineCoverage}%</li> </ul> <p>View full report: <a href="${BUILD_URL}jacoco/">${BUILD_URL}jacoco/</a></p> """.stripIndent() ) } }
Option 2: Use Raw Console Output (As-Is Format)
If you prefer the exact format Jacoco prints to the console, you can capture that output directly without parsing files. For Maven projects, use the jacoco:report command with console output enabled:
stage('Capture & Send Console Coverage') { steps { script { // Capture Jacoco's console output def jacocoConsoleOutput = sh( script: 'mvn jacoco:report -Djacoco.report.output=console', returnStdout: true ).trim() // Send to Slack (formatted as code block) slackSend( channel: '#your-team-channel', message: "📊 Jacoco Console Coverage:\n```${jacocoConsoleOutput}```" ) // Send via email (wrap in pre tag for monospaced format) emailext( to: 'your-team@example.com', subject: "Build ${currentBuild.result}: Jacoco Console Coverage", body: "<pre>${jacocoConsoleOutput}</pre>" ) } } }
Quick Checks Before You Start
- Ensure the Jacoco Plugin is installed in Jenkins (Manage Jenkins > Plugins)
- For Maven/Gradle projects, confirm Jacoco is configured in your build file (example Maven pom.xml snippet below):
<build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.10</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Hope this works smoothly for you—if you hit any issues with plugin versions or path configurations, feel free to tweak the snippets to match your project setup!
内容的提问来源于stack exchange,提问作者Siddhant Mishra




