diff --git a/.github/workflows/README.md b/.github/workflows/README.md index 2cf6da91b7e..1915eeef101 100644 --- a/.github/workflows/README.md +++ b/.github/workflows/README.md @@ -61,7 +61,10 @@ Features: - Uses the same data and announcement artifacts as `deploy.yaml`, with noindex and no analytics - Compares each build with its merge base with `main`: URL list, structured data, SEO tags and internal links. See `scripts/rebrand/README.md` - A failed check marks the run red but still updates the preview -- After the build, the comparison, the designed-screens check and the accessibility check run as three jobs side by side; the designed-screens check runs four browsers at once +- The candidate, PR base and main reference build in parallel from the same pinned production artifacts, including generated Contributors content. Browser checks use the PR-base HTML diff. Changes to static files or non-bundle assets run the full browser suite. The comparison with the merge base with `main` covers the whole site. +- CSS or JS changes add the core design and axe samples to the changed-page checks. Core design covers the named screens, breakpoints, sample pages, home, floating controls at 390 and 1440 px, and every fourth article in the usual article set. +- Pushes, PRs labeled `full-checks`, PRs changing this workflow or the check scripts, and diffs over 60 pages run the full browser suite. With no relevant changes, both browser jobs report success without starting Chromium. +- Design and axe cache Chromium by Playwright version. - Delete this workflow when the redesign launches ## Data Workflows diff --git a/.github/workflows/rebrand-preview.yaml b/.github/workflows/rebrand-preview.yaml index ad6ba763559..79c0f374f63 100644 --- a/.github/workflows/rebrand-preview.yaml +++ b/.github/workflows/rebrand-preview.yaml @@ -9,9 +9,10 @@ name: Doors preview # Checks: Every run also builds the merge base with main from the same data, then compares # the two builds. # A failed check shows on the run but does not hold back the preview. -# Layout: "Build" builds the branch and shares it as an artifact. Three jobs then check it -# side by side: the comparison with main, the designed screens and the accessibility -# rules. "Check results" fails if any of them did. +# Layout: The branch and PR base build in parallel. The main comparison uses the branch inputs. +# The browser checks use changed pages, plus a core sample when CSS/JS changes. +# Pushes, the full-checks label, check infrastructure changes and large diffs use the full suite. +# "Compare with main" still checks the whole site. "Check results" aggregates failures. # Note: Uses the same data and announcement artifacts as deploy.yaml. Delete this # workflow when the redesign launches. @@ -20,6 +21,7 @@ on: branches: - rebrand-doors pull_request: + types: [opened, synchronize, reopened, labeled, unlabeled] branches: - rebrand-doors @@ -28,6 +30,7 @@ env: PYTHONUNBUFFERED: "1" # Output is flushed as it is printed, so the log's timestamps show which section is slow. HUGO_VERSION: "0.158.0" HUGO_EXTENDED: true + PLAYWRIGHT_VERSION: "1.63.0" PREVIEW_BASE: /rebrand-doors/ PREVIEW_URL: https://forrt.org/rebrand-doors/ @@ -36,8 +39,47 @@ concurrency: cancel-in-progress: true jobs: + inputs: + name: Resolve build inputs + runs-on: ubuntu-22.04 + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + permissions: + actions: read + outputs: + data: ${{ steps.inputs.outputs.data }} + announcements: ${{ steps.inputs.outputs.announcements }} + steps: + - name: Pin the production artifacts for both builds + id: inputs + shell: bash + env: + GH_TOKEN: ${{ github.token }} + run: | + for input in data announcements; do + selected='{"id":null,"run_id":null}' + case "$input" in + data) workflow=data-processing.yml ;; + announcements) workflow=create_announcements.yml ;; + esac + artifact=$(gh api --paginate "repos/$GITHUB_REPOSITORY/actions/artifacts?name=$input-artifact&per_page=100" \ + | jq -c -s '[.[].artifacts[] | select(.workflow_run.head_branch == "main" and (.expired | not))] + | sort_by(.created_at, .id) | reverse | .[] | {id, run_id: .workflow_run.id}') + while IFS= read -r candidate; do + [ -n "$candidate" ] || continue + run_id=$(jq -r '.run_id' <<< "$candidate") + run=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$run_id") + if jq -e --arg path ".github/workflows/$workflow" \ + '.conclusion == "success" and .path == $path' <<< "$run" >/dev/null; then + selected=$candidate + break + fi + done <<< "$artifact" + echo "$input=$selected" >> "$GITHUB_OUTPUT" + done + build: name: Build + needs: inputs runs-on: ubuntu-22.04 # Fork PRs get no artifacts token and have no preview to update. if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository @@ -45,123 +87,257 @@ jobs: contents: read actions: read # Needed for artifact access steps: - # Full history for Hugo's Git dates, with file contents fetched only for the commit - # checked out (as in staging-aggregate.yaml). - name: Checkout repository uses: actions/checkout@v6 with: fetch-depth: 0 filter: blob:none - # See deploy.yaml: Git quotes non-ASCII paths unless this is off, and Hugo - # then finds no date for them. - name: Let Git report non-ASCII paths unquoted run: git config core.quotepath false - # ======================= - # Artifacts, as in deploy.yaml - # ======================= - name: Download data artifact - uses: dawidd6/action-download-artifact@07ab29fd4a977ae4d2b275087cf67563dfdf0295 - continue-on-error: true + if: fromJSON(needs.inputs.outputs.data).id != null + uses: actions/download-artifact@v7 with: - workflow: data-processing.yml - name: data-artifact + artifact-ids: ${{ fromJSON(needs.inputs.outputs.data).id }} + run-id: ${{ fromJSON(needs.inputs.outputs.data).run_id }} + github-token: ${{ github.token }} path: ${{ runner.temp }}/artifacts/data - github_token: ${{ secrets.GITHUB_TOKEN }} - search_artifacts: true - branch: main # as in deploy.yaml: only main's runs, never a branch test run (#960) - if_no_artifact_found: warn - name: Download announcements artifact - uses: dawidd6/action-download-artifact@07ab29fd4a977ae4d2b275087cf67563dfdf0295 - continue-on-error: true + if: fromJSON(needs.inputs.outputs.announcements).id != null + uses: actions/download-artifact@v7 with: - workflow: create_announcements.yml - name: announcements-artifact + artifact-ids: ${{ fromJSON(needs.inputs.outputs.announcements).id }} + run-id: ${{ fromJSON(needs.inputs.outputs.announcements).run_id }} + github-token: ${{ github.token }} path: ${{ runner.temp }}/artifacts/announcements - github_token: ${{ secrets.GITHUB_TOKEN }} - search_artifacts: true - branch: main # as in deploy.yaml: only main's runs, never a branch test run (#960) - if_no_artifact_found: warn - name: Apply artifacts run: | scripts/rebrand/apply_artifacts.sh . \ "$RUNNER_TEMP/artifacts/data" "$RUNNER_TEMP/artifacts/announcements" - # The data artifact's Contributors page comes from main's generator, which writes the old - # markup until the redesign launches. Regenerate it with this branch's generator, from the - # contributor cache the same artifact carries (no credentials needed). Only the branch - # build does this; the reference keeps main's page. The generator also reads two published - # Sheets (column names and extra roles); if they cannot be reached, the build goes ahead - # with the artifact's page and a warning. - - name: Rebuild the Contributors page with this branch's generator - run: | - pip install --quiet numpy==1.24.4 pandas==2.0.3 python-dotenv # pinned as in requirements.txt - if python3 scripts/forrt_contribs/tenzing.py; then - mv scripts/forrt_contribs/tenzing.md content/contributors/tenzing.md - else - echo "::warning::Could not regenerate the Contributors page; the preview shows the data artifact's copy." - fi + - name: Setup Hugo + uses: peaceiris/actions-hugo@75d2e84710de30f6ff7268e08f310b60ef14033f + with: + hugo-version: ${{ env.HUGO_VERSION }} + extended: ${{ env.HUGO_EXTENDED }} - # "Compare with main" builds the reference from these same files, not from whatever - # data run is newest by the time it starts. - - name: Pack the artifacts for the reference build + # Keep reusable entries for later builds. Each job restores its own cache snapshot. + - name: Cache Hugo resources + uses: actions/cache@v5 + with: + path: | + ./resources/_gen + ${{ runner.temp }}/hugo-cache + key: hugo-${{ runner.os }}-${{ runner.arch }}-${{ env.HUGO_VERSION }}-head-${{ github.sha }} + restore-keys: | + hugo-${{ runner.os }}-${{ runner.arch }}-${{ env.HUGO_VERSION }}-head- + + # HUGO_ENV=staging adds noindex and leaves out Google Analytics. + - name: Build the branch + run: hugo --cacheDir "$RUNNER_TEMP/hugo-cache" --minify --cleanDestinationDir --destination public --baseURL "$PREVIEW_URL" + env: + HUGO_ENV: staging + + - name: Point root-relative links at the subfolder + run: python3 scripts/rebrand/preview_paths.py public --base "$PREVIEW_BASE" + + # One file uploads and downloads far faster than the site's thousands. + - name: Pack the site + env: + DATA_INPUT: ${{ needs.inputs.outputs.data }} + ANNOUNCEMENTS_INPUT: ${{ needs.inputs.outputs.announcements }} run: | - mkdir -p "$RUNNER_TEMP/artifacts/data" "$RUNNER_TEMP/artifacts/announcements" - tar -cf inputs.tar -C "$RUNNER_TEMP" artifacts + tar -cf site.tar -C public . + jq -n --argjson data "$DATA_INPUT" --argjson announcements "$ANNOUNCEMENTS_INPUT" \ + '{data: $data, announcements: $announcements, contributors: "artifact"}' > site-inputs.json - - name: Upload the artifacts for the reference build + - name: Upload site artifact uses: actions/upload-artifact@v7 with: - name: rebrand-inputs-${{ github.run_number }} - path: inputs.tar + name: rebrand-preview-${{ github.run_number }} + path: site.tar + retention-days: 1 + compression-level: 1 + overwrite: true # A rerun reuses the run number. + if-no-files-found: error + + - name: Upload site input manifest + if: github.event_name == 'push' + uses: actions/upload-artifact@v7 + with: + name: rebrand-site-inputs-${{ github.run_number }} + path: site-inputs.json retention-days: 1 overwrite: true if-no-files-found: error + reference: + name: Build PR base + needs: inputs + runs-on: ubuntu-22.04 + if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && !contains(github.event.pull_request.labels.*.name, 'full-checks') + permissions: + contents: read + actions: read + outputs: + scoped: ${{ steps.scope.outputs.scoped }} + steps: + - name: Checkout repository + uses: actions/checkout@v6 + with: + fetch-depth: 0 + filter: blob:none + ref: ${{ github.event.pull_request.base.sha }} + + - name: Let Git report non-ASCII paths unquoted + run: git config core.quotepath false + + - name: Select full or scoped checks + id: scope + env: + HEAD_SHA: ${{ github.sha }} + run: | + git fetch --no-tags --filter=blob:none origin "$HEAD_SHA" + if ! git diff --quiet HEAD "$HEAD_SHA" -- .github/workflows/rebrand-preview.yaml \ + ':(glob)scripts/rebrand/check_*.py' scripts/rebrand/changed_pages.py scripts/rebrand/sample_pages.json \ + static assets content ':(exclude,glob)assets/**/*.css' ':(exclude,glob)assets/**/*.scss' \ + ':(exclude,glob)assets/**/*.js' ':(exclude,glob)content/**/*.md' ':(exclude,glob)content/**/*.markdown'; then + echo "scoped=false" >> "$GITHUB_OUTPUT" + else + echo "scoped=true" >> "$GITHUB_OUTPUT" + fi + + - name: Reuse a matching push build + if: steps.scope.outputs.scoped == 'true' + id: reuse + shell: bash + env: + GH_TOKEN: ${{ github.token }} + BASE_SHA: ${{ github.event.pull_request.base.sha }} + BASE_BRANCH: ${{ github.event.pull_request.base.ref }} + DATA_INPUT: ${{ needs.inputs.outputs.data }} + ANNOUNCEMENTS_INPUT: ${{ needs.inputs.outputs.announcements }} + run: | + # Require the exact base commit and the same immutable input artifacts. + if ! run=$(gh run list --repo "$GITHUB_REPOSITORY" --workflow rebrand-preview.yaml \ + --branch "$BASE_BRANCH" --event push --status success --commit "$BASE_SHA" \ + --limit 1 --json databaseId,number --jq '.[0] | select(. != null) | [.databaseId, .number] | @tsv'); then + echo "::warning::Could not look up the base push build" + exit 0 + fi + if [ -z "$run" ]; then + echo "No successful push build for the PR base" + exit 0 + fi + read -r run_id run_number <<< "$run" + if ! gh run download "$run_id" --repo "$GITHUB_REPOSITORY" \ + --name "rebrand-site-inputs-$run_number" --dir "$RUNNER_TEMP/base-build"; then + echo "Base push input manifest unavailable" + exit 0 + fi + if ! jq -e --argjson data "$DATA_INPUT" --argjson announcements "$ANNOUNCEMENTS_INPUT" \ + '.data == $data and .announcements == $announcements and .contributors == "artifact"' \ + "$RUNNER_TEMP/base-build/site-inputs.json" >/dev/null; then + echo "Base push artifact has different or unrecorded inputs" + exit 0 + fi + if ! gh run download "$run_id" --repo "$GITHUB_REPOSITORY" \ + --name "rebrand-preview-$run_number" --dir "$RUNNER_TEMP/base-build"; then + echo "Base push site artifact unavailable" + exit 0 + fi + mkdir -p "$RUNNER_TEMP/base-build/public" + if ! tar -xf "$RUNNER_TEMP/base-build/site.tar" -C "$RUNNER_TEMP/base-build/public" \ + --wildcards '*.html' '*.css' '*.js' || [ ! -f "$RUNNER_TEMP/base-build/public/index.html" ]; then + echo "Base push site archive is incomplete" + exit 0 + fi + mv "$RUNNER_TEMP/base-build/public" public + echo "Reused base push build from run $run_id" + echo "reused=true" >> "$GITHUB_OUTPUT" + + - name: Download data artifact + if: steps.scope.outputs.scoped == 'true' && steps.reuse.outputs.reused != 'true' && fromJSON(needs.inputs.outputs.data).id != null + uses: actions/download-artifact@v7 + with: + artifact-ids: ${{ fromJSON(needs.inputs.outputs.data).id }} + run-id: ${{ fromJSON(needs.inputs.outputs.data).run_id }} + github-token: ${{ github.token }} + path: ${{ runner.temp }}/artifacts/data + + - name: Download announcements artifact + if: steps.scope.outputs.scoped == 'true' && steps.reuse.outputs.reused != 'true' && fromJSON(needs.inputs.outputs.announcements).id != null + uses: actions/download-artifact@v7 + with: + artifact-ids: ${{ fromJSON(needs.inputs.outputs.announcements).id }} + run-id: ${{ fromJSON(needs.inputs.outputs.announcements).run_id }} + github-token: ${{ github.token }} + path: ${{ runner.temp }}/artifacts/announcements + + - name: Apply artifacts + if: steps.scope.outputs.scoped == 'true' && steps.reuse.outputs.reused != 'true' + run: | + scripts/rebrand/apply_artifacts.sh . \ + "$RUNNER_TEMP/artifacts/data" "$RUNNER_TEMP/artifacts/announcements" + - name: Setup Hugo + if: steps.scope.outputs.scoped == 'true' && steps.reuse.outputs.reused != 'true' uses: peaceiris/actions-hugo@75d2e84710de30f6ff7268e08f310b60ef14033f with: hugo-version: ${{ env.HUGO_VERSION }} extended: ${{ env.HUGO_EXTENDED }} + - name: Cache Hugo resources + if: steps.scope.outputs.scoped == 'true' && steps.reuse.outputs.reused != 'true' + uses: actions/cache@v5 + with: + path: | + ./resources/_gen + ${{ runner.temp }}/hugo-cache + key: hugo-${{ runner.os }}-${{ runner.arch }}-${{ env.HUGO_VERSION }}-base-${{ github.event.pull_request.base.sha }} + restore-keys: | + hugo-${{ runner.os }}-${{ runner.arch }}-${{ env.HUGO_VERSION }}-base- + hugo-${{ runner.os }}-${{ runner.arch }}-${{ env.HUGO_VERSION }}-head-${{ github.event.pull_request.base.sha }} + hugo-${{ runner.os }}-${{ runner.arch }}-${{ env.HUGO_VERSION }}-head- + # HUGO_ENV=staging adds noindex and leaves out Google Analytics. - - name: Build the branch - run: hugo --gc --minify --cleanDestinationDir --destination public --baseURL "$PREVIEW_URL" + - name: Build the PR base + if: steps.scope.outputs.scoped == 'true' && steps.reuse.outputs.reused != 'true' + run: hugo --cacheDir "$RUNNER_TEMP/hugo-cache" --minify --destination public --baseURL "$PREVIEW_URL" env: HUGO_ENV: staging - name: Point root-relative links at the subfolder + if: steps.scope.outputs.scoped == 'true' && steps.reuse.outputs.reused != 'true' run: python3 scripts/rebrand/preview_paths.py public --base "$PREVIEW_BASE" - # One file uploads and downloads far faster than the site's thousands. - - name: Pack the site - run: tar -cf site.tar -C public . + - name: Pack pages and bundles + if: steps.scope.outputs.scoped == 'true' + working-directory: public + run: find . -type f \( -name '*.html' -o -name '*.css' -o -name '*.js' \) -print0 | tar --null -T - -cf ../site.tar - name: Upload site artifact + if: steps.scope.outputs.scoped == 'true' uses: actions/upload-artifact@v7 with: - name: rebrand-preview-${{ github.run_number }} + name: rebrand-pr-base-${{ github.run_number }} path: site.tar retention-days: 1 compression-level: 1 overwrite: true # A rerun reuses the run number. if-no-files-found: error - compare: - name: Compare with main + main-reference: + name: Build main reference runs-on: ubuntu-22.04 - needs: build + needs: inputs permissions: contents: read - actions: read # Needed for artifact access - outputs: - source-rules: ${{ steps.source-rules.outcome }} - compare: ${{ steps.compare.outcome }} - links: ${{ steps.links.outcome }} - seo: ${{ steps.seo.outcome }} + actions: read steps: - name: Checkout repository uses: actions/checkout@v6 @@ -177,15 +353,26 @@ jobs: - name: Check out the merge base with main as the reference run: git worktree add --detach "$RUNNER_TEMP/reference" "$(git merge-base origin/main HEAD)" - - name: Download the artifacts the branch was built with + - name: Download data artifact + if: fromJSON(needs.inputs.outputs.data).id != null + uses: actions/download-artifact@v7 + with: + artifact-ids: ${{ fromJSON(needs.inputs.outputs.data).id }} + run-id: ${{ fromJSON(needs.inputs.outputs.data).run_id }} + github-token: ${{ github.token }} + path: ${{ runner.temp }}/artifacts/data + + - name: Download announcements artifact + if: fromJSON(needs.inputs.outputs.announcements).id != null uses: actions/download-artifact@v7 with: - name: rebrand-inputs-${{ github.run_number }} - path: ${{ runner.temp }} + artifact-ids: ${{ fromJSON(needs.inputs.outputs.announcements).id }} + run-id: ${{ fromJSON(needs.inputs.outputs.announcements).run_id }} + github-token: ${{ github.token }} + path: ${{ runner.temp }}/artifacts/announcements - name: Apply the same artifacts to the reference run: | - tar -xf "$RUNNER_TEMP/inputs.tar" -C "$RUNNER_TEMP" scripts/rebrand/apply_artifacts.sh "$RUNNER_TEMP/reference" \ "$RUNNER_TEMP/artifacts/data" "$RUNNER_TEMP/artifacts/announcements" @@ -195,14 +382,66 @@ jobs: hugo-version: ${{ env.HUGO_VERSION }} extended: ${{ env.HUGO_EXTENDED }} + - name: Cache Hugo resources + uses: actions/cache@v5 + with: + path: | + ${{ runner.temp }}/reference/resources/_gen + ${{ runner.temp }}/hugo-cache + key: hugo-${{ runner.os }}-${{ runner.arch }}-${{ env.HUGO_VERSION }}-main-${{ github.sha }} + restore-keys: | + hugo-${{ runner.os }}-${{ runner.arch }}-${{ env.HUGO_VERSION }}-main- + - name: Build the reference from the same data working-directory: ${{ runner.temp }}/reference run: | - hugo --gc --minify --destination "$RUNNER_TEMP/public-reference" --baseURL "$PREVIEW_URL" + hugo --cacheDir "$RUNNER_TEMP/hugo-cache" --minify --destination "$RUNNER_TEMP/public-reference" --baseURL "$PREVIEW_URL" python3 "$GITHUB_WORKSPACE/scripts/rebrand/preview_paths.py" "$RUNNER_TEMP/public-reference" --base "$PREVIEW_BASE" env: HUGO_ENV: staging + - name: Pack the reference site + run: tar -cf reference.tar -C "$RUNNER_TEMP/public-reference" . + + - name: Upload the reference site + uses: actions/upload-artifact@v7 + with: + name: rebrand-main-reference-${{ github.run_number }} + path: reference.tar + retention-days: 1 + compression-level: 1 + overwrite: true + if-no-files-found: error + + compare: + name: Compare with main + runs-on: ubuntu-22.04 + needs: [build, main-reference] + permissions: + contents: read + outputs: + source-rules: ${{ steps.source-rules.outcome }} + compare: ${{ steps.compare.outcome }} + links: ${{ steps.links.outcome }} + seo: ${{ steps.seo.outcome }} + steps: + - name: Checkout repository + uses: actions/checkout@v6 + with: + fetch-depth: 0 + filter: blob:none + + - name: Let Git report non-ASCII paths unquoted + run: git config core.quotepath false + + - name: Download the reference site + uses: actions/download-artifact@v7 + with: + name: rebrand-main-reference-${{ github.run_number }} + + - name: Unpack the reference site + run: mkdir "$RUNNER_TEMP/public-reference" && tar -xf reference.tar -C "$RUNNER_TEMP/public-reference" + - name: Download the branch's build uses: actions/download-artifact@v7 with: @@ -236,7 +475,9 @@ jobs: design: name: Designed screens runs-on: ubuntu-22.04 - needs: build + needs: [build, reference] + # The reference build is intentionally skipped in full mode. + if: ${{ !cancelled() && needs.build.result == 'success' && (needs.reference.result == 'success' || needs.reference.result == 'skipped') }} steps: - name: Checkout repository uses: actions/checkout@v6 @@ -246,12 +487,41 @@ jobs: with: name: rebrand-preview-${{ github.run_number }} - - name: Unpack the site + - uses: actions/download-artifact@v7 + if: needs.reference.outputs.scoped == 'true' + with: + name: rebrand-pr-base-${{ github.run_number }} + path: base + - name: Compare pages and bundles + id: selection + env: + SCOPED: ${{ needs.reference.outputs.scoped }} + run: | + if [ "$SCOPED" = true ]; then + tar -xf base/site.tar -C base + mkdir public + tar -xf site.tar -C public + python3 scripts/rebrand/changed_pages.py base public --out changed.json | tee selection.log + else + python3 scripts/rebrand/changed_pages.py --full --out changed.json | tee selection.log + fi + sed -n 's/^Check mode: /mode=/p' selection.log >> "$GITHUB_OUTPUT" + shell: bash + - name: Unpack the site for full checks + if: steps.selection.outputs.mode == 'full' && needs.reference.outputs.scoped != 'true' run: mkdir public && tar -xf site.tar -C public + - name: Cache Chromium + if: steps.selection.outputs.mode != 'none' + uses: actions/cache@v5 + with: + path: ~/.cache/ms-playwright + key: playwright-${{ runner.os }}-${{ runner.arch }}-${{ env.PLAYWRIGHT_VERSION }}-chromium + - name: Install Playwright + if: steps.selection.outputs.mode != 'none' run: | - pip install --quiet playwright==1.63.0 pillow + pip install --quiet "playwright==$PLAYWRIGHT_VERSION" pillow # The runner image already has Chromium's libraries; --with-deps adds fonts from the # Ubuntu mirror. When the mirror is unreachable, install the browser without them. playwright install --with-deps chromium || { @@ -261,12 +531,15 @@ jobs: # Runs its sections in four browsers side by side; each section's time is in the log. - name: Designed screens in a browser (overflow, position, layout shift, pause, directory) - run: python3 scripts/rebrand/check_design.py public --base "$PREVIEW_BASE" --jobs 4 + if: steps.selection.outputs.mode != 'none' + run: python3 scripts/rebrand/check_design.py public --pages changed.json --base "$PREVIEW_BASE" --jobs 4 axe: name: Accessibility rules runs-on: ubuntu-22.04 - needs: build + needs: [build, reference] + # The reference build is intentionally skipped in full mode. + if: ${{ !cancelled() && needs.build.result == 'success' && (needs.reference.result == 'success' || needs.reference.result == 'skipped') }} steps: - name: Checkout repository uses: actions/checkout@v6 @@ -276,21 +549,51 @@ jobs: with: name: rebrand-preview-${{ github.run_number }} - - name: Unpack the site + - uses: actions/download-artifact@v7 + if: needs.reference.outputs.scoped == 'true' + with: + name: rebrand-pr-base-${{ github.run_number }} + path: base + - name: Compare pages and bundles + id: selection + env: + SCOPED: ${{ needs.reference.outputs.scoped }} + run: | + if [ "$SCOPED" = true ]; then + tar -xf base/site.tar -C base + mkdir public + tar -xf site.tar -C public + python3 scripts/rebrand/changed_pages.py base public --out changed.json | tee selection.log + else + python3 scripts/rebrand/changed_pages.py --full --out changed.json | tee selection.log + fi + sed -n 's/^Check mode: /mode=/p' selection.log >> "$GITHUB_OUTPUT" + shell: bash + - name: Unpack the site for full checks + if: steps.selection.outputs.mode == 'full' && needs.reference.outputs.scoped != 'true' run: mkdir public && tar -xf site.tar -C public + - name: Cache Chromium + if: steps.selection.outputs.mode != 'none' + uses: actions/cache@v5 + with: + path: ~/.cache/ms-playwright + key: playwright-${{ runner.os }}-${{ runner.arch }}-${{ env.PLAYWRIGHT_VERSION }}-chromium + - name: Install Playwright + if: steps.selection.outputs.mode != 'none' run: | - pip install --quiet playwright==1.63.0 + pip install --quiet "playwright==$PLAYWRIGHT_VERSION" playwright install --with-deps chromium || { echo "::warning::Could not install Chromium's system packages; installing the browser without them" playwright install chromium } - name: Accessibility rules in a browser (axe-core, by WCAG level) + if: steps.selection.outputs.mode != 'none' run: | curl -fsSL https://cdn.jsdelivr.net/npm/axe-core@4.11.1/axe.min.js -o "$RUNNER_TEMP/axe.min.js" - python3 scripts/rebrand/check_axe.py public --base "$PREVIEW_BASE" --axe "$RUNNER_TEMP/axe.min.js" + python3 scripts/rebrand/check_axe.py public --pages changed.json --base "$PREVIEW_BASE" --axe "$RUNNER_TEMP/axe.min.js" deploy: name: Deploy preview @@ -322,13 +625,15 @@ jobs: checks: name: Check results runs-on: ubuntu-22.04 - needs: [build, compare, design, axe] + needs: [build, reference, compare, design, axe] # Report even when a check job failed; skip only when the build was skipped (fork PRs). - if: always() && needs.build.result != 'skipped' + if: ${{ !cancelled() && needs.build.result != 'skipped' }} steps: - name: Fail if any check failed env: BUILD: ${{ needs.build.result }} + REFERENCE: ${{ needs.reference.result }} + COMPARE_JOB: ${{ needs.compare.result }} SOURCE_RULES: ${{ needs.compare.outputs.source-rules }} COMPARE: ${{ needs.compare.outputs.compare }} LINKS: ${{ needs.compare.outputs.links }} @@ -343,6 +648,7 @@ jobs: echo "Built HTML: $SEO" echo "Designed screens: $DESIGN" echo "Accessibility rules: $AXE" - for result in "$BUILD" "$SOURCE_RULES" "$COMPARE" "$LINKS" "$SEO" "$DESIGN" "$AXE"; do + [ "$REFERENCE" = success ] || [ "$REFERENCE" = skipped ] || exit 1 + for result in "$BUILD" "$COMPARE_JOB" "$SOURCE_RULES" "$COMPARE" "$LINKS" "$SEO" "$DESIGN" "$AXE"; do [ "$result" = "success" ] || exit 1 done diff --git a/scripts/rebrand/changed_pages.py b/scripts/rebrand/changed_pages.py new file mode 100644 index 00000000000..5700dc14600 --- /dev/null +++ b/scripts/rebrand/changed_pages.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +"""Compare built pages and CSS/JS, and select the browser check mode.""" + +import argparse +import hashlib +import json +import re +from pathlib import Path + +FINGERPRINT = re.compile(r"\.(?:[0-9a-f]{128}|[0-9a-f]{96}|[0-9a-f]{64}|[0-9a-f]{32})(?=\.(?:css|js)\b)") +INTEGRITY = re.compile(r"""\s+integrity=(?:"[^"]*"|'[^']*'|[^\s>]+)""") + + +def normalize(html): + def clean_tag(match): + return INTEGRITY.sub("", match[0]) + + return re.sub(r"<(?:link|script)\b[^>]*>", clean_tag, FINGERPRINT.sub("", html)) + + +def page_path(path): + path = "/" + path.lstrip("/") + return path[:-len("index.html")] if path.endswith("/index.html") else path + + +def inventory(root): + pages, bundles = {}, {} + for path in sorted(root.rglob("*")): + suffix = path.suffix + if suffix not in {".html", ".css", ".js"} or not path.is_file(): + continue + relative = path.relative_to(root).as_posix() + if suffix == ".html": + pages[page_path(relative)] = hashlib.sha256(normalize(path.read_text(encoding="utf-8")).encode()).digest() + else: + bundles.setdefault(FINGERPRINT.sub("", relative), set()).add(hashlib.sha256(path.read_bytes()).digest()) + return pages, bundles + + +def compare(base, head): + before, old_bundles = inventory(base) + after, new_bundles = inventory(head) + return { + "changed": sorted(path for path in before.keys() & after.keys() if before[path] != after[path]), + "added": sorted(after.keys() - before.keys()), + "removed": sorted(before.keys() - after.keys()), + "bundles_changed": old_bundles != new_bundles, + } + + +def load_selection(path): + if path is None: + print("Check mode: full (all pages)") + return "full", set() + diff = json.loads(Path(path).read_text(encoding="utf-8")) + live = set(diff["changed"]) | set(diff["added"]) + total = len(live) + len(diff["removed"]) + if diff.get("force_full") or total > 60: + mode = "full" + elif diff["bundles_changed"]: + mode = "core" + elif live: + mode = "changed" + else: + mode = "none" + print(f"Check mode: {mode}") + for field in ("changed", "added", "removed"): + print(f"{field.capitalize()} pages: {', '.join(diff[field]) or '(none)'}") + if mode == "none": + print("no changed pages") + return mode, live + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("base", type=Path, nargs="?") + parser.add_argument("head", type=Path, nargs="?") + parser.add_argument("--out", type=Path, required=True) + parser.add_argument("--full", action="store_true") + args = parser.parse_args() + if args.full: + diff = {"changed": [], "added": [], "removed": [], "bundles_changed": False, "force_full": True} + else: + if not all(path and (path / "index.html").is_file() for path in (args.base, args.head)): + parser.error("base and head must each contain a built index.html") + diff = compare(args.base, args.head) + args.out.write_text(json.dumps(diff, indent=2) + "\n", encoding="utf-8") + load_selection(args.out) + + +if __name__ == "__main__": + main() diff --git a/scripts/rebrand/check_axe.py b/scripts/rebrand/check_axe.py index 4aec0be65d0..ff7f5629231 100644 --- a/scripts/rebrand/check_axe.py +++ b/scripts/rebrand/check_axe.py @@ -21,6 +21,8 @@ sys.path.insert(0, HERE) from check_design import serve # noqa: E402 +from changed_pages import load_selection # noqa: E402 + LEVEL_A = {"wcag2a", "wcag21a", "wcag22a"} LEVEL_AA = {"wcag2aa", "wcag21aa", "wcag22aa"} EXTRA = { @@ -57,14 +59,26 @@ def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("public", nargs="?") parser.add_argument("--url") + parser.add_argument("--pages", help="JSON from changed_pages.py") parser.add_argument("--base", default="/") parser.add_argument("--axe", required=True, help="path to axe.min.js") parser.add_argument("--out", help="write every finding to this JSON file") args = parser.parse_args() + mode, pages = load_selection(args.pages) + if mode == "none": + if args.out: + with open(args.out, "w", encoding="utf-8") as handle: + json.dump([], handle) + return base = args.url.rstrip("/") + "/" if args.url else serve(os.path.abspath(args.public), args.base) with open(os.path.join(HERE, "sample_pages.json"), encoding="utf-8") as handle: sample = {**json.load(handle), **EXTRA} sample.pop("404", None) + if mode == "changed": + names = {path: name for name, path in sample.items()} + sample = {names.get(path, path): path for path in sorted(pages)} + elif mode == "core": + sample.update({path: path for path in sorted(pages - set(sample.values()))}) with open(args.axe, encoding="utf-8") as handle: axe = handle.read() findings, failures, practice = [], [], collections.Counter() diff --git a/scripts/rebrand/check_design.py b/scripts/rebrand/check_design.py index 5a62a9324a9..d0ac3f54649 100644 --- a/scripts/rebrand/check_design.py +++ b/scripts/rebrand/check_design.py @@ -28,11 +28,14 @@ import tempfile import threading import time +from pathlib import Path from urllib.parse import urlsplit from playwright.sync_api import Error as PlaywrightError, sync_playwright from playwright.sync_api import TimeoutError as PlaywrightTimeoutError +from changed_pages import load_selection + WIDTHS = {390: 844, 1024: 768, 1440: 900} SCREENS = { "home": "", @@ -178,8 +181,10 @@ def new_page(browser, width, errors=None, **options): return page -def check_screens(browser, base, failures): +def check_screens(browser, base, failures, selected=None): for name, path in SCREENS.items(): + if selected is not None and "/" + path not in selected: + continue for width in WIDTHS: errors = [] page = new_page(browser, width, errors) @@ -201,7 +206,7 @@ def check_screens(browser, base, failures): page.context.close() -def article_paths(root): +def article_paths(root, selected=None): """Pages drawn on the reading layout, from the build.""" paths = [] for directory, _, files in os.walk(root): @@ -209,6 +214,8 @@ def article_paths(root): with open(os.path.join(directory, "index.html"), encoding="utf-8", errors="replace") as handle: if re.search(r'class="?article-layout', handle.read()): paths.append(os.path.relpath(directory, root).replace(os.sep, "/") + "/") + if selected is not None: + return sorted(path for path in paths if "/" + path in selected) # The two generated collections have over a thousand pages from one template, so take a sample. chosen = [] seen = {} @@ -243,7 +250,7 @@ def check_articles(browser, base, paths, failures, widths=WIDTHS): page.context.close() -def check_callouts(browser, base, root, failures): +def check_callouts(browser, base, root, failures, selected=None): """Call-outs sit in the right-hand column from 1240px, clear of each other, and in the text below that.""" paths = [] for directory, _, files in os.walk(root): @@ -251,6 +258,10 @@ def check_callouts(browser, base, root, failures): with open(os.path.join(directory, "index.html"), encoding="utf-8", errors="replace") as handle: if re.search(r'