1

我有 Spring Boot 应用程序,它使用 gradle 进行构建和打包。我已经配置了 Jacoco 插件并在我的本地生成报告为 HTML。我想将它包含在我的 GitHub 构建工作流程中,因此每当构建运行时,我都想在运行构建的分支中上传/存储 Jacoco 生成的 html 报告。是否可以使用 GitHub 操作?

此外,一旦创建 Jacoco 构建报告,想要提取该构建的最终代码覆盖率(此百分比仅适用于我定义的覆盖率规则)并在运行构建的存储库中创建一个标记。

编辑

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.*'
            ]
        }
    }
}

工作流文件


# 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/

错误

文件“/JacocoBadgeGenerator.py”,第 88 行,在computeCoverage 中,open(filename, newline='') as csvfile: FileNotFoundError: [Errno 2] No such file or directory: 'target/site/jacoco/jacoco.csv'

4

2 回答 2

1

披露:我是这个问题所涉及的 cicirello/jacoco-badge-generator GitHub Action 的作者。

尽管默认行为假定 jacoco.csv 的 Maven 位置,但有一个操作输入可用于指示 jacoco 报告的位置。因此,在您的情况下,使用 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

看起来您的 gradle 配置更改了报告位置,因此您需要以下内容才能与您的报告位置和文件名一致:

  - name: Generate JaCoCo Badge
    id: jacoco
    uses: cicirello/jacoco-badge-generator@v2
    with:
      jacoco-csv-file: build/reports/jacoco/jacoco.csv

id: jacoco只是因为您在工作流程中的后续步骤之一是通过该 ID 引用操作输出。

于 2021-08-11T20:57:24.840 回答
0

如果可以配置 jacoco 生成jacoco.csv文件,那么 GitHub Actionjacoco-badge-generator可以生成请求的徽章。

雅可可徽章

例如,参见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>

对于一个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")
    }
}
于 2021-03-08T06:56:49.627 回答