From a79de5b6db4f553f14010693a741c94fa710fdd8 Mon Sep 17 00:00:00 2001 From: Alex Grozav Date: Wed, 25 Nov 2026 22:56:07 +0200 Subject: [PATCH 1/2] ci: add the shared nightly Lighthouse workflow A reusable workflow that audits a repository's site only when its branch moved since the last audit, plus a self-test that calls it against a fixture page on each pull request that changes it. Co-authored-by: multica-agent --- .github/workflows/lighthouse-test.yml | 28 ++++ .github/workflows/lighthouse.yml | 224 ++++++++++++++++++++++++++ test/lighthouse/lighthouserc.json | 20 +++ test/lighthouse/site/index.html | 22 +++ 4 files changed, 294 insertions(+) create mode 100644 .github/workflows/lighthouse-test.yml create mode 100644 .github/workflows/lighthouse.yml create mode 100644 test/lighthouse/lighthouserc.json create mode 100644 test/lighthouse/site/index.html diff --git a/.github/workflows/lighthouse-test.yml b/.github/workflows/lighthouse-test.yml new file mode 100644 index 0000000..ab13410 --- /dev/null +++ b/.github/workflows/lighthouse-test.yml @@ -0,0 +1,28 @@ +# Tests the shared Lighthouse workflow on each pull request that changes it, +# by calling this branch's own copy against a small fixture page. This +# repository has no package.json, `.node-version` or lockfile, so the inputs +# below also cover the version inputs and an empty install. +name: Lighthouse self-test + +on: + pull_request: + paths: + - .github/workflows/lighthouse.yml + - .github/workflows/lighthouse-test.yml + - test/lighthouse/** + +permissions: {} + +jobs: + lighthouse: + name: Lighthouse self-test + permissions: + actions: read # the change check reads earlier runs + contents: read + uses: ./.github/workflows/lighthouse.yml + with: + install: "" + node-version: "24" + pnpm-version: "11.25.0" + audit: pnpm dlx @lhci/cli@0.16.1 autorun --config=test/lighthouse/lighthouserc.json + reports: .lighthouseci diff --git a/.github/workflows/lighthouse.yml b/.github/workflows/lighthouse.yml new file mode 100644 index 0000000..fd40932 --- /dev/null +++ b/.github/workflows/lighthouse.yml @@ -0,0 +1,224 @@ +# The shared Lighthouse workflow for uxfront-com repositories. Each repository +# calls it from its own `.github/workflows/lighthouse.yml`, on a nightly +# schedule, pinned to the full SHA of a commit on this repository's main branch. +# +# The caller's job must grant `actions: read` and `contents: read`. A called +# workflow can only lower the permissions it gets: the `changes` job needs +# `actions: read` to read earlier runs, and the `audit` job needs +# `contents: read` to check out the code. The caller also keeps the +# concurrency group, since a called workflow that uses the caller's group +# waits on itself. The caller's shape: +# +# name: Lighthouse +# on: +# schedule: +# - cron: " * * *" +# workflow_dispatch: +# # Tests the caller itself, and each later pin bump, before it merges. +# pull_request: +# paths: [.github/workflows/lighthouse.yml] +# concurrency: +# group: lighthouse-${{ github.ref }} +# cancel-in-progress: false +# permissions: {} +# jobs: +# lighthouse: +# name: Lighthouse +# permissions: +# actions: read # the change check reads earlier runs +# contents: read +# uses: uxfront-com/.github/.github/workflows/lighthouse.yml@<40-char SHA> # main +# with: +# build: +# audit: +# reports: +# +# The change check: only a scheduled run can skip. It finds the caller's +# workflow, walks back through its completed runs on the same branch, newest +# first, and stops at the first one whose `Audit` job concluded `success`, +# `failure` or `timed_out`. A skipped or cancelled audit doesn't count, so it +# runs again the next night. A scheduled run skips the audit only when the +# branch is still at the commit that run audited. Every other event audits. +# A failed lookup fails the run, never turns into a skip. +name: Lighthouse + +on: + workflow_call: + inputs: + install: + description: The command that installs dependencies. Empty skips the step. + type: string + default: pnpm install --frozen-lockfile + build: + description: The command that builds the site. Empty skips the step. + type: string + default: "" + audit: + description: The command that runs Lighthouse and exits non-zero when it fails. It runs from the repository root, so it can `cd`. + type: string + required: true + reports: + description: The path to upload as the `lighthouse-reports` artifact. + type: string + default: .lighthouseci + node-version: + description: The Node.js version to use. When set, it wins over `node-version-file`. + type: string + default: "" + node-version-file: + description: The file to read the Node.js version from, used when `node-version` is empty. + type: string + default: .node-version + pnpm-version: + description: The pnpm version to install. Empty reads `packageManager` from package.json. + type: string + default: "" + timeout-minutes: + description: The timeout of the `Audit` job, in minutes. + type: number + default: 30 + +permissions: {} + +jobs: + changes: + name: Changes + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + actions: read # the run history of the caller's workflow + outputs: + audit: ${{ steps.decide.outputs.audit }} + steps: + # Everything comes in through the environment, so the script runs + # unchanged outside Actions. In a called workflow, `github.run_id` and + # `github.event_name` are the caller's. On a pull request, `head_ref` is + # the branch its runs are listed under, where `ref_name` is `/merge`. + - name: Decide whether to audit + id: decide + shell: bash + env: + GH_TOKEN: ${{ github.token }} + REPOSITORY: ${{ github.repository }} + RUN_ID: ${{ github.run_id }} + EVENT: ${{ github.event_name }} + SHA: ${{ github.sha }} + BRANCH: ${{ github.head_ref || github.ref_name }} + run: | + set -euo pipefail + + workflow_id="$(gh api "repos/$REPOSITORY/actions/runs/$RUN_ID" --jq .workflow_id)" + + # Newest first. The current run is still in progress, so + # `status=completed` already leaves it out. Read into a variable, + # so a failed lookup fails the step. + runs="$(gh api --method GET "repos/$REPOSITORY/actions/workflows/$workflow_id/runs" \ + -f branch="$BRANCH" -f status=completed -F per_page=100 \ + --jq '.workflow_runs[] | [.id, .head_sha, .html_url] | @tsv')" + + last_sha="" + last_url="" + last_conclusion="" + while IFS=$'\t' read -r id head_sha url; do + if [ -z "$id" ] || [ "$id" = "$RUN_ID" ]; then + continue + fi + # A caller's run names the job ` / Audit`. + conclusion="$(gh api --method GET "repos/$REPOSITORY/actions/runs/$id/jobs" \ + -f filter=latest -F per_page=100 \ + --jq '[.jobs[] | select(.name == "Audit" or (.name | endswith(" / Audit"))) | .conclusion | select(. == "success" or . == "failure" or . == "timed_out")] | first // empty')" + if [ -n "$conclusion" ]; then + last_sha="$head_sha" + last_url="$url" + last_conclusion="$conclusion" + break + fi + done <<< "$runs" + + if [ "$EVENT" != "schedule" ]; then + audit=true + reason="\`$EVENT\` always audits." + elif [ -z "$last_sha" ]; then + audit=true + reason="no earlier audit on \`$BRANCH\`." + elif [ "$last_sha" != "$SHA" ]; then + audit=true + reason="\`$BRANCH\` moved from \`${last_sha:0:7}\` to \`${SHA:0:7}\` since run $last_url." + else + audit=false + reason="\`$BRANCH\` is still at \`${SHA:0:7}\`, which run $last_url audited (\`$last_conclusion\`)." + fi + + if [ "$audit" = true ]; then + decision="Ran the Lighthouse audit: $reason" + else + decision="Skipped the Lighthouse audit: $reason" + fi + if [ -n "$last_sha" ]; then + last="The last audit on \`$BRANCH\` was run $last_url, at \`${last_sha:0:7}\` (\`$last_conclusion\`)." + else + last="No earlier audit on \`$BRANCH\` in its last 100 completed runs." + fi + + echo "$decision" + echo "$last" + printf '## Lighthouse\n\n%s\n\n%s\n' "$decision" "$last" >> "$GITHUB_STEP_SUMMARY" + echo "audit=$audit" >> "$GITHUB_OUTPUT" + + audit: + name: Audit + needs: changes + if: needs.changes.outputs.audit == 'true' + runs-on: ubuntu-latest + timeout-minutes: ${{ inputs.timeout-minutes }} + permissions: + contents: read + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Setup pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + with: + version: ${{ inputs.pnpm-version }} + + # No dependency cache without an install: there is no lockfile to key it. + - name: Setup Node.js + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: ${{ inputs.node-version }} + node-version-file: ${{ inputs.node-version == '' && inputs.node-version-file || '' }} + cache: ${{ inputs.install != '' && 'pnpm' || '' }} + + # Each command comes in through the environment, not the script text, so + # the caller's input is never expanded into the script. + - name: Install dependencies + if: inputs.install != '' + env: + INSTALL: ${{ inputs.install }} + run: bash --noprofile --norc -eo pipefail -c "$INSTALL" + + - name: Build + if: inputs.build != '' + env: + BUILD: ${{ inputs.build }} + run: bash --noprofile --norc -eo pipefail -c "$BUILD" + + - name: Audit + id: audit + env: + AUDIT: ${{ inputs.audit }} + run: bash --noprofile --norc -eo pipefail -c "$AUDIT" + + # Uploads after a failed audit too, so its reports show why. + - name: Upload the Lighthouse reports + if: ${{ !cancelled() && steps.audit.conclusion != 'skipped' }} + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: lighthouse-reports + path: ${{ inputs.reports }} + include-hidden-files: true + if-no-files-found: warn + retention-days: 14 diff --git a/test/lighthouse/lighthouserc.json b/test/lighthouse/lighthouserc.json new file mode 100644 index 0000000..53f1910 --- /dev/null +++ b/test/lighthouse/lighthouserc.json @@ -0,0 +1,20 @@ +{ + "ci": { + "collect": { + "staticDistDir": "test/lighthouse/site", + "numberOfRuns": 1 + }, + "assert": { + "assertions": { + "categories:performance": ["error", { "minScore": 0.9 }], + "categories:accessibility": ["error", { "minScore": 0.9 }], + "categories:best-practices": ["error", { "minScore": 0.9 }], + "categories:seo": ["error", { "minScore": 0.9 }] + } + }, + "upload": { + "target": "filesystem", + "outputDir": ".lighthouseci" + } + } +} diff --git a/test/lighthouse/site/index.html b/test/lighthouse/site/index.html new file mode 100644 index 0000000..68c05b8 --- /dev/null +++ b/test/lighthouse/site/index.html @@ -0,0 +1,22 @@ + + + + + + + Lighthouse self-test + + + +
+

Lighthouse self-test

+

+ The shared Lighthouse workflow in uxfront-com/.github audits this page + on each pull request that changes it. +

+
+ + From 4ea730022e5f53ba08c6b477ebfc8d9d3a4ec414 Mon Sep 17 00:00:00 2001 From: Alex Grozav Date: Wed, 25 Nov 2026 23:06:37 +0200 Subject: [PATCH 2/2] docs: document the shared Lighthouse workflow Co-authored-by: multica-agent --- README.md | 85 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6adc844..15159a0 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,16 @@ GitHub applies these defaults because this repository is named `.github`. Read [ ## Provided -| File | What it does | -| ---------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| [`SECURITY.md`](SECURITY.md) | Tells people how to report a vulnerability privately, and what happens next. | -| [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md) | Contributor Covenant 3.0, with reports to `support@uxfront.com`. | -| [`CONTRIBUTING.md`](CONTRIBUTING.md) | Walks contributors from an issue to a pull request, and points to each repository's own checks. | -| [`pull_request_template.md`](pull_request_template.md) | Fills in a new pull request with what changed, how it was checked and a short checklist. | -| [`.github/ISSUE_TEMPLATE/`](.github/ISSUE_TEMPLATE/) | A bug report form, a feature request form and a link to the contributing guide. | -| [`.github/workflows/release.yml`](.github/workflows/release.yml) | Versions and publishes a repository's packages with Changesets and npm trusted publishing. Read [Release workflow](#release-workflow). | -| [`.editorconfig`](.editorconfig) | Sets the whitespace rules editors follow: tabs with no fixed width, so each reader picks their own, and two spaces for YAML. Also LF, UTF-8 and a final newline. It matches `@uxfront/oxfmt-config` 0.2 and later. EditorConfig has no `extends`, so each repository keeps an exact copy. Styleframe keeps its own until it moves to the shared style. | +| File | What it does | +| ---------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [`SECURITY.md`](SECURITY.md) | Tells people how to report a vulnerability privately, and what happens next. | +| [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md) | Contributor Covenant 3.0, with reports to `support@uxfront.com`. | +| [`CONTRIBUTING.md`](CONTRIBUTING.md) | Walks contributors from an issue to a pull request, and points to each repository's own checks. | +| [`pull_request_template.md`](pull_request_template.md) | Fills in a new pull request with what changed, how it was checked and a short checklist. | +| [`.github/ISSUE_TEMPLATE/`](.github/ISSUE_TEMPLATE/) | A bug report form, a feature request form and a link to the contributing guide. | +| [`.github/workflows/release.yml`](.github/workflows/release.yml) | Versions and publishes a repository's packages with Changesets and npm trusted publishing. Read [Release workflow](#release-workflow). | +| [`.github/workflows/lighthouse.yml`](.github/workflows/lighthouse.yml) | Runs Lighthouse on a repository's site each night, and only when `main` has moved since the last audit. Read [Lighthouse workflow](#lighthouse-workflow). | +| [`.editorconfig`](.editorconfig) | Sets the whitespace rules editors follow: tabs with no fixed width, so each reader picks their own, and two spaces for YAML. Also LF, UTF-8 and a final newline. It matches `@uxfront/oxfmt-config` 0.2 and later. EditorConfig has no `extends`, so each repository keeps an exact copy. Styleframe keeps its own until it moves to the shared style. | ## Planned @@ -69,6 +70,72 @@ Two rules keep releases working: The publish job also checks that npm is 11.5.1 or later, which trusted publishing needs. It installs nothing, so if the check fails, move the repository to a Node.js release that bundles a newer npm. +## Lighthouse workflow + +The Lighthouse workflow audits a repository's site once a night, and only when `main` has moved since the last audit. Running Lighthouse on every pull request slows each one down, while a nightly run that skips an unchanged branch costs almost nothing on a quiet day. When it audits, it installs, builds and runs the repository's own Lighthouse command, then uploads the reports as the `lighthouse-reports` artifact for 14 days. A failed audit fails the run. + +A repository calls it from its own `.github/workflows/lighthouse.yml`: + +```yaml +name: Lighthouse + +on: + schedule: + - cron: " * * *" + workflow_dispatch: + # Tests the caller itself, and each later pin bump, before it merges. + pull_request: + paths: [.github/workflows/lighthouse.yml] + +concurrency: + group: lighthouse-${{ github.ref }} + cancel-in-progress: false + +permissions: {} + +jobs: + lighthouse: + name: Lighthouse + permissions: + actions: read # the change check reads earlier runs + contents: read + uses: uxfront-com/.github/.github/workflows/lighthouse.yml@<40-char SHA> # main + with: + build: + audit: + reports: +``` + +The `pull_request` trigger only fires for a pull request that changes the caller itself, like the one that adds it or moves its pin, so each change to it runs once before it merges. No other pull request runs Lighthouse. Pin the full SHA with a `# main` comment, as for the release workflow. + +The calling job must grant `actions: read`, since the change check reads the workflow's earlier runs, and `contents: read`, since the audit checks out the code. A called workflow can only lower the permissions it gets. Keep `concurrency` in the caller too, for the same reason as the release workflow. + +| Input | Type | Default | What it does | +| ------------------- | ------ | -------------------------------- | -------------------------------------------------------------------------------------------------------------------- | +| `install` | string | `pnpm install --frozen-lockfile` | The command that installs dependencies. Empty skips the step. | +| `build` | string | `''` | The command that builds the site. Empty skips the step. | +| `audit` | string | required | The command that runs Lighthouse and exits non-zero when it fails. It runs from the repository root, so it can `cd`. | +| `reports` | string | `.lighthouseci` | The path to upload as the `lighthouse-reports` artifact. | +| `node-version` | string | `''` | The Node.js version. When set, it wins over `node-version-file`. | +| `node-version-file` | string | `.node-version` | The file to read the Node.js version from when `node-version` is empty. | +| `pnpm-version` | string | `''` | The pnpm version. Empty reads `packageManager` from `package.json`, and setting both fails the job. | +| `timeout-minutes` | number | `30` | The timeout of the `Audit` job, in minutes. Raise it for a site whose build takes longer. | + +Each command runs in Bash, from an environment variable rather than the script text, so the caller's input is never expanded into the script. + +The `Changes` job decides whether the `Audit` job runs, and writes its decision and the reason to the run's summary: + +- **A scheduled run audits only when the branch moved.** It looks back through the workflow's completed runs on the same branch, newest first, for the last one whose `Audit` job passed, failed or timed out. When the branch is still at that run's commit, it skips the audit and the run stays green. +- **Every other run audits.** A manual run from the **Actions** tab, or a pull request run, always audits. +- **A skipped or cancelled audit doesn't count**, so a cancelled audit runs again the next night. +- **A failed lookup fails the run.** When the history can't be read, like without `actions: read`, the run goes red instead of skipping. + +The check looks back at most 100 completed runs, so after 100 skipped nights in a row it audits the same commit again. GitHub can delay or drop a scheduled run when it's busy, and the next night catches up, since the check compares against the last audit rather than the last night. + +Keep in mind who hears about a failed night: GitHub sends the [notification for a scheduled run](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule) to whoever last changed the cron line in the caller. To receive them yourself, be the last one to change it. + +`lighthouse-test.yml` tests this workflow on each pull request that changes it. It calls the pull request's own copy against the fixture page in `test/lighthouse/`, which must score at least 0.9 in every category. This repository has no `package.json` or `.node-version`, so the self-test also passes the Node.js and pnpm versions and an empty `install`. + ## How defaults apply A repository's own file always wins, so a default only fills a gap: