1+ name : Cache Cleanup
2+
3+ on :
4+ schedule :
5+ # Run every Sunday at 2 AM UTC
6+ - cron : ' 0 2 * * 0'
7+ workflow_dispatch :
8+ inputs :
9+ age_limit :
10+ description : ' Cache age limit in days'
11+ required : false
12+ default : ' 7'
13+ type : string
14+
15+ permissions :
16+ actions : write
17+
18+ jobs :
19+ cleanup :
20+ runs-on : ubuntu-latest
21+ steps :
22+ - name : Cleanup old caches
23+ run : |
24+ echo "Repository: ${{ github.repository }}"
25+ echo "Default branch: ${{ github.event.repository.default_branch }}"
26+
27+ # Set age limit
28+ AGE_LIMIT="${{ github.event.inputs.age_limit || '7' }}"
29+ echo "Age limit: ${AGE_LIMIT} days"
30+
31+ # Get current cache usage
32+ echo "Current cache usage:"
33+ gh api repos/${{ github.repository }}/actions/caches --paginate | jq -r '.actions_caches[] | "\(.key) - \(.size_in_bytes) bytes - \(.created_at)"' | head -20
34+
35+ # Calculate cutoff date
36+ CUTOFF_DATE=$(date -d "${AGE_LIMIT} days ago" -u +"%Y-%m-%dT%H:%M:%SZ")
37+ echo "Cutoff date: ${CUTOFF_DATE}"
38+
39+ # Get list of caches to delete
40+ CACHES_TO_DELETE=$(gh api repos/${{ github.repository }}/actions/caches --paginate | jq -r --arg cutoff "$CUTOFF_DATE" '.actions_caches[] | select(.created_at < $cutoff) | .id')
41+
42+ # Count caches to delete
43+ CACHE_COUNT=$(echo "$CACHES_TO_DELETE" | wc -l)
44+ if [ -z "$CACHES_TO_DELETE" ] || [ "$CACHE_COUNT" -eq 0 ]; then
45+ echo "No caches found older than ${AGE_LIMIT} days"
46+ else
47+ echo "Found ${CACHE_COUNT} caches to delete"
48+
49+ # Delete old caches
50+ for cache_id in $CACHES_TO_DELETE; do
51+ if [ -n "$cache_id" ]; then
52+ echo "Deleting cache ID: ${cache_id}"
53+ gh api --method DELETE repos/${{ github.repository }}/actions/caches/${cache_id} || echo "Failed to delete cache ${cache_id}"
54+ fi
55+ done
56+ fi
57+
58+ # Get list of all branches
59+ ALL_BRANCHES=$(gh api repos/${{ github.repository }}/branches --paginate | jq -r '.[].name')
60+
61+ # Get list of caches
62+ ALL_CACHE_REFS=$(gh api repos/${{ github.repository }}/actions/caches --paginate | jq -r '.actions_caches[].ref')
63+
64+ # Find caches for deleted branches
65+ echo "Checking for caches from deleted branches..."
66+ for cache_ref in $ALL_CACHE_REFS; do
67+ # Extract branch name from ref (format: refs/heads/branch-name)
68+ BRANCH_NAME=$(echo "$cache_ref" | sed 's|refs/heads/||')
69+
70+ # Skip if it's a pull request ref or tag ref
71+ if [[ "$cache_ref" =~ refs/pull/ ]] || [[ "$cache_ref" =~ refs/tags/ ]]; then
72+ continue
73+ fi
74+
75+ # Check if branch still exists
76+ if ! echo "$ALL_BRANCHES" | grep -q "^${BRANCH_NAME}$"; then
77+ echo "Found caches for deleted branch: ${BRANCH_NAME}"
78+
79+ # Get cache IDs for this branch
80+ BRANCH_CACHE_IDS=$(gh api repos/${{ github.repository }}/actions/caches --paginate | jq -r --arg ref "$cache_ref" '.actions_caches[] | select(.ref == $ref) | .id')
81+
82+ # Delete caches for this branch
83+ for cache_id in $BRANCH_CACHE_IDS; do
84+ if [ -n "$cache_id" ]; then
85+ echo "Deleting cache ID ${cache_id} for deleted branch ${BRANCH_NAME}"
86+ gh api --method DELETE repos/${{ github.repository }}/actions/caches/${cache_id} || echo "Failed to delete cache ${cache_id}"
87+ fi
88+ done
89+ fi
90+ done
91+
92+ echo "Cache cleanup completed"
93+
94+ # Show final cache usage
95+ echo "Final cache usage:"
96+ gh api repos/${{ github.repository }}/actions/caches --paginate | jq -r '.actions_caches[] | "\(.key) - \(.size_in_bytes) bytes - \(.created_at)"' | head -10
97+
98+ env :
99+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
0 commit comments