Code coverage icon for GitHub pull requests
Parse code coverage report generated by build and post code coverage status comment to GitHub pull request:
jacoco.xml
cobertura.xml
or cobertura-coverage.xml
clover.xml
coverage.json
Manage Jenkins
| Configure System
Coverage status for GitHub Pull Requests
http(s)://hostname/api/v3/
To be able compare Pull Request coverage plugin needs master coverage for GitHub repository.
You have the option to get the master coverage (base coverage) from your SonarQube instance. Otherwise the plugin will keep track of the master coverage in the project configuration file.
Manage Jenkins
Coverage status for GitHub Pull Requests
Sonar URL
field - e.g. http://sonar.mycompany.com
Use Sonar for master coverage
Sonar access token
.The plugin will try to find the project in SonarQube based on the repository name. If more than one projects match, a warning is logged, and the first one will be used to get the coverage data.
Depends on plugin which you are using to run (trigger) pull request in Jenkins.
Publish coverage to GitHub
step inject CHANGE_ID
and CHANGE_URL
to build envs
where first one is Pull Request ID (number) and second link to repositoryAfter running tests set build result to Success
currentBuild.result = 'SUCCESS'
Trigger MasterCoverageAction to collect master coverage (scmVars is needed for multibranch)
step([$class: 'MasterCoverageAction', scmVars: [GIT_URL: env.GIT_URL]])
step([$class: 'MasterCoverageAction', jacocoCounterType: 'INSTRUCTION', scmVars: [GIT_URL: env.GIT_URL]])
step([$class: 'MasterCoverageAction', jacocoCounterType: 'INSTRUCTION', publishResultAs: 'statusCheck', scmVars: [GIT_URL: env.GIT_URL]])
Trigger CompareCoverageAction to compare coverage and publish results (scmVars is needed for multibranch)
step([$class: 'CompareCoverageAction', scmVars: [GIT_URL: env.GIT_URL]])
step([$class: 'CompareCoverageAction', jacocoCounterType: 'INSTRUCTION', sonarLogin: "login", sonarPassword: "password"])
step([$class: 'MasterCoverageAction', jacocoCounterType: 'INSTRUCTION', publishResultAs: 'statusCheck', scmVars: [GIT_URL: env.GIT_URL]])
Simple Multibranch Pipeline example
pipeline {
agent {
label 'linux'
}
tools {
maven 'maven-3.0.3'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build and Test') {
steps {
sh 'mvn clean package'
}
}
stage('Record Coverage') {
when { branch 'master' }
steps {
script {
currentBuild.result = 'SUCCESS'
}
step([$class: 'MasterCoverageAction', scmVars: [GIT_URL: env.GIT_URL]])
}
}
stage('PR Coverage to Github') {
when { allOf {not { branch 'master' }; expression { return env.CHANGE_ID != null }} }
steps {
script {
currentBuild.result = 'SUCCESS'
}
step([$class: 'CompareCoverageAction', publishResultAs: 'statusCheck', scmVars: [GIT_URL: env.GIT_URL]])
}
}
}
Because of that GitHub can't render icon picture hosted on Jenkins. Plugin can use http://shields.io public resource instead of Jenkins hosted picture. To use it:
Jenkins Configuration
Jenkins is not accessible for GitHub
If you see next picture on your pull request in GitHub:
In most cases that mean that your Jenkins runs on http
instead of https
Plugin uses same protocol as your Jenkins, however GitHub in most cases accessible by https
Default browser policy to block non secure resources on secure pages from unknown domain.
To fix that:
https
https://jenkins.io/doc/book/installing/#configuring-httphttps
onlyhttps
Manage Jenkins
Configure System
Coverage status for GitHub Pull Requests
Jenkins URL for icon in Pull Request
https
like https://jenkins.my.com
lineRate
or branchRate
as result lineRate=0 branchRate=0.5 => Coverage 25%
became 0.5
as lineRate=0
, same policy for branchRate
Supporting Master Coverage from SonarQube. Check (details)[#master-coverage-from-sonar]
Supporting of private Jenkins with public GitHub. To enable that mode goto Jenkins Configuration and turn on Jenkins is not accessible for GitHub
Add support SimpleCov JSON coverage report for Ruby
Add alternative text to coverage image for case when GitHub doesn't able to show image
Correct parsing Cobertura report for edge cases
First public release to Jenkins Plugin Repo