-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
56 lines (46 loc) · 1.74 KB
/
clean-up-cache-cron.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
name: Cleanup Caches by Cron
on:
schedule:
- cron: '0 3 * * *' # Runs daily at 03:00 UTC
workflow_dispatch: # Allows manual triggering if needed
permissions:
contents: write
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Set Up GitHub CLI
run: |
sudo apt-get update && sudo apt-get install -y gh
gh extension install actions/gh-actions-cache || echo "Extension already installed"
- name: Cleanup Caches
env:
GH_TOKEN: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}
run: |
REPO=${{ github.repository }}
BRANCH="master" # Only focus on the master branch
echo "Fetching list of cache keys for branch: $BRANCH"
cacheKeysForMaster=$(gh actions-cache list -R $REPO -B $BRANCH --json key | jq -r '.[].key')
if [ -z "$cacheKeysForMaster" ]; then
echo "No caches found for branch: $BRANCH"
exit 0
fi
echo "Deleting caches..."
for cacheKey in $cacheKeysForMaster; do
echo "Deleting cache: $cacheKey"
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
done
echo "Cleanup completed."
- 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 }}