4
4
schedule :
5
5
- cron : ' 0 0 */3 * *'
6
6
workflow_dispatch :
7
+ inputs :
8
+ force_warmup :
9
+ description : ' Force cache warmup regardless of age'
10
+ required : false
11
+ default : false
12
+ type : boolean
7
13
8
14
jobs :
9
15
cleanup-cache :
@@ -15,11 +21,15 @@ jobs:
15
21
uses : actions/github-script@v7
16
22
with :
17
23
script : |
18
- const retentionDays = 2; // Keep caches for 2 days
24
+ const retentionCount = 2; // Keep the 2 most recent caches per prefix
25
+ const forceWarmupDays = 5; // Force warmup if newest cache is older than this
19
26
const caches = await github.rest.actions.getActionsCacheList({
20
27
owner: context.repo.owner,
21
28
repo: context.repo.repo
22
29
});
30
+
31
+ // Track if any cache needs warming up
32
+ let needsWarmup = false;
23
33
24
34
// Group caches by their prefix pattern (e.g., sccache-Windows-cuda-main)
25
35
const cacheGroups = {};
@@ -42,35 +52,50 @@ jobs:
42
52
const sortedCaches = cacheGroups[prefix].sort((a, b) =>
43
53
new Date(b.created_at) - new Date(a.created_at));
44
54
45
- // Skip the most recent cache
46
- for (let i = 1; i < sortedCaches.length; i++) {
47
- const cache = sortedCaches[i];
48
- const createdAt = new Date(cache.created_at);
49
- const ageInDays = (now - createdAt) / (1000 * 60 * 60 * 24);
50
-
51
- if (ageInDays > retentionDays) {
52
- console.log(`Deleting old cache: ${cache.key}, created ${ageInDays.toFixed(1)} days ago`);
53
- await github.rest.actions.deleteActionsCacheByKey({
54
- owner: context.repo.owner,
55
- repo: context.repo.repo,
56
- key: cache.key
57
- });
58
- } else {
59
- console.log(`Keeping cache: ${cache.key}, created ${ageInDays.toFixed(1)} days ago`);
60
- }
61
- }
62
-
63
- // Always log the kept most recent cache
55
+ // Check if most recent cache is older than forceWarmupDays
64
56
if (sortedCaches.length > 0) {
65
57
const newestCache = sortedCaches[0];
66
58
const createdAt = new Date(newestCache.created_at);
67
59
const ageInDays = (now - createdAt) / (1000 * 60 * 60 * 24);
60
+
61
+ if (ageInDays > forceWarmupDays) {
62
+ console.log(`Cache ${prefix} is stale (${ageInDays.toFixed(1)} days old). Will force warmup.`);
63
+ needsWarmup = true;
64
+ }
65
+
66
+ // Log the kept most recent cache
68
67
console.log(`Keeping most recent cache: ${newestCache.key}, created ${ageInDays.toFixed(1)} days ago`);
68
+
69
+ // Keep second most recent cache if it exists
70
+ if (sortedCaches.length > 1) {
71
+ const secondCache = sortedCaches[1];
72
+ const secondCreatedAt = new Date(secondCache.created_at);
73
+ const secondAgeInDays = (now - secondCreatedAt) / (1000 * 60 * 60 * 24);
74
+ console.log(`Keeping second most recent cache: ${secondCache.key}, created ${secondAgeInDays.toFixed(1)} days ago`);
75
+ }
76
+ }
77
+
78
+ // Delete all caches beyond the retention count
79
+ for (let i = retentionCount; i < sortedCaches.length; i++) {
80
+ const cache = sortedCaches[i];
81
+ const createdAt = new Date(cache.created_at);
82
+ const ageInDays = (now - createdAt) / (1000 * 60 * 60 * 24);
83
+
84
+ console.log(`Deleting old cache: ${cache.key}, created ${ageInDays.toFixed(1)} days ago`);
85
+ await github.rest.actions.deleteActionsCacheByKey({
86
+ owner: context.repo.owner,
87
+ repo: context.repo.repo,
88
+ key: cache.key
89
+ });
69
90
}
70
91
}
92
+
93
+ // Set output to control whether to run warmup jobs
94
+ core.setOutput('needs_warmup', needsWarmup.toString());
71
95
72
96
warmup-unix :
73
97
needs : cleanup-cache
98
+ if : ${{ needs.cleanup-cache.outputs.needs_warmup == 'true' || (github.event_name == 'workflow_dispatch' && github.event.inputs.force_warmup == 'true') }}
74
99
runs-on : ${{ matrix.os }}
75
100
strategy :
76
101
matrix :
@@ -109,6 +134,7 @@ jobs:
109
134
110
135
warmup-win :
111
136
needs : cleanup-cache
137
+ if : ${{ needs.cleanup-cache.outputs.needs_warmup == 'true' || (github.event_name == 'workflow_dispatch' && github.event.inputs.force_warmup == 'true') }}
112
138
runs-on : ${{ matrix.os }}
113
139
strategy :
114
140
matrix :
0 commit comments