Skip to content

Commit b36e94c

Browse files
committed
build: delete stale pre releases after a merge or schedule
1 parent 15e0c40 commit b36e94c

File tree

3 files changed

+85
-84
lines changed

3 files changed

+85
-84
lines changed

.claude/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"Bash(gh pr status:*)",
1010
"Bash(gh pr update-branch:*)",
1111
"Bash(gh pr view:*)",
12+
"Bash(gh release list:*)",
1213
"Bash(gh search code:*)",
1314
"Bash(git diff:*)",
1415
"Bash(git fetch:*)",

.github/workflows/delete-pr-build-on-close.yml

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Delete stale pre-releases
2+
# This workflow deletes pre-releases whose source branch no longer exists.
3+
#
4+
# The CircleCI configuration builds CLI binaries when a PR is opened.
5+
# These are uploaded to the upstream project as GitHub (pre-)releases.
6+
#
7+
# The release tag matches one of the following patterns:
8+
# - v1.2.3-example-branch-placeholder # Branches on upstream
9+
# - v1.2.3-pull-12-head # Branches from forks
10+
#
11+
# Runs on push to main (catches merges) and on a daily schedule
12+
# (catches closed-without-merge PRs and any other missed cleanups).
13+
#
14+
# Only pre-releases are considered. Production releases (v1.2.3) and
15+
# the long-running dev-build pre-release are never touched.
16+
on:
17+
push:
18+
branches:
19+
- main
20+
schedule:
21+
- cron: "0 0 * * *"
22+
workflow_dispatch:
23+
24+
jobs:
25+
delete-stale-pre-releases:
26+
name: Delete stale pre-releases
27+
runs-on: ubuntu-latest
28+
permissions:
29+
contents: write
30+
steps:
31+
- name: Delete pre-releases for branches that no longer exist
32+
env:
33+
GH_TOKEN: ${{ github.token }}
34+
shell: bash
35+
run: |
36+
REPO="slackapi/slack-cli"
37+
DELETED=0
38+
39+
# Gather all pre-release tag names
40+
PRE_RELEASES=$(gh release list --repo="$REPO" --json="tagName,isPrerelease" --limit=100 --jq '.[] | select(.isPrerelease) | .tagName')
41+
42+
for TAG in $PRE_RELEASES; do
43+
# Skip the long-running dev-build pre-release
44+
if [ "$TAG" = "dev-build" ]; then
45+
continue
46+
fi
47+
48+
echo "Checking pre-release: $TAG"
49+
50+
# Extract branch name from the tag. Tags follow the pattern:
51+
# v1.2.3-branch-name (standard PR builds)
52+
# v1.2.3-branch-name-feature (feature builds)
53+
# Strip the leading semver prefix to recover the branch name.
54+
BRANCH=$(echo "$TAG" | sed 's/^v[0-9]*\.[0-9]*\.[0-9]*-//')
55+
56+
# Feature builds have a "-feature" suffix that is not part of the branch name
57+
BRANCH_WITHOUT_FEATURE=$(echo "$BRANCH" | sed 's/-feature$//')
58+
59+
# Check if the branch still exists on the remote
60+
BRANCH_EXISTS=false
61+
for CANDIDATE in "$BRANCH" "$BRANCH_WITHOUT_FEATURE"; do
62+
if gh api "repos/$REPO/branches/$CANDIDATE" --silent 2>/dev/null; then
63+
BRANCH_EXISTS=true
64+
break
65+
fi
66+
done
67+
68+
if [ "$BRANCH_EXISTS" = "true" ]; then
69+
echo " Branch still exists, skipping"
70+
continue
71+
fi
72+
73+
# Branch is gone — delete the stale pre-release and its tag
74+
echo " Branch no longer exists, deleting pre-release: $TAG"
75+
if gh release --repo="$REPO" delete "$TAG" -y --cleanup-tag; then
76+
echo " Successfully deleted $TAG"
77+
DELETED=$((DELETED + 1))
78+
else
79+
echo " Failed to delete $TAG"
80+
fi
81+
sleep 1
82+
done
83+
84+
echo "Deleted $DELETED stale pre-release(s)"

0 commit comments

Comments
 (0)