-
-
Notifications
You must be signed in to change notification settings - Fork 115
50 lines (42 loc) · 1.59 KB
/
Copy pathpr-preview-cleanup.yml
File metadata and controls
50 lines (42 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
name: Cleanup PR Preview
on:
pull_request_target:
types: [closed]
permissions:
contents: read
env:
CLOUDFLARE_PAGES_PROJECT_NAME: typelevel-website
BRANCH_NAME: pr-${{ github.event.pull_request.number }}
jobs:
cleanup-preview:
name: Delete Cloudflare Pages preview deployments
runs-on: ubuntu-latest
steps:
- name: Delete deployments for this PR's branch
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
set -euo pipefail
API="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects/$CLOUDFLARE_PAGES_PROJECT_NAME/deployments"
AUTH_HEADER="Authorization: Bearer $CLOUDFLARE_API_TOKEN"
page=1
ids=()
while :; do
response=$(curl -sf -H "$AUTH_HEADER" "$API?page=$page&per_page=25")
count=$(echo "$response" | jq '.result | length')
[ "$count" -eq 0 ] && break
while IFS= read -r id; do
ids+=("$id")
done < <(echo "$response" | jq -r --arg branch "$BRANCH_NAME" \
'.result[] | select(.deployment_trigger.metadata.branch == $branch) | .id')
page=$((page + 1))
done
if [ ${#ids[@]} -eq 0 ]; then
echo "No preview deployments found for branch $BRANCH_NAME"
exit 0
fi
for id in "${ids[@]}"; do
echo "Deleting deployment $id"
curl -sf -X DELETE -H "$AUTH_HEADER" "$API/$id?force=true"
done