actions/cloudflare-pages-preview/cleanup/action.yml
Den Delimarsky e6b64f4192
cloudflare-pages-preview: add comment-mode=repost, extract scripts
New comment-mode input:
- 'sticky' (default, backwards-compat): update the existing marker comment
  in place — same behavior as before
- 'repost': minimize all prior marker comments as OUTDATED (GitHub's native
  hide-comment UI via GraphQL minimizeComment mutation) and post a fresh
  comment at the bottom of the conversation

Useful for /stageblog where each staging should produce a visible new
comment while keeping history collapsible rather than lost.

Both deploy and cleanup accept the input and must match. cleanup in repost
mode minimizes all prior comments and posts a final cleanup note.

Also:
- Extract both comment scripts to sibling comment.js files (same pattern as
  slash-commands/main.js) — the inline YAML scripts were getting long
- Paginate listComments (was only fetching the first 30)
2026-03-11 23:24:51 +00:00

88 lines
3.3 KiB
YAML

name: Cloudflare Pages PR Preview — Cleanup
description: >-
Deletes all Cloudflare Pages deployments for a PR's branch alias and updates
the sticky PR comment to indicate the preview has been cleaned up. Intended
to run on pull_request.closed.
inputs:
project-name:
description: Cloudflare Pages project name
required: true
api-token:
description: Cloudflare API token with Pages:Edit permission on the project
required: true
account-id:
description: Cloudflare account ID
required: true
branch:
description: Cloudflare Pages branch alias whose deployments should be deleted
required: false
default: pr-${{ github.event.pull_request.number }}
comment-marker:
description: HTML comment marker used to locate the sticky PR comment
required: false
default: '<!-- cf-pages-preview -->'
comment-mode:
description: >-
Must match the deploy action's comment-mode. 'sticky' (default) updates
the single marker comment in place. 'repost' minimizes all marker
comments as OUTDATED and posts a final cleanup comment.
required: false
default: sticky
github-token:
description: GitHub token for updating the PR comment
required: false
default: ${{ github.token }}
pr-number:
description: >-
PR number to update the sticky comment on. Only needed on triggers
where context.payload.pull_request is unavailable (e.g. workflow_dispatch).
Defaults to the event payload's PR number.
required: false
default: ${{ github.event.pull_request.number }}
runs:
using: composite
steps:
- name: Delete preview deployments
shell: bash
env:
CF_API_TOKEN: ${{ inputs.api-token }}
CF_ACCOUNT_ID: ${{ inputs.account-id }}
CF_PROJECT: ${{ inputs.project-name }}
PR_BRANCH: ${{ inputs.branch }}
run: |
set -euo pipefail
api="https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/pages/projects/${CF_PROJECT}/deployments"
echo "Listing deployments for branch alias: ${PR_BRANCH}"
# Cloudflare paginates at 25/page. One PR rarely has >25 deploys, but
# loop until the branch alias returns empty just in case of a long-
# running PR with many pushes.
while :; do
ids=$(curl -sS -H "Authorization: Bearer ${CF_API_TOKEN}" "${api}?per_page=25" \
| jq -r --arg b "$PR_BRANCH" '.result[] | select(.deployment_trigger.metadata.branch == $b) | .id')
if [ -z "$ids" ]; then
echo "No deployments remaining for ${PR_BRANCH}."
break
fi
for id in $ids; do
echo "Deleting deployment ${id}"
curl -sS -X DELETE -H "Authorization: Bearer ${CF_API_TOKEN}" "${api}/${id}?force=true" \
| jq -r '" → success=\(.success) errors=\(.errors // [])"'
done
done
- name: Update PR comment
uses: actions/github-script@v8
env:
COMMENT_MARKER: ${{ inputs.comment-marker }}
COMMENT_MODE: ${{ inputs.comment-mode }}
PR_NUMBER: ${{ inputs.pr-number }}
with:
github-token: ${{ inputs.github-token }}
script: |
const main = require(process.env.GITHUB_ACTION_PATH + '/comment.js');
await main({ github, context, core });