|
| 1 | +name: Cleanup backport branches |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + types: [closed] |
| 9 | + schedule: |
| 10 | + - cron: '0 2 * * 0' # Weekly on Sunday at 2 AM UTC |
| 11 | + workflow_dispatch: |
| 12 | + inputs: |
| 13 | + dry_run: |
| 14 | + description: 'Dry run mode (true/false)' |
| 15 | + required: false |
| 16 | + default: 'true' |
| 17 | + type: choice |
| 18 | + options: |
| 19 | + - 'true' |
| 20 | + - 'false' |
| 21 | + |
| 22 | +jobs: |
| 23 | + # Immediate cleanup when a backport PR is merged |
| 24 | + delete-on-merge: |
| 25 | + name: Delete merged backport branch |
| 26 | + if: | |
| 27 | + github.event_name == 'pull_request' && |
| 28 | + github.event.pull_request.merged == true && |
| 29 | + startsWith(github.head_ref, 'backport/') |
| 30 | + runs-on: ubuntu-latest |
| 31 | + steps: |
| 32 | + - name: Delete merged backport branch |
| 33 | + uses: jessfraz/branch-cleanup-action@master |
| 34 | + env: |
| 35 | + GITHUB_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }} |
| 36 | + |
| 37 | + # Weekly cleanup of any merged backport branches older than 7 days |
| 38 | + scheduled-cleanup: |
| 39 | + name: Cleanup old backport branches |
| 40 | + if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' |
| 41 | + runs-on: ubuntu-latest |
| 42 | + steps: |
| 43 | + - name: Checkout repository |
| 44 | + uses: actions/checkout@v5 |
| 45 | + with: |
| 46 | + fetch-depth: 0 |
| 47 | + token: ${{ secrets.GH_ACCESS_TOKEN }} |
| 48 | + |
| 49 | + - name: Setup cleanup parameters |
| 50 | + id: params |
| 51 | + run: | |
| 52 | + # shellcheck disable=SC2086 |
| 53 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 54 | + # shellcheck disable=SC2086 |
| 55 | + echo "dry_run=${{ github.event.inputs.dry_run }}" >> $GITHUB_OUTPUT |
| 56 | + else |
| 57 | + # For scheduled runs, use dry_run=false (actually delete) |
| 58 | + echo "dry_run=false" >> $GITHUB_OUTPUT |
| 59 | + fi |
| 60 | +
|
| 61 | + - name: Cleanup old merged backport branches |
| 62 | + run: | |
| 63 | + DRY_RUN="${{ steps.params.outputs.dry_run }}" |
| 64 | +
|
| 65 | + echo "=== Backport Branch Cleanup ===" |
| 66 | + echo "Dry run mode: $DRY_RUN" |
| 67 | + echo "Searching for merged backport branches older than 7 days..." |
| 68 | + echo "" |
| 69 | +
|
| 70 | + # Fetch all branches |
| 71 | + git fetch origin |
| 72 | +
|
| 73 | + # Get main/master branch name |
| 74 | + MAIN_BRANCH=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5) |
| 75 | + echo "Main branch: $MAIN_BRANCH" |
| 76 | + echo "" |
| 77 | +
|
| 78 | + DELETED_COUNT=0 |
| 79 | + SKIPPED_COUNT=0 |
| 80 | +
|
| 81 | + # Find all backport branches merged into main |
| 82 | + for branch in $(git branch -r --merged "origin/$MAIN_BRANCH" | grep 'origin/backport/' | sed 's/origin\///'); do |
| 83 | + # Get the merge commit timestamp from main branch (when the branch was actually merged) |
| 84 | + # Find the merge commit in main that contains commits from this branch |
| 85 | + branch_head=$(git rev-parse "origin/$branch" 2>/dev/null || echo "") |
| 86 | + if [ -z "$branch_head" ]; then |
| 87 | + echo "⚠️ Skipping $branch (unable to get branch head)" |
| 88 | + SKIPPED_COUNT=$((SKIPPED_COUNT + 1)) |
| 89 | + continue |
| 90 | + fi |
| 91 | +
|
| 92 | + # Find merge commit in main that merged this branch |
| 93 | + merge_commit=$(git log "origin/$MAIN_BRANCH" --merges --first-parent --format="%H %ct" | \ |
| 94 | + while read -r commit_hash commit_time; do |
| 95 | + if git merge-base --is-ancestor "$branch_head" "$commit_hash" 2>/dev/null; then |
| 96 | + echo "$commit_time" |
| 97 | + break |
| 98 | + fi |
| 99 | + done) |
| 100 | +
|
| 101 | + if [ -z "$merge_commit" ]; then |
| 102 | + echo "⚠️ Skipping $branch (unable to find merge commit)" |
| 103 | + SKIPPED_COUNT=$((SKIPPED_COUNT + 1)) |
| 104 | + continue |
| 105 | + fi |
| 106 | +
|
| 107 | + current_timestamp=$(date +%s) |
| 108 | + days_old=$(( ( current_timestamp - merge_commit ) / 86400 )) |
| 109 | +
|
| 110 | + if [ $days_old -gt 7 ]; then |
| 111 | + if [ "$DRY_RUN" = "true" ]; then |
| 112 | + echo "🔍 [DRY RUN] Would delete: $branch (${days_old} days old)" |
| 113 | + DELETED_COUNT=$((DELETED_COUNT + 1)) |
| 114 | + else |
| 115 | + echo "🗑️ Deleting: $branch (${days_old} days old)" |
| 116 | + if git push origin --delete "$branch" 2>/dev/null; then |
| 117 | + echo " ✅ Successfully deleted" |
| 118 | + DELETED_COUNT=$((DELETED_COUNT + 1)) |
| 119 | + else |
| 120 | + echo " ❌ Failed to delete" |
| 121 | + SKIPPED_COUNT=$((SKIPPED_COUNT + 1)) |
| 122 | + fi |
| 123 | + fi |
| 124 | + fi |
| 125 | + done |
| 126 | +
|
| 127 | + echo "" |
| 128 | + echo "=== Summary ===" |
| 129 | + if [ "$DRY_RUN" = "true" ]; then |
| 130 | + echo "Branches that would be deleted: $DELETED_COUNT" |
| 131 | + else |
| 132 | + echo "Branches deleted: $DELETED_COUNT" |
| 133 | + fi |
| 134 | + echo "Branches skipped: $SKIPPED_COUNT" |
0 commit comments