Skip to content

Commit ed49364

Browse files
committed
Cleanup Github Actions cache for pull requests
1 parent 0a66d2c commit ed49364

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Cleanup caches after merge
2+
on:
3+
pull_request:
4+
types:
5+
- closed
6+
workflow_dispatch:
7+
8+
jobs:
9+
cleanup:
10+
runs-on: ubuntu-latest
11+
12+
permissions:
13+
# `actions:write` permission is required to delete caches
14+
# 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
15+
actions: write
16+
contents: read
17+
18+
steps:
19+
- name: Check out code
20+
uses: actions/checkout@v4
21+
22+
- name: Cleanup cache
23+
run: |
24+
gh extension install actions/gh-actions-cache
25+
26+
REPO=${{ github.repository }}
27+
BRANCH=refs/pull/${{ github.event.pull_request.number }}/merge
28+
29+
echo "Fetching list of cache key"
30+
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH --limit 100 --sort size | cut -f 1 )
31+
32+
## Setting this to not fail the workflow while deleting cache keys.
33+
set +e
34+
echo "Deleting caches..."
35+
for cacheKey in $cacheKeysForPR
36+
do
37+
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
38+
done
39+
echo "Done"
40+
env:
41+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)