name: Delete branch after merge
on:
pull_request:
types:
jobs:
delete-branch:
if: github.event.pull_request.merged == true && github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Delete merged branch
uses: actions/github-script@v7
with:
script: |
const branch = context.payload.pull_request.head.ref;
const protected = ['main', 'develop'];
if (protected.includes(branch)) {
console.log(`Skipping protected branch: ${branch}`);
return;
}
try {
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `heads/${branch}`,
});
console.log(`Deleted branch: ${branch}`);
} catch (e) {
console.log(`Branch already deleted or not found: ${branch}`);
}