Skip to content

GitHub Action to Publish JaCoCo Format Code Coverage XML and attach it to the Workflow Run as a Check Run. Also can enforce Coverage Quality Gate.

License

Notifications You must be signed in to change notification settings

PavanMudigonda/jacoco-reporter

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

JaCoCo Code Coverage Reporter v5.0

GitHub Action to Publish JaCoCo Format Code Coverage XML and attach it to the Workflow Run as a Check Run. You can even set threshold coverage percentage and fail the action.

Note:-

  • The scope of this project is limited to Report and Quality Gate. Any ideas are welcome.
  • I wrote this action as opensource during my vacation time.
  • This actions is used by hundreds of repos in my organization and many other prviate org repos.

Just one request. If you are using my Github Action, please STAR ⭐ it.

Samples

This Action allows you to specify your JaCoCo Code Coverage XML Path, and then generate a markdown report from the test results and then it attaches it to the Workflow Run as a Check Run. You can even set threshold coverage percentage and fail the action.

Here's a quick example of how to use this action in your own GitHub Workflows.

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    
      # generates coverage-report.md and publishes as checkrun
      - name: JaCoCo Code Coverage Report
        id: jacoco_reporter
        uses: PavanMudigonda/jacoco-reporter@v5.0
        with:
          coverage_results_path: jacoco-report/test.xml
          coverage_report_name: Coverage
          coverage_report_title: JaCoCo
          github_token: ${{ secrets.GITHUB_TOKEN }}
          skip_check_run: false
          minimum_coverage: 80
          fail_below_threshold: false
          publish_only_summary: false
      
      # Publish Coverage Job Summary  # Optional
      - name: Add Jacocoo report to workflow run summary
        run: |
          echo "| Outcome | Value |" >> $GITHUB_STEP_SUMMARY
          echo "| --- | --- |" >> $GITHUB_STEP_SUMMARY
          echo "| Code Coverage % | ${{ steps.jacoco_reporter.outputs.coverage_percentage }} |" >> $GITHUB_STEP_SUMMARY
          echo "| :heavy_check_mark: Number of Lines Covered | ${{ steps.jacoco_reporter.outputs.covered_lines }} |" >> $GITHUB_STEP_SUMMARY
          echo "| :x: Number of Lines Missed | ${{ steps.jacoco_reporter.outputs.missed_lines }} |" >> $GITHUB_STEP_SUMMARY
          echo "| Total Number of Lines | ${{ steps.jacoco_reporter.outputs.total_lines }} |" >> $GITHUB_STEP_SUMMARY
          
      # uploads the coverage-report.md artifact  # Optional
     - name: Upload Code Coverage Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: code-coverage-report-markdown
          path: */coverage-results.md 
          retention-days: 1  

Inputs

This Action defines the following formal inputs.

Name Req Description
coverage_results_path true Path to the JaCoCo Code Coverage XML format file which will be used to generate a report.
coverage_report_name false The name of the code coverage report object that will be attached to the Workflow Run. Defaults to the name COVERAGE_RESULTS_<datetime> where <datetime> is in the form yyyyMMdd_hhmmss.
coverage_report_title false The title of the code coverage report that will be embedded in the report itself, which defaults to the same as the coverage_report_name input.
github_token false Input the GITHUB TOKEN Or Personal Access Token you would like to use. Defaults to the auto generated token. Recommended to use GitHub auto generated token ${{ secrets.GITHUB_TOKEN }}.
minimum_coverage false Input the minimum code coverage recommended.
fail_below_threshold false Set True to fail the action and False to let it pass.
skip_check_run false If true, will skip attaching the Coverage Result report to the Workflow Run using a Check Run. Useful if your report has 65k characters that is not accepted by Github REST and GraphQL APIs
publish_only_summary false If true, will publish only a summary table of the Coverage Result report to the Workflow Run using a Check Run. Useful if your full coverage report has 65k characters that is not accepted by Github REST and GraphQL APIs

Outputs

This Action defines the following formal outputs.

Name Description
coverage_percentage Coverage Percentage. Rounded to two decimals.
coveragePercentage Coverage Percentage. Rounded to two decimals.
coveragePercentageString Coverage Percentage. Rounded to two decimals with % symbol attached.
covered_lines Total Covered Lines
missed_lines Total missed Lines
total_lines Total Code Lines

Important Notes:-

  • When action is run in a pull request by dependabot or a forked repo (e.g. when bumping up a version in a pull request) this step will fail with the default github token ${{ secrets.GITHUB_TOKEN }} due to a lack of permissions. Resolution: on consumer side of workflow please add below

Possible fix The workflow needs check: write permissions.

permissions:
  checks: write
  1. Or Alternatively use Personal Authorization Token from GitHub.

Sample Screenshot (Full Coverage Report): publish_only_summary: false

image

Sample Screenshot (Summary Coverage Report): publish_only_summary: true

image

Sample Build Summary Screenshot

image

Sample PR Check Screenshot: only one Code Coverage Check Appears.

image

Sample Gradle Build Repo

https://github.com/PavanMudigonda/jacoco-playground

Sample Gradle Github Actions workflow

https://github.com/PavanMudigonda/jacoco-playground/blob/main/.github/workflows/coverage.yml

Sample Maven Build Repo

https://github.com/PavanMudigonda/java-maven-playground/

Sample Maven Github Actions workflow

https://github.com/PavanMudigonda/java-maven-playground/blob/master/.github/workflows/ci.yml

Sample Ant Build Repo

Sample Ant Github Actions workflow

PowerShell GitHub Action

This Action is implemented as a PowerShell GitHub Action.

Please checkout my Lines of Code Reporter

https://github.com/PavanMudigonda/lines-of-code-reporter/

About

GitHub Action to Publish JaCoCo Format Code Coverage XML and attach it to the Workflow Run as a Check Run. Also can enforce Coverage Quality Gate.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published