|
| 1 | +name: Clean GitHub Actions Cache |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + branch: |
| 7 | + description: 'Branch to clean cache (leave empty for all branches)' |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + cache_key_pattern: |
| 11 | + description: 'Cache key pattern to delete (e.g., "buildx-", leave empty for all)' |
| 12 | + required: false |
| 13 | + type: string |
| 14 | + |
| 15 | +jobs: |
| 16 | + clean-cache: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Checkout Repository |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
| 22 | + - name: Clean Cache |
| 23 | + env: |
| 24 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 25 | + run: | |
| 26 | + echo "🧹 Cleaning GitHub Actions cache..." |
| 27 | + |
| 28 | + # Get repository info |
| 29 | + REPO="${{ github.repository }}" |
| 30 | + BRANCH="${{ inputs.branch }}" |
| 31 | + PATTERN="${{ inputs.cache_key_pattern }}" |
| 32 | + |
| 33 | + echo "Repository: $REPO" |
| 34 | + echo "Branch filter: ${BRANCH:-all branches}" |
| 35 | + echo "Pattern filter: ${PATTERN:-all caches}" |
| 36 | + |
| 37 | + # List all caches |
| 38 | + echo "" |
| 39 | + echo "📋 Listing caches..." |
| 40 | + gh cache list --repo "$REPO" --limit 100 |
| 41 | + |
| 42 | + # Delete caches |
| 43 | + echo "" |
| 44 | + echo "🗑️ Deleting caches..." |
| 45 | + |
| 46 | + DELETED=0 |
| 47 | + FAILED=0 |
| 48 | + |
| 49 | + # Get cache list in JSON format |
| 50 | + CACHES=$(gh cache list --repo "$REPO" --limit 100 --json id,key,ref) |
| 51 | + |
| 52 | + # Parse and delete each cache |
| 53 | + echo "$CACHES" | jq -r '.[] | @json' | while read -r cache; do |
| 54 | + CACHE_ID=$(echo "$cache" | jq -r '.id') |
| 55 | + CACHE_KEY=$(echo "$cache" | jq -r '.key') |
| 56 | + CACHE_REF=$(echo "$cache" | jq -r '.ref') |
| 57 | + |
| 58 | + # Apply branch filter |
| 59 | + if [ -n "$BRANCH" ] && [[ "$CACHE_REF" != *"$BRANCH"* ]]; then |
| 60 | + continue |
| 61 | + fi |
| 62 | + |
| 63 | + # Apply pattern filter |
| 64 | + if [ -n "$PATTERN" ] && [[ "$CACHE_KEY" != *"$PATTERN"* ]]; then |
| 65 | + continue |
| 66 | + fi |
| 67 | + |
| 68 | + echo "Deleting cache: $CACHE_KEY (ID: $CACHE_ID, Ref: $CACHE_REF)" |
| 69 | + |
| 70 | + if gh cache delete "$CACHE_ID" --repo "$REPO" 2>/dev/null; then |
| 71 | + DELETED=$((DELETED + 1)) |
| 72 | + else |
| 73 | + FAILED=$((FAILED + 1)) |
| 74 | + echo " ⚠️ Failed to delete cache ID: $CACHE_ID" |
| 75 | + fi |
| 76 | + done |
| 77 | + |
| 78 | + echo "" |
| 79 | + echo "✅ Cache cleanup completed!" |
| 80 | + echo " Deleted: $DELETED" |
| 81 | + echo " Failed: $FAILED" |
| 82 | + |
| 83 | + # Show remaining caches |
| 84 | + echo "" |
| 85 | + echo "📋 Remaining caches:" |
| 86 | + gh cache list --repo "$REPO" --limit 20 |
| 87 | +
|
| 88 | + - name: Summary |
| 89 | + run: | |
| 90 | + echo "### 🧹 Cache Cleanup Summary" >> $GITHUB_STEP_SUMMARY |
| 91 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 92 | + echo "- **Repository**: ${{ github.repository }}" >> $GITHUB_STEP_SUMMARY |
| 93 | + echo "- **Branch filter**: ${{ inputs.branch || 'all branches' }}" >> $GITHUB_STEP_SUMMARY |
| 94 | + echo "- **Pattern filter**: ${{ inputs.cache_key_pattern || 'all caches' }}" >> $GITHUB_STEP_SUMMARY |
| 95 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 96 | + echo "Cache cleanup completed successfully! ✅" >> $GITHUB_STEP_SUMMARY |
0 commit comments