Skip to content

Cleanup Caches

Cleanup Caches #8

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: |
# 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
cacheKeys=$(gh cache list --limit 100 --json key,createdAt \
--jq '.[] | select(.key | contains("Linux-buildx-'$service'")) | [.key, .createdAt] | @tsv' \
| sort -k2,2r)
# 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"
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..."
for service in "${services[@]}"; do
cleanup_buildx_cache "$service"
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)
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"
else
echo "πŸ’Ύ Keeping recent cache: $key"
fi
done <<< "$cacheKeys"
echo "βœ… Finished cleaning $pattern caches"
}
# Clean up other cache types
cleanup_other_caches "playwright" 2
cleanup_other_caches "yarn" 2
echo "πŸŽ‰ Cache cleanup completed successfully!"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}