Cleanup Branches (Merged or Closed) #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Cleanup Branches (Merged or Closed) | |
| on: | |
| schedule: | |
| - cron: "0 3 * * *" # daily at 03:00 UTC | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pull-requests: read | |
| jobs: | |
| cleanup: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Delete merged or closed branches older than 7 days | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| set -euo pipefail | |
| CUTOFF_DATE=$(date -u -d "7 days ago" +%s) | |
| echo "Cutoff timestamp: $CUTOFF_DATE" | |
| gh pr list \ | |
| --state all \ | |
| --base main \ | |
| --json number,state,mergedAt,closedAt,headRefName \ | |
| --limit 500 | | |
| jq -r '.[] | select(.state == "MERGED" or .state == "CLOSED") | "\(.mergedAt // .closedAt) \(.headRefName)"' | | |
| sort -u | | |
| while read -r closedOrMergedAt branch; do | |
| # Schutz | |
| case "$branch" in | |
| main|master|develop) | |
| echo "Skipping protected branch: $branch" | |
| continue | |
| ;; | |
| esac | |
| CLOSED_OR_MERGED_TS=$(date -d "$closedOrMergedAt" +%s) | |
| if [ "$CLOSED_OR_MERGED_TS" -lt "$CUTOFF_DATE" ]; then | |
| echo "Deleting branch: $branch (at $closedOrMergedAt)" | |
| gh api \ | |
| -X DELETE \ | |
| repos/${{ github.repository }}/git/refs/heads/$branch \ | |
| || echo "Branch $branch already deleted" | |
| else | |
| echo "Keeping branch: $branch (recent)" | |
| fi | |
| done |