Skip to content

Commit

Permalink
Summary for cleaning cache
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Feb 13, 2025
1 parent 5858773 commit 93c68cb
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions .github/workflows/cache-cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,51 @@ jobs:
steps:
- name: Cleanup
run: |
echo "Fetching list of cache key"
cacheKeysForPR=$(gh cache list --ref $BRANCH --limit 100 --json id --jq '.[].id')
## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR
do
gh cache delete $cacheKey
echo "# Cache Cleanup Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**PR Number:** #${{ github.event.pull_request.number }}" >> $GITHUB_STEP_SUMMARY
echo "**Branch:** \`$BRANCH\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "[DEBUG] Fetching cache list..."
# Get full cache details
CACHE_LIST=$(gh cache list --ref $BRANCH --limit 100 --json key,size,createdAt,id)
if [ -z "$CACHE_LIST" ] || [ "$CACHE_LIST" = "[]" ]; then
echo "[DEBUG] No caches found"
echo "No caches found for this PR" >> $GITHUB_STEP_SUMMARY
exit 0
fi
# Create table header
echo "| Cache ID | Cache Key | Size | Created At | Status |" >> $GITHUB_STEP_SUMMARY
echo "|----------|-----------|------|------------|--------|" >> $GITHUB_STEP_SUMMARY
# Extract IDs and process deletions
echo "$CACHE_LIST" | jq -r '.[] | [.id, .key, .size, .createdAt] | @tsv' | while IFS=$'\t' read -r id key size created; do
# Convert size to human readable format
if [ $size -ge 1048576 ]; then
readable_size=$(echo "scale=2; $size/1048576" | bc)"MB"
else
readable_size=$(echo "scale=2; $size/1024" | bc)"KB"
fi
echo "[DELETE] Processing cache ID: $id"
if gh cache delete $id > /dev/null 2>&1; then
status="✅ Deleted"
echo "[SUCCESS] Cache $id deleted"
else
status="❌ Failed"
echo "[ERROR] Failed to delete cache $id"
fi
# Add row to summary table
echo "| \`$id\` | \`$key\` | $readable_size | $created | $status |" >> $GITHUB_STEP_SUMMARY
done
echo "Done"
# Add completion timestamp
echo "" >> $GITHUB_STEP_SUMMARY
echo "Cleanup completed at: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_STEP_SUMMARY
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
Expand Down

0 comments on commit 93c68cb

Please sign in to comment.