ProxmoxVED/.github/workflows/clear_old_branches.yml
CanbiZ (MickLesk) fbd38968c9 Expand branch cleanup to include closed PR branches
The workflow now deletes branches from both merged and closed pull requests older than 7 days, not just merged ones. This helps keep the repository cleaner by removing stale branches associated with closed PRs as well.
2026-01-20 08:34:36 +01:00

54 lines
1.5 KiB
YAML
Generated

name: Cleanup merged branches
on:
schedule:
- cron: "0 3 * * *" # täglich um 03:00 UTC
workflow_dispatch:
permissions:
contents: write
pull-requests: read
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- 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") | "\(.state) \(.mergedAt // .closedAt) \(.headRefName)"' |
while read -r state 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 ($state at $closedOrMergedAt)"
gh api \
-X DELETE \
repos/${{ github.repository }}/git/refs/heads/$branch \
|| echo "Branch $branch already deleted"
else
echo "Keeping branch: $branch (recent $state)"
fi
done