|
| 1 | +name: Lighthouse |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + workflow_dispatch: |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +# Including `github.workflow` in the key keeps this from colliding with any |
| 12 | +# other workflow that happens to share a ref-based group prefix. |
| 13 | +concurrency: |
| 14 | + group: lighthouse-${{ github.workflow }}-${{ github.ref }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + lighthouse: |
| 19 | + name: Lighthouse audit |
| 20 | + runs-on: ubuntu-latest |
| 21 | + timeout-minutes: 15 |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 24 | + |
| 25 | + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 |
| 26 | + with: |
| 27 | + bun-version: 1.3.13 |
| 28 | + |
| 29 | + # Bun is the package manager and script runner, but Next.js (and the |
| 30 | + # lhci binary) run on Node. Pin Node via .nvmrc so a future GitHub |
| 31 | + # bump can't break the audit silently. |
| 32 | + - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 |
| 33 | + with: |
| 34 | + node-version-file: .nvmrc |
| 35 | + |
| 36 | + # Cache Bun's resolved package store keyed on the lockfile hash. |
| 37 | + - name: Cache Bun install cache |
| 38 | + uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 |
| 39 | + with: |
| 40 | + path: ~/.bun/install/cache |
| 41 | + key: ${{ runner.os }}-bun-${{ hashFiles('bun.lock') }} |
| 42 | + restore-keys: | |
| 43 | + ${{ runner.os }}-bun- |
| 44 | +
|
| 45 | + # Cache Next.js's incremental build output. Keyed on lockfile + commit |
| 46 | + # SHA so the exact-key match is always per-commit fresh; restore-keys |
| 47 | + # fall back to any previous build on the same lockfile so most CI runs |
| 48 | + # hit a cache and skip rebuilding unchanged webpack modules. |
| 49 | + - name: Cache Next.js build cache |
| 50 | + uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 |
| 51 | + with: |
| 52 | + path: ${{ github.workspace }}/.next/cache |
| 53 | + key: ${{ runner.os }}-nextjs-${{ hashFiles('bun.lock') }}-${{ github.sha }} |
| 54 | + restore-keys: | |
| 55 | + ${{ runner.os }}-nextjs-${{ hashFiles('bun.lock') }}- |
| 56 | + ${{ runner.os }}-nextjs- |
| 57 | +
|
| 58 | + - name: Install |
| 59 | + run: bun install --frozen-lockfile |
| 60 | + |
| 61 | + - name: Build |
| 62 | + run: bun run build |
| 63 | + |
| 64 | + # The action handles starting the server (per lighthouserc.cjs's |
| 65 | + # `startServerCommand`), running Lighthouse against each URL, asserting |
| 66 | + # the thresholds, and uploading reports to temporary public storage. |
| 67 | + # Threshold rationale lives in lighthouserc.cjs alongside the values. |
| 68 | + - name: Lighthouse CI |
| 69 | + id: lighthouse |
| 70 | + uses: treosh/lighthouse-ci-action@512cc908a55bfb0ad231facca52adf3d3a651df4 # v12 |
| 71 | + with: |
| 72 | + configPath: ./lighthouserc.cjs |
| 73 | + uploadArtifacts: true |
| 74 | + temporaryPublicStorage: true |
| 75 | + |
| 76 | + # Build a markdown table from the .lighthouseci/lhr-*.json reports. |
| 77 | + # `unique_by(.url)` collapses duplicate rows if `numberOfRuns > 1` |
| 78 | + # (the action writes the median LHR per URL, but be defensive); |
| 79 | + # `sort_by(.url)` keeps row order deterministic across runs. |
| 80 | + # `if: always()` so the PR comment still posts when assertions fail — |
| 81 | + # reviewers can see *which* category regressed without digging through |
| 82 | + # the workflow log. |
| 83 | + # |
| 84 | + # Per-cell emoji follows Lighthouse's own scoring buckets: 🟢 ≥90, |
| 85 | + # 🟡 50–89, 🔴 <50. Floor thresholds in lighthouserc.cjs are below |
| 86 | + # the 🟢 cutoff, so a cell can pass assertions and still show 🟡 — the |
| 87 | + # visual signals "we locked this in but it's improvable." |
| 88 | + - name: Format PR comment |
| 89 | + if: always() && github.event_name == 'pull_request' && steps.lighthouse.outputs.links != '' |
| 90 | + id: lh-comment |
| 91 | + env: |
| 92 | + LINKS_JSON: ${{ steps.lighthouse.outputs.links }} |
| 93 | + run: | |
| 94 | + { |
| 95 | + echo 'body<<COMMENT_EOF' |
| 96 | + echo '## 🔦 Lighthouse audit' |
| 97 | + echo '' |
| 98 | + echo '| URL | Perf | A11y | Best practices | SEO | Report |' |
| 99 | + echo '|---|:---:|:---:|:---:|:---:|---|' |
| 100 | + jq -s --argjson links "$LINKS_JSON" -r ' |
| 101 | + def emo(s): if s >= 90 then "🟢" elif s >= 50 then "🟡" else "🔴" end; |
| 102 | + def cell(s): emo(s) + " " + (s | tostring); |
| 103 | + map({ |
| 104 | + url: .finalUrl, |
| 105 | + perf: (.categories.performance.score * 100 | floor), |
| 106 | + a11y: (.categories.accessibility.score * 100 | floor), |
| 107 | + bp: (.categories["best-practices"].score * 100 | floor), |
| 108 | + seo: (.categories.seo.score * 100 | floor) |
| 109 | + }) |
| 110 | + | unique_by(.url) |
| 111 | + | sort_by(.url) |
| 112 | + | map( |
| 113 | + "| `" + (.url | sub("^https?://[^/]+"; "")) + "`" |
| 114 | + + " | " + cell(.perf) |
| 115 | + + " | " + cell(.a11y) |
| 116 | + + " | " + cell(.bp) |
| 117 | + + " | " + cell(.seo) |
| 118 | + + " | [report](" + ($links[.url] // "#") + ") |" |
| 119 | + ) |
| 120 | + | join("\n") |
| 121 | + ' .lighthouseci/lhr-*.json |
| 122 | + echo '' |
| 123 | + echo "_Run [\`${GITHUB_RUN_ID}\`](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}) · $(date -u +'%Y-%m-%d %H:%M UTC')_" |
| 124 | + echo 'COMMENT_EOF' |
| 125 | + } >> "$GITHUB_OUTPUT" |
| 126 | +
|
| 127 | + - name: Post or update PR comment |
| 128 | + if: github.event_name == 'pull_request' && steps.lh-comment.outputs.body != '' |
| 129 | + uses: marocchino/sticky-pull-request-comment@0ea0beb66eb9baf113663a64ec522f60e49231c0 # v3.0.4 |
| 130 | + with: |
| 131 | + header: lighthouse |
| 132 | + message: ${{ steps.lh-comment.outputs.body }} |
0 commit comments