简体   繁体   English

如何在 GitHub Actions 中获取或解析 Jacoco 报告的覆盖率

[英]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.

相关问题 当我的所有测试都在单独的子模块中时,如何使用 gradle 插件获取 Jacoco 覆盖率报告 - How do I get a Jacoco coverage report using gradle plugin when all my tests are in a separate submodule 如何在android studio中使用Jacoco为androidTest生成代码覆盖率报告 - how to generate code coverage report for androidTest using Jacoco in android studio 如何使用gradle在mutli模块系统中使用声纳+ jacoco生成集成测试的代码覆盖率报告 - How to generate code coverage report for integration test using sonar + jacoco in mutli module system using gradle 如何使用 build.gradle.kts 将覆盖率报告 (JaCoCo) 添加到 kotest? - How to add coverage report (JaCoCo) to kotest based using build.gradle.kts? 使用 Gradle 将多个 JaCoCo.exec 文件聚合到单个覆盖率报告中 - Aggregate several JaCoCo .exec files into a single Coverage report with Gradle 如何使用 Jacoco 获取 Gradle 项目中特定类的代码覆盖率 - How To Get Code Coverage for only specific classes in Gradle project using Jacoco 如何在gradle中将jacoco代码覆盖级别设置为模块 - How to set jacoco code coverage levels to module in gradle 如何在 Kotlin (Gradle 5) 中构建 jacoco 测试报告任务 - How to build jacoco test report task in Kotlin (Gradle 5) 如何使用 Junit5 和 Jacoco 配置 build.gradle 以进行测试覆盖 - How to configure build.gradle using Junit5 and Jacoco for test coverage 如何启用 github 操作访问 gradle 属性? - How to enable github actions access gradle properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM