From ca81d44b824130b047b5fa68d193bbf4f7ee4f9b Mon Sep 17 00:00:00 2001 From: Simone Bortolin Date: Sat, 14 Nov 2026 18:01:41 +0100 Subject: [PATCH 1/2] Clean up old Cloudflare Pages previews and keep built site longer - Add preview-cleanup workflow: deletes every Cloudflare Pages deployment except the newest of each open PR (on PR close, after each preview deploy, weekly, or manually with dry-run) - Publish each PR preview on its own pr- branch, so deployments can be matched to PRs and the alias URL always shows the latest preview - Keep the built site for 30 days instead of 1: fork PR previews can wait longer than a day for approval, which made the deploy fail - Fail with a clear message when the built site has expired Co-Authored-By: Claude Opus 5.5 --- .github/workflows/build.yml | 3 + .github/workflows/preview-cleanup.yaml | 96 ++++++++++++++++++++++++++ .github/workflows/preview-deploy.yaml | 21 ++++-- 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/preview-cleanup.yaml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bf835bb9..0a6bdd57 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,3 +40,6 @@ jobs: uses: actions/upload-pages-artifact@v3 with: path: .vitepress/dist + # The default is 1 day: a fork PR preview may wait longer than that + # for a maintainer to approve the deploy + retention-days: 30 diff --git a/.github/workflows/preview-cleanup.yaml b/.github/workflows/preview-cleanup.yaml new file mode 100644 index 00000000..21940acc --- /dev/null +++ b/.github/workflows/preview-cleanup.yaml @@ -0,0 +1,96 @@ +# Delete the old PR previews from Cloudflare Pages. Each PR is published on +# its own branch (pr-, see preview-deploy.yaml): only the newest +# deployment of each open PR is kept, everything else is deleted (older +# deployments of open PRs, closed PRs, previews of the old shared "preview" +# branch). It never runs the PR code, so pull_request_target is safe here. +name: preview-cleanup + +on: + # A PR is closed or merged: its preview is no longer needed + pull_request_target: + types: [closed] + # A new preview is published: the previous ones of the same PR are obsolete + workflow_run: + workflows: [preview-deploy] + types: [completed] + # Catches up anything the events above missed + schedule: + - cron: "17 3 * * 0" + workflow_dispatch: + inputs: + dry-run: + description: Only list the deployments that would be deleted + type: boolean + default: false + +permissions: + contents: read + +# Never run two cleanups at the same time, but don't drop the queued one +concurrency: + group: preview-cleanup + cancel-in-progress: false + +jobs: + cleanup: + runs-on: ubuntu-latest + permissions: + pull-requests: read + env: + GH_TOKEN: ${{ github.token }} + CF_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + CF_ACCOUNT: ${{ secrets.CLOUDFLARE_ACCOUNT }} + PROJECT: hack-gpon-preview + DRY_RUN: ${{ inputs.dry-run && 'true' || 'false' }} + steps: + - name: Delete old deployments + run: | + set -euo pipefail + api="https://api.cloudflare.com/client/v4/accounts/$CF_ACCOUNT/pages/projects/$PROJECT" + cf() { curl -sS --fail-with-body -H "Authorization: Bearer $CF_TOKEN" "$@"; } + + # The deployment served on the project's main URL can't be deleted + canonical=$(cf "$api" | jq -r '.result.canonical_deployment.id // empty') + open_prs=$(gh pr list --repo "$GITHUB_REPOSITORY" --state open --limit 1000 \ + --json number --jq '[.[].number]') + echo "Open PRs: $open_prs" + + # Collect every deployment first: deleting while paging shifts the pages + : > deployments.jsonl + page=1 + while :; do + resp=$(cf "$api/deployments?per_page=25&page=$page") + jq -c '.result[]' <<<"$resp" >> deployments.jsonl + total=$(jq -r '.result_info.total_pages // 1' <<<"$resp") + (( page >= total )) && break + page=$((page + 1)) + done + echo "Found $(wc -l < deployments.jsonl) deployments" + + # Newest first: keep the first deployment seen for each open PR + jq -rs --arg canonical "$canonical" --argjson open "$open_prs" ' + sort_by(.created_on) | reverse + | reduce .[] as $d ({seen: {}, delete: []}; + ($d.deployment_trigger.metadata.branch // "-") as $branch + | ([$branch | capture("^pr-(?[0-9]+)$") | .n | tonumber][0]) as $pr + | if $d.id == $canonical then . + elif $pr != null and any($open[]; . == $pr) and (.seen[$branch] | not) + then .seen[$branch] = true + else .delete += ["\($d.id) \($branch) \($d.created_on)"] + end) + | .delete[]' deployments.jsonl > to-delete.txt + + echo "Deleting $(wc -l < to-delete.txt) deployments" + failed=0 + while read -r id branch created; do + echo "- $id ($branch, $created)" + [ "$DRY_RUN" = "true" ] && continue + # force: also delete deployments that still have a branch alias + cf -X DELETE "$api/deployments/$id?force=true" > /dev/null \ + || { echo "::warning::Failed to delete deployment $id"; failed=$((failed + 1)); } + done < to-delete.txt + + if (( failed > 0 )); then + echo "::error::$failed deployments could not be deleted" + exit 1 + fi diff --git a/.github/workflows/preview-deploy.yaml b/.github/workflows/preview-deploy.yaml index e783aa4f..d4dbe7b8 100644 --- a/.github/workflows/preview-deploy.yaml +++ b/.github/workflows/preview-deploy.yaml @@ -107,20 +107,33 @@ jobs: [[ "$sha" == "$HEAD_SHA" ]] || { echo "PR #$number is not at $HEAD_SHA"; exit 1; } echo "number=$number" >> "$GITHUB_OUTPUT" - name: Extract site - run: mkdir dist && tar -xf github-pages/artifact.tar -C dist + run: | + if [ ! -f github-pages/artifact.tar ]; then + echo "::error::The built site has expired, re-run the preview-pr workflow of the PR" + exit 1 + fi + mkdir dist && tar -xf github-pages/artifact.tar -C dist + # One branch per PR: its alias URL always shows the latest preview, and + # preview-cleanup.yaml can tell which deployments belong to which PR - name: Publish to Cloudflare Pages id: preview-pages uses: cloudflare/wrangler-action@v3 with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT }} - command: pages deploy dist --project-name=hack-gpon-preview --branch=preview + command: >- + pages deploy dist --project-name=hack-gpon-preview + --branch=pr-${{ steps.pr.outputs.number }} --commit-hash=${{ env.HEAD_SHA }} - name: Comment on the PR env: URL: ${{ steps.preview-pages.outputs.deployment-url }} + ALIAS_URL: ${{ steps.preview-pages.outputs.deployment-alias-url }} run: | - gh pr comment "${{ steps.pr.outputs.number }}" --repo "$GITHUB_REPOSITORY" \ - --body "Preview of the website obtained from the PR: $URL" + body="Preview of the website obtained from the PR at $HEAD_SHA: $URL" + if [ -n "$ALIAS_URL" ]; then + body+=$'\n\n'"Always the latest preview of this PR: $ALIAS_URL" + fi + gh pr comment "${{ steps.pr.outputs.number }}" --repo "$GITHUB_REPOSITORY" --body "$body" - name: Report deploy status if: always() env: From 2cfbf38e89536c7761e20083c1a66f06ca74af80 Mon Sep 17 00:00:00 2001 From: Simone Bortolin Date: Sat, 14 Nov 2026 18:02:50 +0100 Subject: [PATCH 2/2] Fix the Telegram notification workflow - Remove the "if" lines passed as action inputs (the action warned about them and they did nothing: the step-level "if" already filters) - Pin appleboy/telegram-action to v1.1.2 instead of master - Don't fail the run when the Telegram API resets the connection - Give the job a read-nothing token Co-Authored-By: Claude Opus 5.5 --- .github/workflows/notify-telegram-pr.yml | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/.github/workflows/notify-telegram-pr.yml b/.github/workflows/notify-telegram-pr.yml index 28a0637a..3d494826 100644 --- a/.github/workflows/notify-telegram-pr.yml +++ b/.github/workflows/notify-telegram-pr.yml @@ -5,28 +5,33 @@ on: types: [opened, reopened] issues: types: [opened, reopened] + +permissions: {} + jobs: build: name: Notify Telegram runs-on: ubuntu-latest steps: + # The Telegram API sometimes resets the connection from the GitHub + # runners: a missed notification must not mark the PR or issue as failed - name: send telegram message on PR opened if: github.event.pull_request - uses: appleboy/telegram-action@master + continue-on-error: true + uses: appleboy/telegram-action@v1.1.2 with: to: ${{ secrets.TELEGRAM_TO }} token: ${{ secrets.TELEGRAM_TOKEN }} - if: github.event.issue message: | ${{ github.actor }} created PR ${{ github.event.pull_request.title }} See changes: ${{ github.event.pull_request.html_url }} - name: send telegram message on issue opened if: github.event.issue - uses: appleboy/telegram-action@master + continue-on-error: true + uses: appleboy/telegram-action@v1.1.2 with: to: ${{ secrets.TELEGRAM_TO }} token: ${{ secrets.TELEGRAM_TOKEN }} - if: github.event.issue message: | ${{ github.actor }} created issue ${{ github.event.issue.title }} See issue: ${{ github.event.issue.html_url }}