|
| 1 | +name: Repository Cleanup |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run every Sunday at 3 AM UTC |
| 6 | + - cron: '0 3 * * 0' |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + dry_run: |
| 10 | + description: 'Dry run (show what would be deleted)' |
| 11 | + required: false |
| 12 | + type: boolean |
| 13 | + default: false |
| 14 | + max_age_days: |
| 15 | + description: 'Delete workflow runs older than X days' |
| 16 | + required: false |
| 17 | + type: number |
| 18 | + default: 30 |
| 19 | + |
| 20 | +permissions: |
| 21 | + contents: write |
| 22 | + actions: write |
| 23 | + |
| 24 | +jobs: |
| 25 | + cleanup_workflow_runs: |
| 26 | + name: Cleanup Old Workflow Runs |
| 27 | + runs-on: ubuntu-latest |
| 28 | + steps: |
| 29 | + - name: Delete old workflow runs |
| 30 | + uses: Mattraks/delete-workflow-runs@v2 |
| 31 | + with: |
| 32 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + repository: ${{ github.repository }} |
| 34 | + retain_days: ${{ inputs.max_age_days || 30 }} |
| 35 | + keep_minimum_runs: 3 |
| 36 | + delete_workflow_pattern: "all" |
| 37 | + |
| 38 | + cleanup_caches: |
| 39 | + name: Cleanup Old Caches |
| 40 | + runs-on: ubuntu-latest |
| 41 | + steps: |
| 42 | + - name: Cleanup old caches |
| 43 | + uses: actions/github-script@v7 |
| 44 | + with: |
| 45 | + script: | |
| 46 | + const caches = await github.rest.actions.getActionsCacheList({ |
| 47 | + owner: context.repo.owner, |
| 48 | + repo: context.repo.repo, |
| 49 | + per_page: 100 |
| 50 | + }); |
| 51 | +
|
| 52 | + const now = new Date(); |
| 53 | + const maxAge = 14 * 24 * 60 * 60 * 1000; // 14 days in ms |
| 54 | + let deleted = 0; |
| 55 | +
|
| 56 | + for (const cache of caches.data.actions_caches) { |
| 57 | + const cacheAge = now - new Date(cache.last_accessed_at); |
| 58 | + if (cacheAge > maxAge) { |
| 59 | + if (${{ inputs.dry_run || false }}) { |
| 60 | + console.log(`[DRY RUN] Would delete cache: ${cache.key} (${cache.size_in_bytes} bytes)`); |
| 61 | + } else { |
| 62 | + await github.rest.actions.deleteActionsCacheById({ |
| 63 | + owner: context.repo.owner, |
| 64 | + repo: context.repo.repo, |
| 65 | + cache_id: cache.id |
| 66 | + }); |
| 67 | + console.log(`Deleted cache: ${cache.key}`); |
| 68 | + deleted++; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + console.log(`Deleted ${deleted} old caches`); |
| 73 | +
|
| 74 | + cleanup_stale_branches: |
| 75 | + name: Cleanup Stale Branches |
| 76 | + runs-on: ubuntu-latest |
| 77 | + steps: |
| 78 | + - uses: actions/checkout@v6 |
| 79 | + with: |
| 80 | + fetch-depth: 0 |
| 81 | + |
| 82 | + - name: Delete merged branches |
| 83 | + run: | |
| 84 | + echo "Checking for merged branches to delete..." |
| 85 | +
|
| 86 | + # Get all remote branches except main, master, develop |
| 87 | + BRANCHES=$(git branch -r --merged origin/main | grep -v 'main\|master\|develop\|HEAD' | sed 's/origin\///') |
| 88 | +
|
| 89 | + if [ -z "$BRANCHES" ]; then |
| 90 | + echo "No stale branches found." |
| 91 | + exit 0 |
| 92 | + fi |
| 93 | +
|
| 94 | + for branch in $BRANCHES; do |
| 95 | + if [ "${{ inputs.dry_run }}" = "true" ]; then |
| 96 | + echo "[DRY RUN] Would delete branch: $branch" |
| 97 | + else |
| 98 | + echo "Deleting merged branch: $branch" |
| 99 | + git push origin --delete "$branch" || echo "Could not delete $branch" |
| 100 | + fi |
| 101 | + done |
| 102 | +
|
| 103 | + cleanup_draft_releases: |
| 104 | + name: Cleanup Old Draft Releases |
| 105 | + runs-on: ubuntu-latest |
| 106 | + steps: |
| 107 | + - name: Delete old draft releases |
| 108 | + uses: actions/github-script@v7 |
| 109 | + with: |
| 110 | + script: | |
| 111 | + const releases = await github.rest.repos.listReleases({ |
| 112 | + owner: context.repo.owner, |
| 113 | + repo: context.repo.repo, |
| 114 | + per_page: 100 |
| 115 | + }); |
| 116 | +
|
| 117 | + const now = new Date(); |
| 118 | + const maxAge = 7 * 24 * 60 * 60 * 1000; // 7 days |
| 119 | + let deleted = 0; |
| 120 | +
|
| 121 | + for (const release of releases.data) { |
| 122 | + if (release.draft) { |
| 123 | + const releaseAge = now - new Date(release.created_at); |
| 124 | + if (releaseAge > maxAge) { |
| 125 | + if (${{ inputs.dry_run || false }}) { |
| 126 | + console.log(`[DRY RUN] Would delete draft release: ${release.name || release.tag_name}`); |
| 127 | + } else { |
| 128 | + await github.rest.repos.deleteRelease({ |
| 129 | + owner: context.repo.owner, |
| 130 | + repo: context.repo.repo, |
| 131 | + release_id: release.id |
| 132 | + }); |
| 133 | + console.log(`Deleted draft release: ${release.name || release.tag_name}`); |
| 134 | + deleted++; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + console.log(`Deleted ${deleted} old draft releases`); |
| 140 | +
|
| 141 | + summary: |
| 142 | + name: Cleanup Summary |
| 143 | + runs-on: ubuntu-latest |
| 144 | + needs: [cleanup_workflow_runs, cleanup_caches, cleanup_stale_branches, cleanup_draft_releases] |
| 145 | + if: always() |
| 146 | + steps: |
| 147 | + - name: Summary |
| 148 | + run: | |
| 149 | + echo "## 🧹 Repository Cleanup Complete" >> $GITHUB_STEP_SUMMARY |
| 150 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 151 | + echo "| Task | Status |" >> $GITHUB_STEP_SUMMARY |
| 152 | + echo "|------|--------|" >> $GITHUB_STEP_SUMMARY |
| 153 | + echo "| Workflow Runs | ${{ needs.cleanup_workflow_runs.result }} |" >> $GITHUB_STEP_SUMMARY |
| 154 | + echo "| Caches | ${{ needs.cleanup_caches.result }} |" >> $GITHUB_STEP_SUMMARY |
| 155 | + echo "| Stale Branches | ${{ needs.cleanup_stale_branches.result }} |" >> $GITHUB_STEP_SUMMARY |
| 156 | + echo "| Draft Releases | ${{ needs.cleanup_draft_releases.result }} |" >> $GITHUB_STEP_SUMMARY |
| 157 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 158 | + echo "Dry run mode: ${{ inputs.dry_run || false }}" >> $GITHUB_STEP_SUMMARY |
0 commit comments