Skip to content

Commit bf8a3fd

Browse files
committed
Add cleanup workflow for closed PRs
Currently, it only deletes the GitHub Actions caches associated with the closed Pull Request. Signed-off-by: Tom Wieczorek <[email protected]>
1 parent 0de1631 commit bf8a3fd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Cleanup closed PRs
2+
on:
3+
pull_request:
4+
types:
5+
- closed
6+
7+
jobs:
8+
# Based on https://github.com/actions/cache/blob/v4.2.0/tips-and-workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy
9+
actions-caches:
10+
name: Delete GitHub Action caches
11+
runs-on: ubuntu-latest
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: Cleanup
20+
env:
21+
GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
22+
GH_REPO: "${{ github.repository }}"
23+
BRANCH: "refs/pull/${{ github.event.pull_request.number }}/merge"
24+
run: |
25+
set -euo pipefail
26+
gh api -X GET /repos/{owner}/{repo}/actions/caches -f ref="$BRANCH" --paginate -q '.actions_caches[] | "\(.id) \(.key)"' | {
27+
fail=0
28+
while read -r id key; do
29+
echo Deleting cache with ID $id: $key
30+
gh api -X DELETE /repos/{owner}/{repo}/actions/caches/"$id" || fail=1
31+
done
32+
[ $fail -eq 0 ]
33+
}

0 commit comments

Comments
 (0)