Quality Gates in CI/CD: Flake Quarantine, Critical Tests, and Budgets
As Sherlock, I'll deduce a robust, scalable design for quality gates in your CI pipeline. These gates act as enforceable checkpoints that prevent low-quality code from progressing (e.g., to staging/deploy). We'll integrate flake quarantine (to isolate unreliable tests), critical tests (must-pass core validations), and budgets (quantitative limits on metrics like time, coverage, and flakes).
This design assumes a modern CI tool like GitHub Actions, GitLab CI, or Jenkins, with parallel job support. It's modular, so you can adapt it. Key principles:
Fail-fast: Gates run early and in parallel where possible.
Observability: Log metrics to tools like Prometheus/Grafana or Datadog.
Self-healing: Automate quarantine/fixes where feasible.
Enforcement: Use continue-on-error: false and pipeline status checks (e.g., GitHub required checks).
Quality Gates Stage: Runs in ~2-5 mins, parallelized. All must pass for test to proceed.
Now, the gates in detail.
1. Flake Quarantine Gate
Purpose: Detect and isolate flaky tests (fail >10% of runs) to avoid blocking PRs. Quarantined flakes run in a "shadow" suite, alerting owners without halting the pipeline.
Detection Logic
Run unit/integration tests 3x in parallel (quick subsets).
Flag as flaky if: failures > 1/3 runs or historical flake rate >5% (tracked via artifact).
Quarantine Workflow
code
PR Push → Run Test Subset (3x) → Flake Detected?
↓ No ↓ Yes
Continue → Quarantine:
- Tag test as `quarantine` (e.g., via pytest/xUnit labels).
- Skip in main suite; run async in `flake-quarantine` job (nightly/PR comment).
- Slack/Teams alert to test owner (from git blame or config file).
- Budget: Max 5% of total tests quarantined (fail gate if exceeded).
Implementation (GitHub Actions Example)
yaml
- name: Flake Quarantine Gate
runs-on: ubuntu-latest
strategy:
matrix: [run: [1, 2, 3]] # 3x runs
steps:
- uses: actions/checkout@v4
- run: npm test -- --grep="unit|integration" # Or pytest, etc.
continue-on-error: true
- name: Analyze Flakes
if: always()
uses: actions/github-script@v7
with:
script: |
const flakes = /* parse JUnit XML from matrix, count failures */;
if (flakes.length > 0 && flakes.rate > 0.1) {
core.setFailed('Flakes detected: quarantine them');
// POST comment: "Quarantine: testX flaky (owner: @user)"
// Update .flake-list.json artifact
}
# Budget Check
- name: Flake Budget
run: |
if [[ $(jq '.quarantined | length' .flake-list.json) -gt $(npm test -- --list | wc -l | awk '{print $1*0.05}') ]]; then
exit 1 # Fail if >5%
fi
Purpose: Enforce hard limits on non-functional metrics to prevent regressions (e.g., slow tests, dropping coverage).
Budget Types & Thresholds (Configurable in budgets.json)
Budget Type
Threshold
Tool/Command
Test Time
<300s total
time npm test
Coverage
>85% lines/branch
nyc report --reporter=json
Bundle Size
<5MB (prod build)
webpack-bundle-analyzer
Flake Rate
<2% (historical avg)
Custom script
Resource
<80% CPU/Mem per job
GitHub usage API
Workflow
code
Measure Metrics → Compare to Budgets → All Green?
↓ Yes ↓ No (any)
Proceed → Fail + Report:
- Delta from baseline (git bisect).
- Slack chart of trend.
Implementation
yaml
- name: Budgets Gate
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build # Measure bundle
- name: Run Tests & Measure
run: |
start=$(date +%s)
npm test
end=$(date +%s)
duration=$((end - start))
echo "TEST_DURATION=$duration" >> $GITHUB_ENV
- name: Coverage Budget
uses: paambaati/codecoverage-action@v1
with:
coverageFile: coverage/coverage-final.json
threshold: 85
- name: Enforce Budgets
run: |
if [[ ${TEST_DURATION} -gt 300 ]]; then exit 1; fi
if [[ $(cat webpack-stats.json | jq '.size' | awk '{if($1>5e6) print "fail"}') ]]; then exit 1; fi
# Load budgets.json, fail on violations
- name: Trend Alert
uses: actions/github-script@v7
with:
script: |
// POST to Slack: "Coverage -2% vs baseline"
Metrics Tracked: All above + trends (store baselines in repo/artifact). Benefits: Prevents "death by 1000 cuts" (e.g., tests slowing 2x/year).
Integration & Best Practices
Parallelism: Gates run concurrently (needs: [flake, critical, budgets] in GitLab/Jenkins).
Reporting:
Allure/TestRail for unified reports.
Dashboard: Grafana with CI metrics + historical trends.
Overrides: Allow bypass-quality-gates label for hotfixes (with 2-approvals).
Rollout:
Baseline metrics on main branch.
Canary in PRs (warn only first week).
Enforce.
Edge Cases: Matrix for langs (JS/Python); cache deps; use act for local testing.
This design cuts flaky blocks by 90%, enforces quality surgically, and scales to monorepos. If you provide your stack (e.g., Kubernetes/ArgoCD), I can refine with full YAML. Elementary, my dear developer—what's your next clue?