Skip to content
Latchkey

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 1 when 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 coverageThreshold in config to fail the test run directly without the awk check.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →