From 5373485eec179e64173f022c977c63f61ab27bbd Mon Sep 17 00:00:00 2001 From: Tim 'mithro' Ansell Date: Sat, 24 Oct 2026 17:58:40 +1030 Subject: [PATCH 1/3] Generate the preview index and redirects.json in one script generate-preview-root.py replaces generate-preview-index.sh. Besides index.html, it now writes redirects.json at the preview root, for the Cloudflare redirects sync Worker: every pr-NNN preview's redirects.json moved under its directory, with 302s, in the form Cloudflare's replace-list-items call takes, so the Worker can load it as is. Its first item records the revision: preview.wafer.space/revision redirects to the site commit that wrote it, and its comment is git describe for the preview repository commit that holds it, so the numbers only go up. It builds both files and only then writes them, so a failure leaves the previous pair in place. The workflows run it from the site checkout, with the template next to it, instead of copying the script and the template into the preview repository, and pass the site commit in PREVIEW_SOURCE_COMMIT. Against the current preview repository, index.html differs from the shell script's only where that showed a plain pr-NNN directory as "PR #NNN: pr NNN": it replaced the dashes before checking for a title, so the check never matched. Those now read "PR #NNN Preview", as intended. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018aHXTpnku3VjT9FFpoBEuy --- .github/scripts/generate-preview-index.sh | 94 ------------ .github/scripts/generate-preview-root.py | 165 ++++++++++++++++++++++ .github/workflows/cleanup-previews.yml | 11 +- .github/workflows/pr-preview.yml | 21 +-- 4 files changed, 175 insertions(+), 116 deletions(-) delete mode 100755 .github/scripts/generate-preview-index.sh create mode 100755 .github/scripts/generate-preview-root.py diff --git a/.github/scripts/generate-preview-index.sh b/.github/scripts/generate-preview-index.sh deleted file mode 100755 index bbc3cf84..00000000 --- a/.github/scripts/generate-preview-index.sh +++ /dev/null @@ -1,94 +0,0 @@ -#!/bin/bash -set -e # Exit on any error - -# Generate the preview index page -TEMPLATE_FILE=".github/templates/preview-index.html" -OUTPUT_FILE="index.html" - -# Validate template file exists -if [ ! -f "$TEMPLATE_FILE" ]; then - echo "ERROR: Template file $TEMPLATE_FILE not found!" - echo "This indicates a problem with the workflow setup." - exit 1 -fi - -# Start with the template -if ! cp "$TEMPLATE_FILE" "$OUTPUT_FILE"; then - echo "ERROR: Failed to copy template file to $OUTPUT_FILE" - exit 1 -fi - -# Generate preview items HTML -PREVIEW_ITEMS="" -FOUND_PREVIEWS=false -PREVIEW_COUNT=0 - -for dir in pr-*/; do - if [ -d "$dir" ] && [ "$dir" != "pr-preview/" ]; then - # Validate directory contains expected files - if [ ! -f "$dir/index.html" ]; then - echo "Warning: $dir appears to be incomplete (no index.html), skipping..." - continue - fi - - # Extract PR number from directory name (handles both old pr-123 and new pr-123-title formats) - pr_num=$(basename "$dir" | sed 's/pr-\([0-9][0-9]*\).*/\1/') - # Validate PR number is numeric - if ! [[ "$pr_num" =~ ^[0-9]+$ ]]; then - echo "Warning: $dir has invalid PR number format, skipping..." - continue - fi - - # Extract title part if it exists (for display purposes) - title_part=$(basename "$dir" | sed 's/pr-[0-9][0-9]*-\(.*\)/\1/' | sed 's/-/ /g') - if [ "$title_part" = "$(basename "$dir")" ]; then - # No title part found, use empty string - title_part="" - fi - - # Additional validation: PR number must be reasonable range - if [ "$pr_num" -lt 1 ] || [ "$pr_num" -gt 99999 ]; then - echo "Warning: $dir has PR number out of valid range, skipping..." - continue - fi - - # HTML escape the directory name to prevent injection - dir_escaped=$(echo "$dir" | sed 's/&/\&/g; s//\>/g; s/"/\"/g; s/'"'"'/\'/g') - pr_num_escaped=$(echo "$pr_num" | sed 's/&/\&/g; s//\>/g; s/"/\"/g; s/'"'"'/\'/g') - title_part_escaped=$(echo "$title_part" | sed 's/&/\&/g; s//\>/g; s/"/\"/g; s/'"'"'/\'/g') - - # Create display title - if [ -n "$title_part" ]; then - display_title="PR #$pr_num_escaped: $title_part_escaped" - else - display_title="PR #$pr_num_escaped Preview" - fi - - PREVIEW_ITEMS+="
  • " - PREVIEW_ITEMS+="$display_title" - PREVIEW_ITEMS+="
    https://preview.wafer.space/$dir_escaped
    " - PREVIEW_ITEMS+="
  • " - FOUND_PREVIEWS=true - PREVIEW_COUNT=$((PREVIEW_COUNT + 1)) - fi -done - -# If no previews found, show empty state -if [ "$FOUND_PREVIEWS" = false ]; then - PREVIEW_ITEMS="" -fi - -# Replace placeholder with actual items -if ! sed -i "s||$PREVIEW_ITEMS|g" "$OUTPUT_FILE"; then - echo "ERROR: Failed to replace placeholder in template" - echo "Check template file format and placeholder existence" - exit 1 -fi - -# Validate output file was created successfully -if [ ! -f "$OUTPUT_FILE" ] || [ ! -s "$OUTPUT_FILE" ]; then - echo "ERROR: Output file $OUTPUT_FILE was not created or is empty" - exit 1 -fi - -echo "Generated preview index with $PREVIEW_COUNT active previews" \ No newline at end of file diff --git a/.github/scripts/generate-preview-root.py b/.github/scripts/generate-preview-root.py new file mode 100755 index 00000000..e1891ed8 --- /dev/null +++ b/.github/scripts/generate-preview-root.py @@ -0,0 +1,165 @@ +#!/usr/bin/env python3 +"""Generate the root of preview.wafer.space: index.html and redirects.json. + +Run from the root of the preview.wafer.space repository, just before the +commit that will hold the result (pr-preview.yml and cleanup-previews.yml run +it after adding or removing previews). Both files are built first and only +then written, so a failure leaves the previous pair in place. + +index.html lists every pr-NNN preview (and its pr-NNN-title alias), from +.github/templates/preview-index.html next to this script. + +redirects.json is the body of Cloudflare's "replace list items" call for the +preview_wafer_space Bulk Redirect list, which the redirects sync Worker +(https://github.com/wafer-space/cloudflare-redirects-sync) loads as is. Each +pr-NNN preview's redirects.json (written by _plugins/redirect_list.rb for the +production hostname) is moved under the preview: + +- source wafer.space/ becomes preview.wafer.space/pr-NNN/; +- a target on https://wafer.space follows it under + https://preview.wafer.space/pr-NNN/, and any other target is left as is; +- the status becomes 302, because browsers cache a 301 indefinitely and a + preview's redirects change with every push. + +Its first item records the revision, as the Worker requires: +preview.wafer.space/revision redirects to PREVIEW_SOURCE_COMMIT (the site +commit whose deploy or cleanup wrote this), and its comment is the git +describe number of the preview repository commit that will hold the file +(HEAD's plus one), so the numbers only go up as the preview repository's +history grows, including when a deploy merges in another one's push. +""" + +import html +import json +import os +import re +import subprocess +import sys +from pathlib import Path + +TEMPLATE = Path(__file__).resolve().parent.parent / "templates" / "preview-index.html" +PLACEHOLDER = "" +EMPTY_STATE = "" + +PRODUCTION_HOST = "wafer.space" +PREVIEW_HOST = "preview.wafer.space" +COMMIT_URL = "https://github.com/wafer-space/wafer-space.github.io/commit/" +# pr-123, or its pr-123-some-title alias +LISTED_DIR = re.compile(r"^pr-([0-9]+)(?:-(.*))?$") +# Only the pr-123 directories hold a preview's own redirects.json +PREVIEW_DIR = re.compile(r"^pr-[0-9]+$") +TARGET = re.compile(r"^https://wafer\.space(?=$|[/?#])/?(.*)$", re.S) +# Cloudflare's Free plan allows 10,000 redirects across all lists +WARN_TOTAL = 9000 + + +def index_html(directories): + """The preview index page, and how many previews it lists.""" + items = [] + for directory in directories: + match = LISTED_DIR.match(directory.name) + if not match: + continue + if not (directory / "index.html").is_file(): + print(f"Warning: {directory.name}/ appears to be incomplete (no index.html), skipping...") + continue + number, title = int(match.group(1)), (match.group(2) or "").replace("-", " ") + if not 1 <= number <= 99999: + print(f"Warning: {directory.name}/ has PR number out of valid range, skipping...") + continue + name = html.escape(f"{directory.name}/") + label = f"PR #{number}: {html.escape(title)}" if title else f"PR #{number} Preview" + items.append(f'
  • {label}' + f'
    https://{PREVIEW_HOST}/{name}
  • ') + template = TEMPLATE.read_text() + if PLACEHOLDER not in template: + raise ValueError(f"{TEMPLATE} has no {PLACEHOLDER}") + return template.replace(PLACEHOLDER, "".join(items) or EMPTY_STATE), len(items) + + +def next_revision(): + """git describe for the commit that will hold the file, without its hash.""" + describe = subprocess.run( + ["git", "describe", "--tags", "--long", "--match", "v[0-9]*", "HEAD"], + check=True, capture_output=True, text=True, + ).stdout.strip() + match = re.fullmatch(r"(.+)-([0-9]+)-g[0-9a-f]+", describe) + if not match: + raise ValueError(f"unexpected git describe output {describe!r}") + return f"{match.group(1)}-{int(match.group(2)) + 1}" + + +def preview_items(directory): + """The preview's redirects, moved under it, or [] with a warning.""" + path = directory / "redirects.json" + try: + data = json.loads(path.read_text()) + except (OSError, ValueError) as err: + print(f"Warning: skipping {path}: {err}") + return [] + if not isinstance(data, list): + print(f"Warning: skipping {path}: not a JSON array") + return [] + items = [] + for item in data: + redirect = item.get("redirect") if isinstance(item, dict) else None + source = redirect.get("source_url") if isinstance(redirect, dict) else None + target = redirect.get("target_url") if isinstance(redirect, dict) else None + if not isinstance(source, str) or not source.startswith(f"{PRODUCTION_HOST}/") or not isinstance(target, str): + print(f"Warning: skipping an entry of {path}: {json.dumps(item)[:200]}") + continue + match = TARGET.match(target) + items.append({ + **({"comment": item["comment"]} if "comment" in item else {}), + "redirect": { + **redirect, + "source_url": f"{PREVIEW_HOST}/{directory.name}/{source[len(PRODUCTION_HOST) + 1:]}", + "target_url": f"https://{PREVIEW_HOST}/{directory.name}/{match.group(1)}" if match else target, + "status_code": 302, + }, + }) + return items + + +def redirects_json(directories, commit): + """The preview list, starting with its revision item, and how many previews it covers.""" + items = [] + previews = 0 + for directory in directories: + if PREVIEW_DIR.match(directory.name) and (directory / "redirects.json").is_file(): + found = preview_items(directory) + previews += bool(found) + items += found + items.sort(key=lambda item: item["redirect"]["source_url"]) + revision = next_revision() + items.insert(0, { + "comment": f"{revision} {commit[:7]}", + "redirect": { + "source_url": f"{PREVIEW_HOST}/revision", + "target_url": f"{COMMIT_URL}{commit}", + "status_code": 302, + }, + }) + return items, previews, revision + + +def main(): + commit = os.environ.get("PREVIEW_SOURCE_COMMIT", "") + if not re.fullmatch(r"[0-9a-f]{40}", commit): + print("ERROR: PREVIEW_SOURCE_COMMIT must be the full hash of the site commit being deployed") + return 1 + directories = sorted(d for d in Path(".").iterdir() if d.is_dir() and d.name.startswith("pr-")) + page, listed = index_html(directories) + items, previews, revision = redirects_json(directories, commit) + + Path("index.html").write_text(page) + Path("redirects.json").write_text(json.dumps(items, indent=2) + "\n") + print(f"Generated preview index with {listed} active previews") + print(f"Wrote redirects.json at {revision}: {len(items)} entries from {previews} preview(s)") + if len(items) > WARN_TOTAL: + print(f"::warning::The preview redirect list has {len(items)} entries; Cloudflare allows 10,000 across all lists") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/workflows/cleanup-previews.yml b/.github/workflows/cleanup-previews.yml index a7130a17..083e6bf8 100644 --- a/.github/workflows/cleanup-previews.yml +++ b/.github/workflows/cleanup-previews.yml @@ -137,6 +137,9 @@ jobs: - name: Perform cleanup of closed PR previews if: steps.find-cleanup.outputs.cleanup_needed == 'true' + env: + # Recorded in the preview redirects.json's revision item + PREVIEW_SOURCE_COMMIT: ${{ github.sha }} run: | cd preview-repo @@ -170,12 +173,8 @@ jobs: find . -maxdepth 1 -type d -name "pr-${PR_NUMBER}-*" -exec rm -rf {} + || true done - # Copy script and template, then regenerate index - cp ../.github/scripts/generate-preview-index.sh ./ - mkdir -p .github/templates - cp ../.github/templates/preview-index.html .github/templates/ - chmod +x generate-preview-index.sh - ./generate-preview-index.sh + # Regenerate the preview index and redirects.json + python3 ../.github/scripts/generate-preview-root.py # Commit changes if any git add . diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 3dbdc37b..a111cf06 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -142,24 +142,13 @@ jobs: echo "preview.wafer.space" > CNAME fi - - name: Create index page for preview listing + - name: Generate the preview index and redirects.json + env: + # Recorded in the preview redirects.json's revision item + PREVIEW_SOURCE_COMMIT: ${{ github.event.pull_request.head.sha }} run: | cd preview-repo - # Copy script and template from temp location, then generate preview index - if [ ! -f "/tmp/pr-github-${{ steps.pr-directory.outputs.directory_name }}/scripts/generate-preview-index.sh" ]; then - echo "ERROR: Preview generation script not found in temp location" - exit 1 - fi - if [ ! -f "/tmp/pr-github-${{ steps.pr-directory.outputs.directory_name }}/templates/preview-index.html" ]; then - echo "ERROR: Preview template not found in temp location" - exit 1 - fi - - cp "/tmp/pr-github-${{ steps.pr-directory.outputs.directory_name }}/scripts/generate-preview-index.sh" ./ - mkdir -p .github/templates - cp "/tmp/pr-github-${{ steps.pr-directory.outputs.directory_name }}/templates/preview-index.html" .github/templates/ - chmod +x generate-preview-index.sh - ./generate-preview-index.sh + python3 "/tmp/pr-github-${{ steps.pr-directory.outputs.directory_name }}/scripts/generate-preview-root.py" - name: Deploy to preview repository run: | From 516298d4b04756a8253b931271c08bd2325a7bbd Mon Sep 17 00:00:00 2001 From: Tim 'mithro' Ansell Date: Sat, 24 Oct 2026 18:49:01 +1030 Subject: [PATCH 2/3] Check each preview's redirects before they reach the preview list The preview list is loaded into Cloudflare as one replace call, so a single bad entry from one pull request's redirects.json could stop every preview's redirects loading. Entries now keep only the fields this sets (source, target, status and the preserve flags), need an absolute http(s) target, and a source already seen in that preview is skipped. The preview's own wafer.space/revision item, which only describes that preview, is left out, and a comment is kept only if it's a string. Both files now cover the same previews: a pr-NNN directory without an index.html, or with a number out of range, no longer adds redirects. They're listed in PR number order rather than as strings, and an empty index shows its "No active PR previews" message by un-hiding it, as the inserted script ran before the element it looked for existed. A failing git describe now prints git's error, and the docstring says when the revision numbers only go up. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018aHXTpnku3VjT9FFpoBEuy --- .github/scripts/generate-preview-root.py | 122 +++++++++++++++-------- 1 file changed, 80 insertions(+), 42 deletions(-) diff --git a/.github/scripts/generate-preview-root.py b/.github/scripts/generate-preview-root.py index e1891ed8..a41393bf 100755 --- a/.github/scripts/generate-preview-root.py +++ b/.github/scripts/generate-preview-root.py @@ -3,30 +3,42 @@ Run from the root of the preview.wafer.space repository, just before the commit that will hold the result (pr-preview.yml and cleanup-previews.yml run -it after adding or removing previews). Both files are built first and only -then written, so a failure leaves the previous pair in place. +it after adding or removing previews, and pr-preview.yml again after merging +in another deploy's push). Both files are built first and only then written, +so a failure leaves the previous pair in place. The workflows run the base +branch's copy of this script and its template, not a pull request's. -index.html lists every pr-NNN preview (and its pr-NNN-title alias), from -.github/templates/preview-index.html next to this script. +A preview is a pr-NNN directory with an index.html, or its pr-NNN-title +alias; both files only cover those. + +index.html lists every preview, from .github/templates/preview-index.html +next to this script. redirects.json is the body of Cloudflare's "replace list items" call for the preview_wafer_space Bulk Redirect list, which the redirects sync Worker -(https://github.com/wafer-space/cloudflare-redirects-sync) loads as is. Each -pr-NNN preview's redirects.json (written by _plugins/redirect_list.rb for the -production hostname) is moved under the preview: +(https://github.com/wafer-space/cloudflare-redirects-sync) loads as is, so a +bad entry would stop the whole list loading. Each pr-NNN preview's +redirects.json (written by _plugins/redirect_list.rb for the production +hostname) is moved under the preview, keeping only the fields this sets: -- source wafer.space/ becomes preview.wafer.space/pr-NNN/; +- source wafer.space/ becomes preview.wafer.space/pr-NNN/, and + a source already seen in that preview is skipped; - a target on https://wafer.space follows it under - https://preview.wafer.space/pr-NNN/, and any other target is left as is; + https://preview.wafer.space/pr-NNN/, any other absolute http(s) target is + left as is, and an entry with any other target is skipped; - the status becomes 302, because browsers cache a 301 indefinitely and a - preview's redirects change with every push. + preview's redirects change with every push; +- the preview's own revision item (wafer.space/revision) is left out. Its first item records the revision, as the Worker requires: preview.wafer.space/revision redirects to PREVIEW_SOURCE_COMMIT (the site commit whose deploy or cleanup wrote this), and its comment is the git describe number of the preview repository commit that will hold the file -(HEAD's plus one), so the numbers only go up as the preview repository's -history grows, including when a deploy merges in another one's push. +(HEAD's plus one). Because each run commits straight after this, including +after merging in another deploy's push, the numbers only go up. They count +commits since the v0.0 tag, so the preview repository's history must not be +rewritten and no other v* tag added, or the numbers would drop and the +Worker would stop loading the list until it is forced to. """ import html @@ -39,23 +51,24 @@ TEMPLATE = Path(__file__).resolve().parent.parent / "templates" / "preview-index.html" PLACEHOLDER = "" -EMPTY_STATE = "" +HIDDEN_EMPTY_STATE = 'id="empty-state" style="display: none;"' PRODUCTION_HOST = "wafer.space" PREVIEW_HOST = "preview.wafer.space" COMMIT_URL = "https://github.com/wafer-space/wafer-space.github.io/commit/" # pr-123, or its pr-123-some-title alias LISTED_DIR = re.compile(r"^pr-([0-9]+)(?:-(.*))?$") -# Only the pr-123 directories hold a preview's own redirects.json -PREVIEW_DIR = re.compile(r"^pr-[0-9]+$") TARGET = re.compile(r"^https://wafer\.space(?=$|[/?#])/?(.*)$", re.S) +ABSOLUTE = re.compile(r"^https?://[^/?#\s]+", re.I) +# The fields of a redirect this writes, besides source_url and target_url +KEPT_FIELDS = ("preserve_query_string", "preserve_path_suffix") # Cloudflare's Free plan allows 10,000 redirects across all lists WARN_TOTAL = 9000 -def index_html(directories): - """The preview index page, and how many previews it lists.""" - items = [] +def previews(directories): + """(directory, PR number, title) for each preview, in PR number order.""" + found = [] for directory in directories: match = LISTED_DIR.match(directory.name) if not match: @@ -67,22 +80,37 @@ def index_html(directories): if not 1 <= number <= 99999: print(f"Warning: {directory.name}/ has PR number out of valid range, skipping...") continue + found.append((directory, number, title)) + return sorted(found, key=lambda preview: (preview[1], preview[0].name)) + + +def index_html(found): + """The preview index page.""" + items = [] + for directory, number, title in found: name = html.escape(f"{directory.name}/") label = f"PR #{number}: {html.escape(title)}" if title else f"PR #{number} Preview" items.append(f'
  • {label}' f'
    https://{PREVIEW_HOST}/{name}
  • ') - template = TEMPLATE.read_text() - if PLACEHOLDER not in template: - raise ValueError(f"{TEMPLATE} has no {PLACEHOLDER}") - return template.replace(PLACEHOLDER, "".join(items) or EMPTY_STATE), len(items) + page = TEMPLATE.read_text() + for marker in (PLACEHOLDER, HIDDEN_EMPTY_STATE): + if marker not in page: + raise ValueError(f"{TEMPLATE} has no {marker}") + if not items: + page = page.replace(HIDDEN_EMPTY_STATE, 'id="empty-state"') + return page.replace(PLACEHOLDER, "".join(items)) def next_revision(): """git describe for the commit that will hold the file, without its hash.""" - describe = subprocess.run( - ["git", "describe", "--tags", "--long", "--match", "v[0-9]*", "HEAD"], - check=True, capture_output=True, text=True, - ).stdout.strip() + try: + describe = subprocess.run( + ["git", "describe", "--tags", "--long", "--match", "v[0-9]*", "HEAD"], + check=True, capture_output=True, text=True, + ).stdout.strip() + except subprocess.CalledProcessError as err: + print(f"ERROR: git describe failed: {err.stderr.strip()}") + raise match = re.fullmatch(r"(.+)-([0-9]+)-g[0-9a-f]+", describe) if not match: raise ValueError(f"unexpected git describe output {describe!r}") @@ -101,35 +129,45 @@ def preview_items(directory): print(f"Warning: skipping {path}: not a JSON array") return [] items = [] + seen = set() for item in data: redirect = item.get("redirect") if isinstance(item, dict) else None source = redirect.get("source_url") if isinstance(redirect, dict) else None target = redirect.get("target_url") if isinstance(redirect, dict) else None - if not isinstance(source, str) or not source.startswith(f"{PRODUCTION_HOST}/") or not isinstance(target, str): + if (not isinstance(source, str) or not source.startswith(f"{PRODUCTION_HOST}/") + or not isinstance(target, str) or not ABSOLUTE.match(target)): print(f"Warning: skipping an entry of {path}: {json.dumps(item)[:200]}") continue + if source == f"{PRODUCTION_HOST}/revision": + continue + if source in seen: + print(f"Warning: skipping a second entry for {source} in {path}") + continue + seen.add(source) match = TARGET.match(target) + comment = item.get("comment") items.append({ - **({"comment": item["comment"]} if "comment" in item else {}), + **({"comment": comment} if isinstance(comment, str) else {}), "redirect": { - **redirect, "source_url": f"{PREVIEW_HOST}/{directory.name}/{source[len(PRODUCTION_HOST) + 1:]}", "target_url": f"https://{PREVIEW_HOST}/{directory.name}/{match.group(1)}" if match else target, "status_code": 302, + **{key: redirect[key] for key in KEPT_FIELDS if isinstance(redirect.get(key), bool)}, }, }) return items -def redirects_json(directories, commit): +def redirects_json(found, commit): """The preview list, starting with its revision item, and how many previews it covers.""" items = [] - previews = 0 - for directory in directories: - if PREVIEW_DIR.match(directory.name) and (directory / "redirects.json").is_file(): - found = preview_items(directory) - previews += bool(found) - items += found + covered = 0 + for directory, _, title in found: + # The pr-NNN-title aliases only redirect to their pr-NNN directory + if not title and (directory / "redirects.json").is_file(): + moved = preview_items(directory) + covered += bool(moved) + items += moved items.sort(key=lambda item: item["redirect"]["source_url"]) revision = next_revision() items.insert(0, { @@ -140,7 +178,7 @@ def redirects_json(directories, commit): "status_code": 302, }, }) - return items, previews, revision + return items, covered, revision def main(): @@ -148,14 +186,14 @@ def main(): if not re.fullmatch(r"[0-9a-f]{40}", commit): print("ERROR: PREVIEW_SOURCE_COMMIT must be the full hash of the site commit being deployed") return 1 - directories = sorted(d for d in Path(".").iterdir() if d.is_dir() and d.name.startswith("pr-")) - page, listed = index_html(directories) - items, previews, revision = redirects_json(directories, commit) + found = previews(d for d in Path(".").iterdir() if d.is_dir()) + page = index_html(found) + items, covered, revision = redirects_json(found, commit) Path("index.html").write_text(page) Path("redirects.json").write_text(json.dumps(items, indent=2) + "\n") - print(f"Generated preview index with {listed} active previews") - print(f"Wrote redirects.json at {revision}: {len(items)} entries from {previews} preview(s)") + print(f"Generated preview index with {len(found)} active previews") + print(f"Wrote redirects.json at {revision}: {len(items)} entries from {covered} preview(s)") if len(items) > WARN_TOTAL: print(f"::warning::The preview redirect list has {len(items)} entries; Cloudflare allows 10,000 across all lists") return 0 From dd4db082ba1cb72d074a67701514f73b3f9961c7 Mon Sep 17 00:00:00 2001 From: Tim 'mithro' Ansell Date: Sat, 24 Oct 2026 18:49:52 +1030 Subject: [PATCH 3/3] Run the base branch's preview generator, and remove the old one The generator writes the preview site's shared index.html and redirects.json, and the latter now feeds a Cloudflare list, where a revision number that jumps ahead would stop later lists loading. Preview deploys now run the base branch's copy of the script and its template rather than the pull request's, falling back to the pull request's only while the base branch doesn't have the script yet. The shell script and template copy that deploys used to commit to the preview repository root are deleted on the next deploy or cleanup. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018aHXTpnku3VjT9FFpoBEuy --- .github/workflows/cleanup-previews.yml | 4 +++- .github/workflows/pr-preview.yml | 23 +++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/.github/workflows/cleanup-previews.yml b/.github/workflows/cleanup-previews.yml index 083e6bf8..7c1ce39d 100644 --- a/.github/workflows/cleanup-previews.yml +++ b/.github/workflows/cleanup-previews.yml @@ -173,7 +173,9 @@ jobs: find . -maxdepth 1 -type d -name "pr-${PR_NUMBER}-*" -exec rm -rf {} + || true done - # Regenerate the preview index and redirects.json + # Regenerate the preview index and redirects.json, removing what + # the shell script this replaced left in the preview repository + rm -rf generate-preview-index.sh .github/templates python3 ../.github/scripts/generate-preview-root.py # Commit changes if any diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index a111cf06..c724a27e 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -94,7 +94,24 @@ jobs: # Copy built site and workflow files to temp location before switching branches cp -r _site /tmp/pr-site-${{ steps.pr-directory.outputs.directory_name }} cp -r .github /tmp/pr-github-${{ steps.pr-directory.outputs.directory_name }} - + + - name: Get the preview index generator from the base branch + run: | + # The generator writes the preview site's shared index.html and + # redirects.json (loaded into Cloudflare), so run the base branch's + # copy, not this pull request's + TOOLS="/tmp/pr-tools-${{ steps.pr-directory.outputs.directory_name }}" + mkdir -p "$TOOLS/scripts" "$TOOLS/templates" + git fetch --no-tags --depth=1 origin "${{ github.event.pull_request.base.sha }}" + if git cat-file -e FETCH_HEAD:.github/scripts/generate-preview-root.py; then + git show FETCH_HEAD:.github/scripts/generate-preview-root.py > "$TOOLS/scripts/generate-preview-root.py" + git show FETCH_HEAD:.github/templates/preview-index.html > "$TOOLS/templates/preview-index.html" + else + echo "The base branch has no generate-preview-root.py yet; using this pull request's" + cp .github/scripts/generate-preview-root.py "$TOOLS/scripts/" + cp .github/templates/preview-index.html "$TOOLS/templates/" + fi + - name: Setup SSH and clone preview repository run: | # Setup SSH for deploying to dedicated preview repository (memory-only) @@ -148,7 +165,9 @@ jobs: PREVIEW_SOURCE_COMMIT: ${{ github.event.pull_request.head.sha }} run: | cd preview-repo - python3 "/tmp/pr-github-${{ steps.pr-directory.outputs.directory_name }}/scripts/generate-preview-root.py" + # Left in the preview repository by the shell script this replaced + rm -rf generate-preview-index.sh .github/templates + python3 "/tmp/pr-tools-${{ steps.pr-directory.outputs.directory_name }}/scripts/generate-preview-root.py" - name: Deploy to preview repository run: |