@@ -4,7 +4,7 @@ run-name: Preview by @${{ github.actor }}
44
55on :
66 pull_request :
7- types : [opened, synchronize, reopened]
7+ types : [opened, synchronize, reopened, closed ]
88 workflow_dispatch :
99
1010concurrency :
@@ -17,14 +17,72 @@ permissions:
1717 actions : read
1818
1919jobs :
20+ cleanup-preview-branches :
21+ # Run only when PR is closed or merged
22+ if : github.event.action == 'closed'
23+ permissions :
24+ contents : write
25+ runs-on : ubuntu-latest
26+
27+ steps :
28+ - name : Checkout repository
29+ uses : actions/checkout@v5
30+ with :
31+ fetch-depth : 0
32+
33+ - name : Delete preview branches for this PR
34+ run : |
35+ set -euo pipefail
36+
37+ PR_STATE="${{ github.event.pull_request.merged && 'merged' || 'closed' }}"
38+ echo "[INFO] Cleaning up preview branches for $PR_STATE PR #${{ github.event.pull_request.number }}"
39+
40+ # Get the source branch name
41+ SOURCE_BRANCH="${{ github.event.pull_request.head.ref }}"
42+ echo "[INFO] Source branch: $SOURCE_BRANCH"
43+
44+ # Generate the safe prefix that would have been used for this branch
45+ # This transforms the branch name to match the preview branch naming convention:
46+ # 1. Remove all non-alphanumeric characters (tr -cd '[:alnum:]')
47+ # 2. Take only the first 6 characters (cut -c1-6)
48+ # Example: "feature/my-branch-123" -> "featur"
49+ safe_prefix=$(echo "$SOURCE_BRANCH" | tr -cd '[:alnum:]' | cut -c1-6)
50+ echo "[INFO] Looking for preview branches matching pattern: preview-${safe_prefix}-*"
51+
52+ # Find and delete all preview branches with this prefix
53+ git fetch origin
54+ BRANCHES=$(git branch -r | grep "origin/preview-${safe_prefix}-" | sed 's|origin/||' || true)
55+
56+ if [ -z "$BRANCHES" ]; then
57+ echo "[INFO] No preview branches found for this PR"
58+ else
59+ echo "[INFO] Found preview branches to delete:"
60+ echo "$BRANCHES"
61+
62+ for branch in $BRANCHES; do
63+ # Safety check: ensure branch starts with 'preview-'
64+ if [[ ! "$branch" =~ ^preview- ]]; then
65+ echo "[ERROR] Branch '$branch' does not start with 'preview-'"
66+ echo "[ERROR] Refusing to delete non-preview branch for safety"
67+ exit 1
68+ fi
69+
70+ echo "[INFO] Deleting branch: $branch"
71+ git push origin --delete "$branch" || echo "[WARN] Failed to delete $branch (may already be deleted)"
72+ done
73+
74+ echo "[SUCCESS] Cleanup complete"
75+ fi
76+
2077 create-preview :
2178 # This job needs to push a branch, so contents: write here only.
2279 permissions :
2380 contents : write
2481 runs-on : ubuntu-latest
2582
2683 # Skip for PRs from forks - they don't have write access
27- if : github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'workflow_dispatch'
84+ # Skip when PR is closed
85+ if : (github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'workflow_dispatch') && github.event.action != 'closed'
2886
2987 # Expose the generated preview branch name for the next job
3088 outputs :
0 commit comments