Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I have configured Jacoco plugin and generating report in my local as 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. Is it possible using GitHub Actions?

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.

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/[email protected]

      - 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 '[email protected]'
                  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'

like image 830
springbootlearner Avatar asked Oct 25 '25 06:10

springbootlearner


1 Answers

Disclosure: I am the author of the cicirello/jacoco-badge-generator GitHub Action that this question concerns.

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. 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):

  - 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:

  - 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.

like image 58
Vincent A. Cicirello Avatar answered Oct 28 '25 05:10

Vincent A. Cicirello