--- /dev/null
+# Cleanup caches by a branch, original version from actions/cache documentation
+name: Cache Cleanup
+on:
+ pull_request:
+ types:
+ - closed
+ workflow_dispatch:
+
+jobs:
+ cleanup:
+ name: Cache Cleanup
+ runs-on: ubuntu-latest
+ permissions:
+ # `actions:write` permission is required to delete caches
+ # See also: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#delete-a-github-actions-cache-for-a-repository-using-a-cache-id
+ actions: write
+ contents: read
+ steps:
+ - name: Cleanup
+ run: |
+ echo "Fetching list of cache keys"
+ 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
+ done
+ echo "Done"
+ env:
+ GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ GH_REPO: ${{ github.repository }}
+ BRANCH: ${{ github.event.ref || format('refs/pull/{0}/merge', github.event.pull_request.number) }}