From 933a49845d68b84069efc79381c111221533f816 Mon Sep 17 00:00:00 2001 From: Mario-SO Date: Mon, 30 Dec 2024 11:51:13 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=20Better=20cache=20handling=20for?= =?UTF-8?q?=20build=20server=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/clean_caches.yml | 81 +++++++++++++++++++----------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/.github/workflows/clean_caches.yml b/.github/workflows/clean_caches.yml index 14355a923..08ff2e9a0 100644 --- a/.github/workflows/clean_caches.yml +++ b/.github/workflows/clean_caches.yml @@ -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 }}