Skip to content

Commit

Permalink
πŸš‘ Better cache handling for build server action
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario-SO committed Dec 30, 2024
1 parent f53b324 commit 933a498
Showing 1 changed file with 53 additions and 28 deletions.
81 changes: 53 additions & 28 deletions .github/workflows/clean_caches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,68 @@ jobs:
- name: Cleanup old caches
shell: bash
run: |
function cleanup_caches() {
local pattern=$1
echo "Cleaning up caches matching pattern: $pattern"
# 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"
# Create a temporary file to store cache information
temp_file=$(mktemp)
# 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)
# Get all cache keys for the pattern, sorted by date (newest first)
gh cache list --limit 100 --json key,createdAt \
# 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 > "$temp_file"
| sort -k2,2r)
# Process each cache type separately
while read -r key date; do
# Extract the base pattern (everything before the hash)
base_pattern=$(echo "$key" | sed -E 's/-[a-f0-9]{40}$//')
# Count how many we've seen of this type
count=$(grep -c "^${base_pattern}" "$temp_file")
count=0
while IFS=$'\t' read -r key date; do
((count++))
# If we've seen more than 2 caches of this type, delete it
if [ "$count" -gt 2 ]; then
echo "Deleting old cache: $key"
if [ "$count" -gt "$keep" ]; then
echo "πŸ—‘οΈ Deleting old cache: $key"
gh cache delete "$key"
else
echo "Keeping recent cache: $key"
echo "πŸ’Ύ Keeping recent cache: $key"
fi
done < "$temp_file"
# Cleanup
rm -f "$temp_file"
done <<< "$cacheKeys"
echo "βœ… Finished cleaning $pattern caches"
}
# Clean up different types of caches
cleanup_caches "Linux-buildx"
cleanup_caches "playwright"
cleanup_caches "yarn"
# Clean up other cache types
cleanup_other_caches "playwright" 2
cleanup_other_caches "yarn" 2
echo "πŸŽ‰ Cache cleanup completed successfully!"
env:
GH_TOKEN: ${{ secrets.TOKEN }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}

0 comments on commit 933a498

Please sign in to comment.