Cleanup Caches #14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Cleanup Caches | |
on: | |
workflow_dispatch: # Keep manual trigger | |
workflow_run: | |
workflows: ['Build server', 'Playwright Tests', 'Prettier'] | |
types: | |
- completed | |
jobs: | |
cleanup: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Cleanup old caches | |
shell: bash | |
run: | | |
# Test GitHub CLI authentication | |
echo "π Testing GitHub CLI authentication..." | |
gh auth status || { | |
echo "β GitHub CLI authentication failed" | |
exit 1 | |
} | |
# Define services array | |
services=("server" "stage-transcriptions" "session-transcriptions" "clips" "reel-creator") | |
function cleanup_buildx_cache() { | |
local service=$1 | |
echo "π Scanning buildx cache for service: $service" | |
# Get all cache keys for this specific service's buildx cache | |
echo "π Fetching cache list..." | |
cacheKeys=$(gh cache list --limit 100 --json key,createdAt \ | |
--jq '.[] | select(.key | contains("Linux-buildx-'$service'")) | [.key, .createdAt] | @tsv' \ | |
| sort -k2,2r) || { | |
echo "β Failed to fetch cache list for $service" | |
return 1 | |
} | |
if [ -z "$cacheKeys" ]; then | |
echo "βΉοΈ No caches found for service: $service" | |
return 0 | |
fi | |
# Keep count of caches for this service | |
count=0 | |
while IFS=$'\t' read -r key date; do | |
((count++)) | |
# Keep only the most recent cache for each service | |
if [ "$count" -gt 1 ]; then | |
echo "ποΈ Deleting old cache for $service: $key" | |
gh cache delete "$key" || echo "β οΈ Failed to delete cache: $key" | |
else | |
echo "πΎ Keeping most recent cache for $service: $key" | |
fi | |
done <<< "$cacheKeys" | |
echo "β Finished cleaning $service caches" | |
} | |
# Clean up buildx caches for each service | |
echo "π Starting cache cleanup process..." | |
failed_services=() | |
for service in "${services[@]}"; do | |
if ! cleanup_buildx_cache "$service"; then | |
failed_services+=("$service") | |
fi | |
done | |
# Clean up other types of caches (playwright, yarn) | |
function cleanup_other_caches() { | |
local pattern=$1 | |
local keep=$2 | |
echo "π Scanning caches matching pattern: $pattern" | |
cacheKeys=$(gh cache list --limit 100 --json key,createdAt \ | |
--jq '.[] | select(.key | contains("'$pattern'")) | [.key, .createdAt] | @tsv' \ | |
| sort -k2,2r) || { | |
echo "β Failed to fetch cache list for $pattern" | |
return 1 | |
} | |
if [ -z "$cacheKeys" ]; then | |
echo "βΉοΈ No caches found for pattern: $pattern" | |
return 0 | |
fi | |
count=0 | |
while IFS=$'\t' read -r key date; do | |
((count++)) | |
if [ "$count" -gt "$keep" ]; then | |
echo "ποΈ Deleting old cache: $key" | |
gh cache delete "$key" || echo "β οΈ Failed to delete cache: $key" | |
else | |
echo "πΎ Keeping recent cache: $key" | |
fi | |
done <<< "$cacheKeys" | |
echo "β Finished cleaning $pattern caches" | |
} | |
# Clean up other cache types | |
failed_patterns=() | |
for pattern in "playwright" "yarn"; do | |
if ! cleanup_other_caches "$pattern" 2; then | |
failed_patterns+=("$pattern") | |
fi | |
done | |
# Report results | |
echo "π Cleanup Summary:" | |
if [ ${#failed_services[@]} -eq 0 ] && [ ${#failed_patterns[@]} -eq 0 ]; then | |
echo "π Cache cleanup completed successfully!" | |
exit 0 | |
else | |
echo "β οΈ Cache cleanup completed with some issues:" | |
[ ${#failed_services[@]} -gt 0 ] && echo "β Failed services: ${failed_services[*]}" | |
[ ${#failed_patterns[@]} -gt 0 ] && echo "β Failed patterns: ${failed_patterns[*]}" | |
exit 1 | |
fi | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GH_REPO: ${{ github.repository }} |