-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
130 lines (112 loc) · 5.05 KB
/
cleanup-cache.yml
File metadata and controls
130 lines (112 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
name: Cleanup GitHub Actions Cache
on:
workflow_dispatch:
inputs:
branch:
description: 'Branch name (leave empty for current branch)'
required: false
type: string
key_pattern:
description: 'Cache key pattern (e.g., buildx, gha, leave empty for all)'
required: false
type: string
max_age_days:
description: 'Delete caches older than N days (0 = all)'
required: false
default: '0'
type: string
jobs:
cleanup:
runs-on: ubuntu-latest
permissions:
actions: write
contents: read
steps:
- name: Cleanup caches
shell: pwsh
run: |
Write-Host "Starting cache cleanup..."
$branch = "${{ github.event.inputs.branch }}"
$keyPattern = "${{ github.event.inputs.key_pattern }}"
$maxAgeDays = "${{ github.event.inputs.max_age_days }}"
# Get cache list
if ($branch) {
Write-Host "Filtering by branch: $branch"
$cacheListJson = gh cache list --repo ${{ github.repository }} --branch $branch --json id,key,createdAt,sizeInBytes --limit 1000
} else {
Write-Host "Getting all caches..."
$cacheListJson = gh cache list --repo ${{ github.repository }} --json id,key,createdAt,sizeInBytes --limit 1000
}
$caches = $cacheListJson | ConvertFrom-Json
# Filter by key pattern if specified
if ($keyPattern) {
Write-Host "Filtering by key pattern: $keyPattern"
$caches = $caches | Where-Object { $_.key -like "*$keyPattern*" }
}
# Filter by age if specified
if ($maxAgeDays -ne "0") {
$cutoffDate = (Get-Date).AddDays(-[int]$maxAgeDays).ToUniversalTime()
Write-Host "Filtering caches older than: $($cutoffDate.ToString('yyyy-MM-ddTHH:mm:ssZ'))"
$caches = $caches | Where-Object { [DateTime]::Parse($_.createdAt) -lt $cutoffDate }
}
$cacheCount = $caches.Count
Write-Host "Found $cacheCount caches to delete"
if ($cacheCount -eq 0) {
Write-Host "No caches to delete"
exit 0
}
# Calculate total size
$totalBytes = ($caches | Measure-Object -Property sizeInBytes -Sum).Sum
$totalSizeMB = [math]::Round($totalBytes / 1MB, 2)
$totalSizeGB = [math]::Round($totalBytes / 1GB, 2)
# Display summary
Write-Host "`n========== DELETION SUMMARY =========="
Write-Host "Total caches to delete: $cacheCount"
Write-Host "Total size: $totalSizeMB MB ($totalSizeGB GB)"
Write-Host "======================================`n"
# Display cache details
Write-Host "Caches to be deleted:"
Write-Host ("{0,-60} {1,-20} {2,10}" -f "Key", "Last Updated", "Size (MB)")
Write-Host ("{0,-60} {1,-20} {2,10}" -f "---", "------------", "---------")
$displayCount = [Math]::Min(20, $cacheCount)
for ($i = 0; $i -lt $displayCount; $i++) {
$cache = $caches[$i]
$sizeMB = [math]::Round($cache.sizeInBytes / 1MB, 2)
$lastUpdate = [DateTime]::Parse($cache.createdAt).ToString("yyyy-MM-dd HH:mm")
$keyDisplay = if ($cache.key.Length -gt 57) { $cache.key.Substring(0, 54) + "..." } else { $cache.key }
Write-Host ("{0,-60} {1,-20} {2,10}" -f $keyDisplay, $lastUpdate, $sizeMB)
}
if ($cacheCount -gt 20) {
Write-Host "... and $($cacheCount - 20) more caches"
}
# Delete caches
Write-Host "`nDeleting caches..."
$deleted = 0
$failed = 0
$deletedSize = 0
foreach ($cache in $caches) {
try {
gh cache delete $cache.id --repo ${{ github.repository }} 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
$deleted++
$deletedSize += $cache.sizeInBytes
Write-Host "✓ Deleted: $($cache.key.Substring(0, [Math]::Min(50, $cache.key.Length)))"
} else {
$failed++
Write-Host "✗ Failed: $($cache.key.Substring(0, [Math]::Min(50, $cache.key.Length)))"
}
} catch {
$failed++
Write-Host "✗ Failed: $($cache.key.Substring(0, [Math]::Min(50, $cache.key.Length)))"
}
}
# Final summary
$deletedSizeMB = [math]::Round($deletedSize / 1MB, 2)
$deletedSizeGB = [math]::Round($deletedSize / 1GB, 2)
Write-Host "`n========== CLEANUP COMPLETE =========="
Write-Host "Successfully deleted: $deleted caches"
Write-Host "Failed: $failed caches"
Write-Host "Total freed space: $deletedSizeMB MB ($deletedSizeGB GB)"
Write-Host "======================================"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}