CI: Add workflow to cleanup PR caches when closed

This is pretty much copied from the GitHub Actions documentation:
https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries
This commit is contained in:
Rémi Verschelde 2025-03-13 16:54:07 +01:00
parent 701505eb4f
commit fd96ba48c6
No known key found for this signature in database
GPG key ID: C3336907360768E1

28
.github/workflows/cache_cleanup.yml vendored Normal file
View file

@ -0,0 +1,28 @@
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries
name: 🧹 Cache Cleanup
on:
pull_request:
types:
- closed
jobs:
cleanup:
name: Cleanup PR caches
runs-on: ubuntu-latest
steps:
- name: Cleanup
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge
run: |
echo "Fetching list of cache key"
cache_keys_for_pr=$(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 cache_key in $cache_keys_for_pr; do
gh cache delete $cache_key
echo "Deleted: $cache_key"
done
echo "Done"