Skip to content
Merged
89 changes: 77 additions & 12 deletions .github/workflows/lighthouse-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
# A repo with no lighthouserc file yet needs one before this does anything
# useful — see lighthouserc.example.json in this repository for a starting
# point pitched at the advisory-first house style (warn, not error).
#
# Two ways to get the site under test:
# - build here (the default): install, run `build`, then lhci serves the
# output or runs the lighthouserc file's startServerCommand;
# - audit the caller's own build (`artifact-name`): a job earlier in the same
# run uploads its static output, and this job downloads it and skips
# install and build. Nothing is built twice, the pages audited are the
# pages that ship, and private packages (GitHub Packages) need no token
# here. Point the lighthouserc file's collect.staticDistDir at
# `artifact-path`.
name: lighthouse-ci

on:
Expand Down Expand Up @@ -45,6 +55,22 @@ on:
starting — uncommon, but some setups do it that way.
type: string
default: npm run build
artifact-name:
description: >-
Audit a build artifact instead of building here: the name of an
artifact uploaded earlier in the same workflow run (the calling job
needs that job). Install and build are skipped, and Node comes from
node-version, else the current LTS, not the repo's version file:
only lhci runs here, and it is tested against LTS releases. Empty
(the default) builds here.
type: string
default: ""
artifact-path:
description: >-
Where to unpack artifact-name, relative to the checkout. Point the
lighthouserc file's collect.staticDistDir here.
type: string
default: dist
lhci-version:
description: >-
@lhci/cli version to pin. Keep this in step across repos so a
Expand Down Expand Up @@ -99,7 +125,7 @@ jobs:
# write on the job that calls this workflow.
pull-requests: write
steps:
- uses: actions/checkout@v7.0.1
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
# This job runs npx-fetched third-party code (@lhci/cli) and holds
# pull-requests: write. Neither needs a persisted git credential —
Expand All @@ -108,8 +134,8 @@ jobs:
persist-credentials: false

- name: Install pnpm
if: inputs.package-manager == 'pnpm'
uses: pnpm/action-setup@v6.0.10
if: inputs.package-manager == 'pnpm' && inputs.artifact-name == ''
uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10
with:
version: ${{ inputs.pnpm-version }}

Expand All @@ -120,6 +146,18 @@ jobs:
id: node
run: |
set -euo pipefail
# Auditing an artifact: nothing of the repo's is installed or run,
# only lhci, so use the version lhci is tested on rather than the
# repo's (which may track a Node release lhci has not met yet).
if [ -n "${ARTIFACT}" ]; then
{
echo "file="
echo "version=${VERSION:-lts/*}"
echo "lockfile=false"
echo "cache="
} >> "$GITHUB_OUTPUT"
exit 0
fi
if [ -n "${VERSION_FILE}" ] && [ -f "${VERSION_FILE}" ]; then
echo "file=${VERSION_FILE}" >> "$GITHUB_OUTPUT"
echo "version=" >> "$GITHUB_OUTPUT"
Expand Down Expand Up @@ -149,8 +187,9 @@ jobs:
VERSION_FILE: ${{ inputs.node-version-file }}
VERSION: ${{ inputs.node-version }}
PM: ${{ inputs.package-manager }}
ARTIFACT: ${{ inputs.artifact-name }}

- uses: actions/setup-node@v7.0.0
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version-file: ${{ steps.node.outputs.file }}
node-version: ${{ steps.node.outputs.version }}
Expand All @@ -163,6 +202,7 @@ jobs:
package-manager-cache: ${{ steps.node.outputs.lockfile == 'true' }}

- name: Install dependencies
if: inputs.artifact-name == ''
run: |
set -euo pipefail
if [ -n "${INSTALL}" ]; then
Expand All @@ -183,9 +223,20 @@ jobs:
PM: ${{ inputs.package-manager }}
HAS_LOCK: ${{ steps.node.outputs.lockfile }}

# The command arrives through env, not ${{ }} interpolation, so the
# caller's string is never pasted into the script text.
- name: Build
if: inputs.build != ''
run: ${{ inputs.build }}
if: inputs.build != '' && inputs.artifact-name == ''
run: eval "${BUILD}"
env:
BUILD: ${{ inputs.build }}

- name: Download the build artifact
if: inputs.artifact-name != ''
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: ${{ inputs.artifact-name }}
path: ${{ inputs.artifact-path }}

- name: Check for a lighthouserc file
id: config
Expand Down Expand Up @@ -286,9 +337,11 @@ jobs:
# every run died with "No usable sandbox!" (tankster-ai-public job
# 116013940439). --no-sandbox is the usual fix for Chrome in a
# container, and --disable-dev-shm-usage avoids Docker's 64 MB
# /dev/shm. Set through LHCI's environment and only there, so it
# replaces a repo's own collect.settings.chromeFlags on those runners
# alone.
# /dev/shm. They are added by a wrapper that CHROME_PATH points at,
# not through lhci's settings: LHCI_COLLECT__SETTINGS__CHROME_FLAGS
# replaced the repo's whole collect.settings object (skipAudits,
# throttling, form factor), and a puppeteerScript's own launch never
# read it.
run: |
set -euo pipefail
# Exported only when set, so an empty value never masks a CHROME_PATH
Expand All @@ -297,13 +350,25 @@ jobs:
export CHROME_PATH="${SETUP_CHROME_PATH}"
fi
if [ "${SELF_HOSTED}" = "true" ]; then
export LHCI_COLLECT__SETTINGS__CHROME_FLAGS="--no-sandbox --disable-dev-shm-usage"
real="${CHROME_PATH:-}"
if [ -z "${real}" ]; then
for b in google-chrome google-chrome-stable chromium chromium-browser; do
real="$(command -v "$b")" && break
done
fi
if [ -n "${real}" ]; then
wrapper="${RUNNER_TEMP}/chrome-no-sandbox"
printf '#!/bin/sh\nexec "%s" --no-sandbox --disable-dev-shm-usage "$@"\n' "${real}" > "${wrapper}"
chmod 0755 "${wrapper}"
export CHROME_PATH="${wrapper}"
echo "Chrome for lhci: ${real}, through ${wrapper}"
fi
fi
npx --yes "@lhci/cli@${LHCI_VERSION}" autorun

- name: Upload Lighthouse results
if: always() && steps.config.outputs.found == 'true'
uses: actions/upload-artifact@v7.0.1
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: lighthouse-results
path: .lighthouseci/
Expand All @@ -323,7 +388,7 @@ jobs:
# of strict. Losing the comment shouldn't fail the check — the
# Lighthouse run itself already succeeded or failed on its own terms.
continue-on-error: true
uses: actions/github-script@v9.0.0
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
STRICT: ${{ inputs.strict }}
with:
Expand Down
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ needs to be insulated from that.
| `node-ci.yml` | Node / TypeScript | npm or pnpm, single job, `.nvmrc` as the version source of truth |
| `rust-ci.yml` | Rust | fmt, clippy `-D warnings`, test, release build (not on PRs unless `release-on-pr`), and the no-panics-in-production gate |
| `security.yml` | any | osv-scanner + gitleaks in one job, both blocking; the advisory ecosystem audit runs weekly (every run if `audit-strict`) |
| `lighthouse-ci.yml` | Node / web frontends | `lhci autorun` against a locally built app, plus a sticky PR comment with the score table; advisory by default |
| `lighthouse-ci.yml` | Node / web frontends | `lhci autorun` against a locally built app or the caller's own build artifact, plus a sticky PR comment with the score table; advisory by default |
| `runtime-gate.yml` | web apps (any stack) | boots the app (Dockerfile, command or static), crawls it in a real browser and **fails on runtime errors**: exceptions, console errors, hydration errors, failed requests, 4xx/5xx, blank pages; plus advisory screenshot diffs, advisory Lighthouse scores for the pages it reached, and an advisory Gemini explorer that uses the app like a person. See `docs/runtime-gate.md` |
| `lockfile.yml` | Node / TypeScript | `workflow_dispatch` lockfile regeneration that proves the result before committing |
| `workflows-lint.yml` | any | actionlint over `.github/workflows/`, self-updating and cached |
Expand Down Expand Up @@ -99,6 +99,52 @@ blocking on protected branches" rather than one fixed value can compute
`strict` from the triggering event instead of hardcoding it:
`strict: ${{ github.event_name != 'pull_request' }}`.

A repo whose CI already builds a static site can have Lighthouse audit that
build instead of building it again: upload the output in the build job, then
call `lighthouse-ci.yml` from a job that `needs` it, with `artifact-name` (and
`artifact-path`, where the lighthouserc file's `collect.staticDistDir` points).
Comment thread
rishitank marked this conversation as resolved.
Resolved
Install and build are skipped, so private packages need no token in that job,
and the pages audited are the pages that ship. With `staticDistDir`, give
`collect.url` as paths (`"/"`, `"/about.html"`) or `http://localhost` URLs:
lhci serves the folder over plain HTTP on a port of its own and swaps only the
port, keeping the host and scheme, so a remote URL such as a staging host would
be audited there, not the artifact, and an `https://localhost` URL would fail.
`artifact-name` and `artifact-path` arrived in `v1` on 20 November 2026; a
caller pinned to an older SHA of this workflow has to move its pin first.

```yaml
build:
runs-on: ${{ vars.CI_RUNNER || 'ubuntu-latest' }}
steps:
Comment thread
rishitank marked this conversation as resolved.
Resolved
# ... install, test, build ...
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: site
path: dist
retention-days: 1

lighthouse:
needs: [build]
permissions:
contents: read
pull-requests: write
uses: TanksterAI/.github/.github/workflows/lighthouse-ci.yml@v1
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
with:
artifact-name: site
artifact-path: dist
```

Keep Lighthouse out of the `ci-gate` job's `needs` while it is advisory. Keep
reports off public links too: `upload.target` should be `filesystem` with
`upload.outputDir` set to `.lighthouseci` (the reports then go in the
`lighthouse-results` artifact, and the PR comment finds
`.lighthouseci/manifest.json`; without `outputDir` lhci writes to the
repository root and the comment is skipped), never
`temporary-public-storage`, which publishes each report at a public link.
The artifact is as visible as the repository: in a public repository any
signed-in GitHub user can download it, so don't audit pages that must stay
unpublished, such as a staging build, from one.

The agent pipeline below (`claude-address-feedback`, `auto-merge-gate`, and
CodeRabbit Pro as the reviewer leg) deliberately does **not** follow this
advisory-first pattern -- it went live org-wide from day one, at the account
Expand Down
4 changes: 3 additions & 1 deletion lighthouserc.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
}
},
"upload": {
"target": "temporary-public-storage"
"//": "filesystem keeps the reports in the workflow's lighthouse-results artifact. Never temporary-public-storage for a private repository: it publishes every audited page's report, staging pages included, at a public link.",
"target": "filesystem",
"outputDir": ".lighthouseci"
}
}
}