Skip to content

Commit 87d7cd5

Browse files
authored
feat: add github workflow to delete PR branch on merge (#6)
1 parent 6640e7d commit 87d7cd5

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Delete branch after PR merge
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
permissions:
8+
contents: write # allows deleting refs (branches)
9+
10+
jobs:
11+
delete-branch:
12+
if: github.event.pull_request.merged == true
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Delete branch
17+
env:
18+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
BRANCH: ${{ github.event.pull_request.head.ref }}
20+
REPO: ${{ github.repository }}
21+
PR_REPO: ${{ github.event.pull_request.head.repo.full_name }}
22+
run: |
23+
if [ "$PR_REPO" != "$REPO" ]; then
24+
echo "Branch is from a fork ($PR_REPO) — skipping deletion."
25+
exit 0
26+
fi
27+
28+
echo "Deleting branch '$BRANCH' from '$REPO'..."
29+
curl -s -X DELETE \
30+
-H "Authorization: Bearer $GH_TOKEN" \
31+
-H "Accept: application/vnd.github.v3+json" \
32+
"https://api.github.com/repos/$REPO/git/refs/heads/$BRANCH"

0 commit comments

Comments
 (0)