Skip to content

Commit 1ca3c1a

Browse files
committed
Actions: Remove all caches after past two
Also conditionally activate warmup Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com>
1 parent f1501b0 commit 1ca3c1a

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

.github/workflows/warmup.yml

+46-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ on:
44
schedule:
55
- cron: '0 0 */3 * *'
66
workflow_dispatch:
7+
inputs:
8+
force_warmup:
9+
description: 'Force cache warmup regardless of age'
10+
required: false
11+
default: false
12+
type: boolean
713

814
jobs:
915
cleanup-cache:
@@ -15,11 +21,15 @@ jobs:
1521
uses: actions/github-script@v7
1622
with:
1723
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
1926
const caches = await github.rest.actions.getActionsCacheList({
2027
owner: context.repo.owner,
2128
repo: context.repo.repo
2229
});
30+
31+
// Track if any cache needs warming up
32+
let needsWarmup = false;
2333
2434
// Group caches by their prefix pattern (e.g., sccache-Windows-cuda-main)
2535
const cacheGroups = {};
@@ -42,35 +52,50 @@ jobs:
4252
const sortedCaches = cacheGroups[prefix].sort((a, b) =>
4353
new Date(b.created_at) - new Date(a.created_at));
4454
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
6456
if (sortedCaches.length > 0) {
6557
const newestCache = sortedCaches[0];
6658
const createdAt = new Date(newestCache.created_at);
6759
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
6867
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+
});
6990
}
7091
}
92+
93+
// Set output to control whether to run warmup jobs
94+
core.setOutput('needs_warmup', needsWarmup.toString());
7195
7296
warmup-unix:
7397
needs: cleanup-cache
98+
if: ${{ needs.cleanup-cache.outputs.needs_warmup == 'true' || (github.event_name == 'workflow_dispatch' && github.event.inputs.force_warmup == 'true') }}
7499
runs-on: ${{ matrix.os }}
75100
strategy:
76101
matrix:
@@ -109,6 +134,7 @@ jobs:
109134
110135
warmup-win:
111136
needs: cleanup-cache
137+
if: ${{ needs.cleanup-cache.outputs.needs_warmup == 'true' || (github.event_name == 'workflow_dispatch' && github.event.inputs.force_warmup == 'true') }}
112138
runs-on: ${{ matrix.os }}
113139
strategy:
114140
matrix:

0 commit comments

Comments
 (0)