[英]How to get or parse coverage persentage of Jacoco report in GitHub Actions
I have Spring Boot app it uses gradle for build and package.我有 Spring 启动应用程序,它使用 gradle 进行构建和 package。 I have configured Jacoco plugin and generating report in my local as HTML.我已经配置 Jacoco 插件并在我的本地生成报告为 HTML。 I want to include it in my GitHub build workflow so whenever build runs I want to upload/store that Jacoco generated html report in the branch in which build is running.我想将它包含在我的 GitHub 构建工作流程中,因此每当构建运行时,我想上传/存储 Jacoco 在运行构建的分支中生成的 html 报告。 Is it possible using GitHub Actions?是否可以使用 GitHub 操作?
Also once Jacoco build report is created want to extract final code coverage percentage of that build (this percentage is only for my defined coverage rule) and create a badge in the repository in which build is running.此外,一旦创建 Jacoco 构建报告,想要提取该构建的最终代码覆盖率(此百分比仅适用于我定义的覆盖率规则)并在运行构建的存储库中创建一个标记。
EDIT编辑
test {
finalizedBy jacocoTestReport // report is always generated after tests run
}
jacocoTestReport {
dependsOn test // tests are required to run before generating the report
}
jacoco {
toolVersion = "0.8.6"
//reportsDirectory = file("$buildDir/report/")
}
jacocoTestReport {
dependsOn test
sourceSets sourceSets.main
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
reports {
xml.enabled false
csv.enabled true
html.destination file("${buildDir}/reports/jacoco/Html")
csv.destination file("${buildDir}/reports/jacoco/jacoco.csv")
}
}
//Test coverage Rule to make sure code coverage is 100 %
jacocoTestCoverageVerification {
violationRules {
rule {
element = 'CLASS'
limit {
counter = 'LINE'
value = 'COVEREDRATIO'
minimum = 1.0
}
excludes = [
'com.cicd.herokuautodeploy.model.*',
'com.cicd.herokuautodeploy.HerokuautodeployApplication',
'com.cicd.herokuautodeploy.it.*'
]
}
}
}
WorkFlow File工作流文件
# This workflow will build a Java project with Gradle whenever Pull and Merge request to main branch
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
name: Build WorkFlow - Building and Validating Test Coverage
on:
pull_request:
branches: [ main,dev ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.11
uses: actions/setup-java@v1
with:
java-version: 1.11
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
- name: Generate JaCoCo Badge
id: jacoco
uses: cicirello/jacoco-badge-generator@v2.0.1
- name: Log coverage percentage
run: |
echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"
- name: Commit the badge (if it changed)
run: |
if [[ `git status --porcelain` ]]; then
git config --global user.name 'UserName'
git config --global user.email 'useremail@gmail.com'
git add -A
git commit -m "Autogenerated JaCoCo coverage badge"
git push
fi
- name: Upload JaCoCo coverage report
uses: actions/upload-artifact@v2
with:
name: jacoco-report
path: reports/jacoco/
Error错误
File "/JacocoBadgeGenerator.py", line 88, in computeCoverage with open(filename, newline='') as csvfile: FileNotFoundError: [Errno 2] No such file or directory: 'target/site/jacoco/jacoco.csv'文件“/JacocoBadgeGenerator.py”,第 88 行,在computeCoverage 中,open(filename, newline='') as csvfile: FileNotFoundError: [Errno 2] No such file or directory: 'target/site/jacoco/jacoco.csv'
Disclosure: I am the author of the cicirello/jacoco-badge-generator GitHub Action that this question concerns.披露:我是这个问题所涉及的 cicirello/jacoco-badge-generator GitHub Action 的作者。
Although the default behavior assumes Maven's location of the jacoco.csv, there is an action input that can be used to indicate the location of the jacoco report.尽管默认行为假定 Maven 的位置为 jacoco.csv,但有一个操作输入可用于指示 jacoco 报告的位置。 So in your case, with gradle, you can change the cicirello/jacoco-badge-generator step of your workflow to the following (if you use gradle's default location and filename for the report):因此,在您的情况下,使用 gradle,您可以将工作流程的 cicirello/jacoco-badge-generator 步骤更改为以下内容(如果您使用 gradle 的默认位置和报告的文件名):
- name: Generate JaCoCo Badge
uses: cicirello/jacoco-badge-generator@v2
with:
jacoco-csv-file: build/reports/jacoco/test/jacocoTestReport.csv
It looks like your gradle configuration changed the report location though, so you will need the following in order to coincide with your report location and filename:看起来您的 gradle 配置更改了报告位置,因此您需要以下内容才能与您的报告位置和文件名一致:
- name: Generate JaCoCo Badge
id: jacoco
uses: cicirello/jacoco-badge-generator@v2
with:
jacoco-csv-file: build/reports/jacoco/jacoco.csv
The id: jacoco
is just because one of your later steps in your workflow is referring to the action outputs through that id. id: jacoco
只是因为您在工作流程中的后续步骤之一是通过该 id 引用操作输出。
If you can configure jacoco to generate a jacoco.csv
file, then the GitHub Action jacoco-badge-generator
can generate the requested badge.如果可以配置 jacoco 生成jacoco.csv
文件,那么 GitHub Action jacoco-badge-generator
可以生成请求的徽章。
See for instance " Use Jacoco And GitHub Actions to Improve Code Coverage " from Rodrigo Graciano for an example of pom.xml
project configuration to generate the report during build.例如,参见Rodrigo Graciano的“ 使用 Jacoco 和 GitHub 操作来提高代码覆盖率”,以pom.xml
项目配置的示例,以在构建期间生成报告。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
For a gradle example :对于gradle 示例:
jacocoTestReport {
dependsOn test
sourceSets sourceSets.main
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
reports {
xml.enabled false
csv.enabled true
html.destination file("${buildDir}/reports/jacoco/Html")
csv.destination file("${buildDir}/reports/jacoco/jacoco.csv")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.