@samitouri / QOS-React / commits / 208905257f

[ci] Add cache cleanup workflow (#32675)

> Caches have branch scope restriction in place. This means that if caches for a specific branch are using a lot of storage quota, it may result into more frequently used caches from default branch getting thrashed. For example, if there are many pull requests happening on a repo and are creating caches, these cannot be used in default branch scope but will still occupy a lot of space till they get cleaned up by eviction policy. But sometime we want to clean them up on a faster cadence so as to ensure default branch is not thrashing. https://github.com/actions/cache/blob/main/tips-and-workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/32675). * __->__ #32675 * #32674

lauren committed Mar 19, 2025 at 15:42 UTC 208905257f5da4b05f3153388563cabf14eb85bb
1 file changed +35
.github/workflows/shared_cleanup_branch_caches.yml new
+35
@@ -0,0 +1,35 @@
1 +# https://github.com/actions/cache/blob/main/tips-and-workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy
2 +
3 +name: (Shared) Cleanup Branch Caches
4 +on:
5 + pull_request:
6 + types:
7 + - closed
8 + workflow_dispatch:
9 +
10 +jobs:
11 + cleanup:
12 + runs-on: ubuntu-latest
13 + permissions:
14 + # `actions:write` permission is required to delete caches
15 + # 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
16 + actions: write
17 + contents: read
18 + steps:
19 + - name: Cleanup
20 + run: |
21 + echo "Fetching list of cache key"
22 + cacheKeysForPR=$(gh cache list --ref $BRANCH --limit 100 --json id --jq '.[].id')
23 +
24 + ## Setting this to not fail the workflow while deleting cache keys.
25 + set +e
26 + echo "Deleting caches..."
27 + for cacheKey in $cacheKeysForPR
28 + do
29 + gh cache delete $cacheKey
30 + done
31 + echo "Done"
32 + env:
33 + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34 + GH_REPO: ${{ github.repository }}
35 + BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge