Add or update delete-merged-branches workflow
This commit is contained in:
parent
9d4b846cb5
commit
7262ad65b3
|
|
@ -0,0 +1,41 @@
|
|||
# Author: Ahmad Samir <a.atta@exp-sa.com>
|
||||
name: Auto Delete Branch After Merge
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [closed] # triggers when PR is closed (merged or just closed)
|
||||
|
||||
jobs:
|
||||
delete-merged-branch:
|
||||
if: github.event.pull_request.merged == true
|
||||
name: Delete Merged Branch
|
||||
runs-on: app-sever-project-runner # your self-hosted runner
|
||||
|
||||
steps:
|
||||
- name: Delete merged branch (with rules)
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GH_TOKEN }}
|
||||
REPO: ${{ github.repository }}
|
||||
BASE_REF: ${{ github.event.pull_request.base.ref }}
|
||||
HEAD_REF: ${{ github.event.pull_request.head.ref }}
|
||||
run: |
|
||||
echo "🔍 Base branch: $BASE_REF"
|
||||
echo "🔍 Head branch: $HEAD_REF"
|
||||
|
||||
# ❌ Do not delete dev_* merged into preprod_*
|
||||
if [[ "$HEAD_REF" == dev_* && "$BASE_REF" == preprod_* ]]; then
|
||||
echo "🚫 Rule: Do not delete dev_* merged into preprod_*"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# ❌ Do not delete preprod_* merged into master_*
|
||||
if [[ "$HEAD_REF" == preprod_* && "$BASE_REF" == master_* ]]; then
|
||||
echo "🚫 Rule: Do not delete preprod_* merged into master_*"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "✅ Allowed to delete '$HEAD_REF' from '$REPO'"
|
||||
|
||||
curl -s -X DELETE -H "Authorization: token $GH_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/$REPO/git/refs/heads/$HEAD_REF -w "
|
||||
🧹 Deleted branch '$HEAD_REF' from '$REPO' — HTTP %{http_code}
|
||||
"
|
||||
Loading…
Reference in New Issue