How to Block a Merge When Coverage Drops on a Pull Request in GitHub Actions
Exit non-zero when coverage falls below the floor, then mark that job a required status check.
Run coverage with a minimum threshold (your runner can enforce it, or compare the percentage yourself), exit 1 on a drop, and require the job in branch protection.
Steps
- Run tests with coverage and a known floor.
exit 1when the percentage is below the floor.- Make the job a required check in branch protection.
Workflow
.github/workflows/ci.yml
jobs:
coverage-gate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- name: Enforce coverage floor
run: |
npm test -- --coverage --coverageReporters=json-summary
PCT=$(node -p "require('./coverage/coverage-summary.json').total.lines.pct")
echo "Lines: $PCT%"
awk "BEGIN { exit ($PCT < 80) }" || { echo "Coverage below 80%"; exit 1; }Gotchas
- A workflow can only block a merge once the job is added as a required status check in branch protection.
- Jest also supports
coverageThresholdin config to fail the test run directly without the awk check.
Related guides
How to Post a Coverage Report Comment on a Pull Request in GitHub ActionsAdd a coverage summary comment to each pull request in GitHub Actions using a Jest coverage report and the je…
How to Upload a Coverage Report as an Artifact in GitHub ActionsAttach the generated HTML coverage report to a GitHub Actions run as a downloadable artifact with actions/upl…