How to Build a DIY Quality Gate in CI With Scripts
A DIY gate is a script that reads coverage and complexity outputs and exits non-zero when a threshold is crossed.
Many tools have a built-in threshold flag (Jest coverageThreshold, pytest-cov --cov-fail-under). For anything else, parse the report in a small script and exit 1 when it fails.
Built-in coverage gate
.github/workflows/ci.yml
jobs:
gate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npx jest --coverage --coverageThreshold='{"global":{"lines":80,"branches":70}}'Script gate on a JSON summary
Terminal
#!/usr/bin/env bash
set -euo pipefail
PCT=$(jq '.total.lines.pct' coverage/coverage-summary.json)
echo "line coverage: $PCT%"
awk "BEGIN { exit ($PCT < 80) ? 1 : 0 }" \
|| { echo "coverage below 80%"; exit 1; }Gotchas
- Use the tool built-in threshold when it exists; it is less brittle than parsing a report format.
- Emit the summary reporter (e.g.
--coverageReporters=json-summary) so the script has structured input.
Related guides
How to Enforce a Cognitive Complexity Limit in CICap function cognitive complexity in CI using the ESLint sonarjs rule or a SonarQube quality profile threshol…
How to Ratchet Quality From a Baseline in CIPrevent quality regressions on a legacy codebase by baselining current issues and failing CI only when new vi…