yaml
name: CI Reliability Uplift
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
# Metrics collection job: Runs first to establish baseline and publishes metrics
metrics:
runs-on: ubuntu-latest
outputs:
cache-hit: ${{ steps.cache.outputs.cache-hit }}
test-flakes: ${{ steps.flaky.outputs.flakes }}
retry-counts: ${{ steps.retry.outputs.retries }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # Enables cache correctness baseline
- name: Cache dependencies (Correctness: Hash-based keys)
id: cache
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Publish Cache Metrics
id: cache-metrics
uses: actions/github-script@v7
with:
script: |
const cacheHit = "${{ steps.cache.outputs.cache-hit }}" === 'true';
core.setOutput('cache-hit', cacheHit ? 1 : 0);
console.log(`Cache hit rate metric: ${cacheHit ? 'HIT' : 'MISS'}`);
# Placeholder for test flakes and retries (populated post-run)
- name: Placeholder Metrics
id: flaky
run: echo "flakes=0" >> $GITHUB_OUTPUT
- name: Placeholder Retry Metrics
id: retry
run: echo "retries=0" >> $GITHUB_OUTPUT
# Publish to external metrics (e.g., Prometheus Pushgateway or Datadog)
- name: Push Metrics
if: always()
uses: shogo82148/actions-setup-prometheus-pushgateway@v1
with:
host: ${{ secrets.PROMETHEUS_PUSHGATEWAY_HOST }}
job: ci-reliability
metrics: |
ci_cache_hit_total{job="${{ github.job }}", ref="${{ github.ref }}"} ${{ steps.cache-metrics.outputs.cache-hit }}
ci_total_runs_total{job="${{ github.job }}", ref="${{ github.ref }}"} 1
# Test job with flaky quarantine, retries + jitter, cache correctness
test:
runs-on: ubuntu-latest
needs: metrics # Sequence after metrics baseline
strategy:
matrix:
# Parallelism for speed, with flake isolation per matrix
shard: [1, 2, 3]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Cache dependencies (Correctness: Multi-layer, post-run validation)
id: cache
uses: actions/cache@v4
with:
# Layer 1: Global npm cache
path: |
~/.npm
**/node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}-${{ matrix.shard }}
restore-keys: |
${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}-
${{ runner.os }}-node-
# Cache correctness: Fail if corrupted (via subsequent install check)
- name: Validate Cache Correctness
run: |
npm ci --frozen-lockfile || (echo "::error::Cache corruption detected - invalidating" && exit 1)
# Ensures cache is correct; fails build if deps mismatch
- name: Install Playwright (example for e2e tests)
if: matrix.shard == 3
run: npm install -D @playwright/test
# Retries with Jitter: Custom action for flaky operations
- name: Run Tests with Retries + Jitter
id: retry-tests
uses: ./.github/actions/retry-with-jitter
with:
command: |
npm test -- --shard=${{ matrix.shard }}/3
max-retries: 3
base-delay: 1000 # ms
multiplier: 2 # Exponential backoff
# Flaky Test Quarantine: Detect and isolate flakes
- name: Detect & Quarantine Flaky Tests
id: flaky-quarantine
if: failure() # Only on test failure
run: |
# Generate JUnit XML if not already (assume npm test outputs it)
FLAKES=$(grep -c "flaky" test-results.xml || echo 0)
if [ $FLAKES -gt 0 ]; then
echo "Flaky tests detected: $FLAKES"
echo "::notice::Quarantining flaky tests to separate job"
echo "flakes=$FLAKES" >> $GITHUB_OUTPUT
# Move flakes to quarantine dir (script below)
node scripts/quarantine-flakes.js
fi
# Update metrics
- name: Update Metrics Outputs
if: always()
run: |
echo "retries=${{ steps.retry-tests.outputs.retries }}" >> $GITHUB_OUTPUT
echo "flakes=${{ steps.flaky-quarantine.outputs.flakes }}" >> $GITHUB_OUTPUT
# Upload artifacts for flakes
- name: Upload Flaky Test Logs
if: steps.flaky-quarantine.outputs.flakes
uses: actions/upload-artifact@v4
with:
name: flaky-tests-shard-${{ matrix.shard }}
path: quarantined-tests/
# Separate job for quarantined flaky tests (runs only if flakes detected)
quarantine-flaky:
runs-on: ubuntu-latest
needs: [test, metrics]
if: needs.test.outputs.flakes > 0 # Dynamic trigger via outputs
strategy:
matrix:
test-file: ${{ fromJson(needs.test.outputs.quarantined-files || '[]') }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Run Quarantined Test Only
run: npm test -- --testPathPattern="${{ matrix.test-file }}"
# Always allow pass/fail but notify
- name: Notify Flake Status
if: failure()
uses: slackapi/slack-github-action@v1.26.0
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
with:
text: "🚨 Flaky test ${{ matrix.test-file }} failed in quarantine - needs fix!"
# Final metrics aggregation & summary
summary:
runs-on: ubuntu-latest
needs: [metrics, test, quarantine-flaky]
if: always()
steps:
- name: Aggregate Metrics
uses: actions/github-script@v7
with:
script: |
const cacheHit = parseFloat("${{ needs.metrics.outputs.cache-hit }}");
const flakes = parseFloat("${{ needs.test.outputs.test-flakes }}");
const retries = parseFloat("${{ needs.test.outputs.retry-counts }}");
const reliabilityScore = ((cacheHit * 0.3) + ((1 - flakes/100) * 0.4) + ((1 - retries/3) * 0.3)).toFixed(2);
core.summary.addHeading('CI Reliability Score', { level: 2 });
core.summary.addTable([
[{ data: 'Cache Hit Rate' }, { data: `${cacheHit * 100}%` }],
[{ data: 'Flaky Tests Quarantined' }, { data: `${flakes}` }],
[{ data: 'Avg Retries' }, { data: `${retries}` }],
[{ data: 'Overall Score' }, { data: `${reliabilityScore}/1.0` }]
]);
core.summary.write();
- name: Publish Aggregated Metrics
uses: shogo82148/actions-setup-prometheus-pushgateway@v1
with:
host: ${{ secrets.PROMETHEUS_PUSHGATEWAY_HOST }}
job: ci-summary
metrics: |
ci_reliability_score_total{ref="${{ github.ref }}"} ${{ vars.CI_RELIABILITY_SCORE || 0 }}