-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
65 lines (56 loc) · 2.1 KB
/
clean-up-cache.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
64
65
name: Cleanup Caches by Branch
on:
pull_request:
types:
- closed
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Setup GitHub CLI
run: |
# Ensure the latest version of GitHub CLI is installed
sudo apt-get update && sudo apt-get install -y gh
# Install the actions-cache extension
gh extension install actions/gh-actions-cache || echo "Extension already installed"
- name: Check Token Permissions
run: gh auth status
env:
GH_TOKEN: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}
- name: Cleanup Caches
run: |
REPO=${{ github.repository }}
# Adjust branch reference if necessary
BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge"
echo "Fetching list of cache keys for branch: $BRANCH"
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH --json key | jq -r '.[].key')
if [ -z "$cacheKeysForPR" ]; then
echo "No caches found for branch: $BRANCH"
exit 0
fi
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR; do
echo "Deleting cache: $cacheKey"
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
done
echo "Cleanup completed."
env:
GH_TOKEN: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}
- name: Verify Remaining Caches
run: |
echo "Checking for remaining caches in branch: $BRANCH"
remainingCaches=$(gh actions-cache list -R ${{ github.repository }} -B $BRANCH --json key | jq -r '.[].key')
if [ -z "$remainingCaches" ]; then
echo "All caches successfully cleared."
else
echo "Remaining caches detected:"
echo "$remainingCaches"
exit 1
fi
env:
GH_TOKEN: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}