I work for Akkio, where I’m building out a no-code predictive AI platform. If you’re looking to harness the power of AI without needing a data scientist, give us a try!
Had some difficulty finding an up-to-date way to do this, so I figure I save anybody looking for this in the future some time.
Without future ado, here’s the .github/workflows/.yml:
name: Code Coverage
on:
pull_request:
branches: [ "master" ]
push:
branches: [ "master" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'temurin'
cache: maven
- name: Generate Coverage Report
run: |
mvn -B package --file pom.xml
- name: Upload Report
uses: 'actions/upload-artifact@v2'
with:
name: jacoco-report
path: ${{ github.workspace }}/target/site/jacoco/jacoco.xml
- name: Add coverage to PR
id: jacoco
uses: madrapps/jacoco-report@v1.2
with:
paths: ${{ github.workspace }}/target/site/jacoco/jacoco.xml
token: ${{ secrets.GITHUB_TOKEN }}
min-coverage-overall: 80
min-coverage-changed-files: 80
title: Code Coverage
- name: Save Coverage To Environment Variable
run: |
echo "TOTAL_COVERAGE=${{ steps.jacoco.outputs.coverage-overall }}" >> $GITHUB_ENV
echo "CHANGED_FILES_COVERAGE=${{ steps.jacoco.outputs.coverage-changed-files }}" >> $GITHUB_ENV
- name: Print & Check Coverage Info
run: |
import os
import sys
print("Total Coverage: " + str(os.environ["TOTAL_COVERAGE"]))
print("Changed Files Coverage: " + str(os.environ["CHANGED_FILES_COVERAGE"]))
if float(os.environ["TOTAL_COVERAGE"]) < 80 or float(os.environ["CHANGED_FILES_COVERAGE"]) < 80:
print("Insufficient Coverage!")
sys.exit(-1) # Cause Status Check Failure due to noncompliant coverage
sys.exit(0)
shell: python
Enter fullscreen mode Exit fullscreen mode
The Python script at the end technically returns a 255 for some reason in case of insufficient coverage, but it still returns a nonzero exit code, which is what we need.
Some notes:
- Maven dependencies are cached. Subsequent runs should be much faster than the initial one.
- Python is necessary because I couldn’t find a convenient way to parse the coverage number jacoco-report gives us (which ends up as a string) into a float in Bash.
- madrapps/jacoco-report is not optional, though you can disable the comment it adds onto the PR if you like. It’s necessary because it outputs the
steps.jacoco.outputs.coverage-overall
andsteps.jacoco.outputs.coverage-changed-files
variables.
原文链接:Github Actions – Use Maven + Jacoco to Gate PR Merging by Code Coverage
© 版权声明
THE END
暂无评论内容