File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44#
55# Respects protected branches and skips fork branches.
66# Deletes all matching branches per run, oldest first.
7- #
8- # To test without deleting anything, trigger this workflow manually via
9- # workflow_dispatch and leave dry-run set to "true" (the default). The
10- # run will log "Would delete: ..." for every branch it would have removed.
117
128name : Delete stale branches
139
1410on :
15- push :
16- branches :
17- - stale-branch-cleanup # temporary: for dry-run testing on this PR branch — remove before merging
1811 schedule :
1912 - cron : " 0 4 * * *"
2013 workflow_dispatch :
21- inputs :
22- dry-run :
23- description : " Dry run (log what would be deleted without deleting)"
24- required : false
25- default : " true"
26- type : choice
27- options :
28- - " true"
29- - " false"
3014
3115permissions :
3216 contents : write
3923 - name : Delete unassociated branches older than 60 days
4024 env :
4125 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
42- DRY_RUN : ${{ github.event_name == 'push' && 'true' || github.event_name == 'schedule' && 'false' || inputs.dry-run }}
4326 run : |
4427 deleted=0
4528
46- if [ "$DRY_RUN" = "true" ]; then
47- echo "*** DRY RUN — no branches will be deleted ***"
48- fi
49-
5029 protected=$(gh api "repos/$GITHUB_REPOSITORY/branches?protected=true&per_page=100" \
5130 | jq -r '.[].name')
5231
6342 return
6443 fi
6544
66- if [ "$DRY_RUN" = "true" ]; then
67- echo "Would delete ($reason): $branch"
68- deleted=$((deleted + 1))
69- return
70- fi
71-
7245 gh api "repos/$GITHUB_REPOSITORY/git/refs/heads/$branch" \
7346 -X DELETE 2>/dev/null \
7447 && { echo "Deleted ($reason): $branch"; deleted=$((deleted + 1)); } \
@@ -137,4 +110,4 @@ jobs:
137110
138111 rm -f "$candidates"
139112
140- echo "--- Done: $deleted branch(es) ${DRY_RUN:+would be } deleted ---"
113+ echo "--- Done: $deleted branch(es) deleted ---"
You can’t perform that action at this time.
0 commit comments