Repository navigation
83 lines (80 loc) · 3.38 KB
/
Copy pathcache-janitor.yml
File metadata and controls
83 lines (80 loc) · 3.38 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
name: Actions cache janitor
# Scheduled workflow to delete stale/superseded cache entries.
# Goal is to keep the repo cache usage under the 10 GB limit, but also to ensure
# that stale entries are not restored. Without this cleanup, it's been observed
# that a new cache entry can fall to the end of the LRU list and be evicted
# before it is used.
on:
schedule:
- cron: '30 1-23/2 * * *' # :30 past every odd numbered hour
workflow_dispatch:
inputs:
dry-run:
description: Only list what would be deleted
type: boolean
default: false
permissions:
actions: write
jobs:
prune:
name: keep the newest entry per lineage
if: ${{ github.repository_owner == 'wolfssl' }}
runs-on: ubuntu-24.04
timeout-minutes: 10
steps:
- name: List, group and delete superseded cache entries
shell: bash
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
DRY_RUN: ${{ github.event_name == 'workflow_dispatch' && inputs.dry-run && 'true' || 'false' }}
run: |
# Oldest first: saves land at the end and restores do not reorder
# the list, so a row cannot repeat across pages while paging.
gh api --paginate -X GET "repos/$REPO/actions/caches" \
-F per_page=100 -f sort=created_at -f direction=asc \
--jq '.actions_caches[]' | jq -s '.' > caches.json
python3 - <<'PY' > delete-ids.txt
import json, re, sys
entries = json.load(open("caches.json"))
def lineage(key):
key = re.sub(r"-[0-9a-f]{40}$", "", key)
key = re.sub(r"-\d+-\d+$", "", key)
return key
# Guard against a row repeated across pages.
entries = list({e["id"]: e for e in entries}.values())
groups = {}
for e in entries:
groups.setdefault((e["ref"], lineage(e["key"])), []).append(e)
keep = deleted = 0
for (ref, lin), group in sorted(groups.items()):
group.sort(key=lambda e: e["created_at"], reverse=True)
keep += group[0]["size_in_bytes"]
for e in group[1:]:
deleted += e["size_in_bytes"]
print(e["id"])
print("delete %s %6.0f MB %s (%s), newest is %s" % (
e["created_at"][:16], e["size_in_bytes"] / 1e6, e["key"],
ref, group[0]["created_at"][:16]), file=sys.stderr)
print("entries: %d, keep %.1f GB, delete %.1f GB" % (
len(entries), keep / 1e9, deleted / 1e9), file=sys.stderr)
PY
echo "superseded entries: $(wc -l < delete-ids.txt)"
if [ "$DRY_RUN" = "true" ]; then
echo "dry run: nothing deleted"
exit 0
fi
# An entry can vanish between the list and the delete (eviction, an
# in-job prune, another janitor run); that is not a failure. Anything
# else, a 403 for instance, is.
failed=0
while read -r id; do
if err=$(gh api --method DELETE "repos/$REPO/actions/caches/$id" 2>&1 >/dev/null); then
echo "deleted $id"
elif [[ $err == *"HTTP 404"* ]]; then
echo "::warning::$id already gone"
else
echo "failed to delete $id: $err"; failed=1
fi
done < delete-ids.txt
exit $failed