-
-
Notifications
You must be signed in to change notification settings - Fork 39
86 lines (72 loc) · 2.74 KB
/
cache-clean.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
name: Cache Clean
on:
workflow_dispatch:
inputs:
all-branches:
type: boolean
description: "Delete caches for all branches (nuclear option)"
pull_request:
types:
- closed
permissions:
actions: write
jobs:
clear-cache:
runs-on: ubuntu-latest
steps:
- name: Delete caches
uses: actions/github-script@v7
with:
script: |
let totalDeleted = 0;
let totalBytes = 0;
let pageSize = 100;
let inputs = context.payload.inputs;
let ref = context.ref;
if (inputs && inputs["all-branches"] == "true") {
console.warn("Deleting caches for all branches");
ref = undefined;
}
function sizeInMb(size) {
return (size / 1024 / 1024).toFixed(2);
}
async function fetchCaches(params) {
return github.rest.actions.getActionsCacheList({
owner: context.repo.owner,
repo: context.repo.repo,
...params
});
}
async function fetchAllCaches(ref) {
let currentPage = 1;
let currentCaches = await fetchCaches({ per_page: pageSize, page: currentPage, ref: ref });
let totalCount = currentCaches.data.total_count;
let allCaches = currentCaches.data.actions_caches;
console.log(`Found ${totalCount} caches for ref ${ref} (page ${currentPage} of ${Math.ceil(totalCount / pageSize)})`);
while (true) {
if (allCaches.length >= totalCount || currentPage > 100) {
break;
} else {
currentPage++;
currentCaches = await fetchCaches({ per_page: pageSize, page: currentPage, ref: ref });
allCaches = allCaches.concat(currentCaches.data.actions_caches);
}
}
return allCaches;
}
async function deleteCachesForRef(ref) {
let caches = await fetchAllCaches(ref);
for (let cache of caches) {
totalDeleted++;
totalBytes += cache.size_in_bytes;
await github.rest.actions.deleteActionsCacheById({
owner: context.repo.owner,
repo: context.repo.repo,
cache_id: cache.id
});
console.log(`Deleted cache ${cache.id} (${sizeInMb(cache.size_in_bytes)} MB) for ref ${cache.ref}`);
}
}
await deleteCachesForRef(ref);
let totalMb = sizeInMb(totalBytes);
console.log(`Clear completed, deleted ${totalDeleted} caches (${totalMb} MB)`);