mirror of
https://github.com/Cleanuparr/Cleanuparr.git
synced 2026-09-17 07:56:27 -04:00
64 lines
2.0 KiB
YAML
64 lines
2.0 KiB
YAML
name: Docs Preview Cleanup
|
|
|
|
on:
|
|
pull_request:
|
|
types: [closed]
|
|
paths:
|
|
- 'docs/**'
|
|
workflow_dispatch:
|
|
inputs:
|
|
pr_number:
|
|
description: 'PR number whose preview deployments to delete'
|
|
required: true
|
|
type: string
|
|
|
|
permissions:
|
|
contents: read
|
|
|
|
jobs:
|
|
cleanup:
|
|
runs-on: ubuntu-latest
|
|
# Skip fork PRs
|
|
if: github.event_name == 'workflow_dispatch' || github.event.pull_request.head.repo.full_name == github.repository
|
|
env:
|
|
CF_API_TOKEN: ${{ secrets.CLOUDFLARE_PAGES_TOKEN }}
|
|
CF_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
PROJECT: cleanuparr-docs
|
|
BRANCH: pull-${{ github.event.pull_request.number || inputs.pr_number }}
|
|
steps:
|
|
- name: Delete preview deployments for this PR
|
|
run: |
|
|
set -euo pipefail
|
|
api="https://api.cloudflare.com/client/v4"
|
|
auth="Authorization: Bearer $CF_API_TOKEN"
|
|
|
|
if [ -z "${CF_ACCOUNT_ID:-}" ]; then
|
|
echo "CLOUDFLARE_ACCOUNT_ID repo variable is not set" >&2
|
|
exit 1
|
|
fi
|
|
echo "Account $CF_ACCOUNT_ID, tearing down branch $BRANCH"
|
|
|
|
ids=""
|
|
page=1
|
|
while :; do
|
|
resp=$(curl -fsS -H "$auth" \
|
|
"$api/accounts/$CF_ACCOUNT_ID/pages/projects/$PROJECT/deployments?page=$page&per_page=25")
|
|
count=$(echo "$resp" | jq '.result | length')
|
|
[ "$count" -eq 0 ] && break
|
|
|
|
page_ids=$(echo "$resp" | jq -r --arg b "$BRANCH" \
|
|
'.result[] | select(.deployment_trigger.metadata.branch == $b) | .id')
|
|
ids="$ids $page_ids"
|
|
page=$((page + 1))
|
|
done
|
|
|
|
deleted=0
|
|
for id in $ids; do
|
|
echo "Deleting deployment $id"
|
|
curl -fsS -X DELETE -H "$auth" \
|
|
"$api/accounts/$CF_ACCOUNT_ID/pages/projects/$PROJECT/deployments/$id?force=true" >/dev/null
|
|
deleted=$((deleted + 1))
|
|
done
|
|
|
|
echo "Deleted $deleted deployment(s) for $BRANCH"
|