Fix Julia cache flood in CI workflows #238
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Cleanup github runner caches on closed pull requests | |
| on: | |
| # pull_request_target is required so fork PRs can delete their own caches on close. | |
| # pull_request from a fork ships a read-only GITHUB_TOKEN regardless of the | |
| # permissions: block, causing gh cache delete to fail with HTTP 403. | |
| # Safe here: no PR code is checked out, the workflow only calls gh cache list/delete. | |
| pull_request_target: | |
| types: | |
| - closed | |
| jobs: | |
| cleanup: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| actions: write | |
| steps: | |
| - name: Cleanup | |
| run: | | |
| echo "::group::Fetching cache list for PR #${{ github.event.pull_request.number }}" | |
| echo "Branch ref: $BRANCH" | |
| # Get full cache list with details for logging | |
| cacheList=$(gh cache list --ref $BRANCH --limit 100 --json id,key,sizeInBytes) | |
| cacheCount=$(echo "$cacheList" | jq '. | length') | |
| echo "Found $cacheCount cache(s) for this PR" | |
| if [ "$cacheCount" -gt 0 ]; then | |
| echo "Cache details:" | |
| echo "$cacheList" | jq -r '.[] | " - ID: \(.id) | Key: \(.key) | Size: \(.sizeInBytes | tonumber / 1024 / 1024 | floor)MB"' | |
| fi | |
| echo "::endgroup::" | |
| if [ "$cacheCount" -eq 0 ]; then | |
| echo "No caches to delete" | |
| exit 0 | |
| fi | |
| # Extract just the IDs for deletion | |
| cacheKeysForPR=$(echo "$cacheList" | jq -r '.[].id') | |
| ## Setting this to not fail the workflow while deleting cache keys. | |
| set +e | |
| echo "::group::Deleting caches" | |
| deleted=0 | |
| failed=0 | |
| for cacheKey in $cacheKeysForPR | |
| do | |
| echo "Deleting cache ID: $cacheKey" | |
| if gh cache delete $cacheKey; then | |
| echo " ✓ Successfully deleted cache $cacheKey" | |
| ((deleted++)) | |
| else | |
| echo " ✗ Failed to delete cache $cacheKey" | |
| ((failed++)) | |
| fi | |
| done | |
| echo "::endgroup::" | |
| echo "::notice::Cache cleanup complete: $deleted deleted, $failed failed out of $cacheCount total" | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| GH_REPO: ${{ github.repository }} | |
| BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge |