Skip to content

Cleanup Caches

Cleanup Caches #14

Workflow file for this run

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 }}