Both deploy and cleanup previously read context.payload.pull_request.number directly, which throws on workflow_dispatch and other non-PR triggers. New optional inputs: - deploy: pr-number (default: event PR number), commit-sha (default: github.sha) - cleanup: pr-number (default: event PR number) If pr-number resolves to empty/0, the comment step is skipped gracefully instead of crashing. This lets /stageblog's companion workflow_dispatch receiver call the composite deploy action directly instead of inlining wrangler + noindex injection. The noindex logic staying in the actions repo (rather than in a script file inside the caller repo that gets checked out at the fork's SHA) is also the secure path — fork authors can't tamper with it. The slash-commands README example is updated to use the deploy action with the new inputs. The earlier commit on this branch (which rewrote the example to call wrangler directly) is effectively superseded.
99 lines
3.6 KiB
YAML
99 lines
3.6 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 -->'
|
|
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 }}
|
|
PR_NUMBER: ${{ inputs.pr-number }}
|
|
with:
|
|
github-token: ${{ inputs.github-token }}
|
|
script: |
|
|
const { owner, repo } = context.repo;
|
|
const prNumber = Number(process.env.PR_NUMBER);
|
|
if (!prNumber) {
|
|
core.info('No PR number available — skipping comment update.');
|
|
return;
|
|
}
|
|
const marker = process.env.COMMENT_MARKER;
|
|
|
|
const { data: comments } = await github.rest.issues.listComments({
|
|
owner, repo, issue_number: prNumber
|
|
});
|
|
const existing = comments.find(c =>
|
|
c.user.type === 'Bot' && c.body.includes(marker)
|
|
);
|
|
|
|
if (existing) {
|
|
await github.rest.issues.updateComment({
|
|
owner, repo, comment_id: existing.id,
|
|
body: `${marker}\n## Preview\n\n_Preview deployments for this PR have been cleaned up._`
|
|
});
|
|
}
|