-
Notifications
You must be signed in to change notification settings - Fork 1.7k
63 lines (52 loc) · 2.27 KB
/
delete-caches.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
name: Cleanup Caches
# See:
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries
on:
pull_request:
types:
- closed
jobs:
cleanup-branch-caches:
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
env:
REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
steps:
- name: Check out code
uses: actions/[email protected]
- name: Setup gh-actions-cache extension
run: gh extension install actions/gh-actions-cache
- name: Retrieve Trunk SHA
id: get-sha
run: |
SHA=$(gh pr view -R $REPO $PR_NUMBER --json mergeCommit --jq .mergeCommit.oid)
echo "sha=$SHA" >> $GITHUB_OUTPUT
- name: Cleanup Caches
env:
TRUNK_SHA: ${{ steps.get-sha.outputs.sha }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set +e
PR_BRANCH=refs/pull/$PR_NUMBER/merge
echo "Fetching list of cache keys for the PR branch ($PR_BRANCH)"
PR_CACHE_KEYS=$(gh actions-cache list -R $REPO -B $PR_BRANCH | cut -f 1)
echo "Deleting caches for PR branch ($PR_BRANCH)..."
for CACHE_KEY in $PR_CACHE_KEYS; do
gh actions-cache delete $CACHE_KEY -R $REPO -B $PR_BRANCH --confirm
done
if [[ -n "$TRUNK_SHA" ]]; then
echo "Found corresponding merge commit $TRUNK_SHA"
QUEUE_BRANCH="gh-readonly-queue/develop/pr-${PR_NUMBER}-${TRUNK_SHA}"
echo "Fetching list of cache keys for the merge queue branch ($QUEUE_BRANCH)"
QUEUE_CACHE_KEYS=$(gh actions-cache list -R $REPO -B $QUEUE_BRANCH | cut -f 1)
echo "Deleting caches for merge queue branch ($QUEUE_BRANCH)..."
for CACHE_KEY in $QUEUE_CACHE_KEYS; do
gh actions-cache delete $CACHE_KEY -R $REPO -B $QUEUE_BRANCH --confirm
done
fi
echo "Done"