Skip to content

Commit efb2b60

Browse files
committed
Use a unique disk cache key for each workflow run
1 parent d635e2d commit efb2b60

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

index.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function setupBazel() {
2626

2727
await setupBazelisk()
2828
await restoreCache(config.bazeliskCache)
29-
await restoreCache(config.diskCache)
29+
await restoreDiskCache(config.diskCache)
3030
await restoreCache(config.repositoryCache)
3131
await restoreExternalCaches(config.externalCache)
3232

@@ -143,7 +143,7 @@ async function restoreExternalCaches(cacheConfig) {
143143
}
144144
}
145145

146-
async function restoreCache(cacheConfig) {
146+
async function restoreCacheImpl(cacheConfig, primaryKey, restoreKeys, cacheHit) {
147147
if (!cacheConfig.enabled) {
148148
return
149149
}
@@ -152,23 +152,20 @@ async function restoreCache(cacheConfig) {
152152
await setTimeout(delay, async function () {
153153
core.startGroup(`Restore cache for ${cacheConfig.name}`)
154154

155-
const hash = await glob.hashFiles(cacheConfig.files.join('\n'))
156155
const name = cacheConfig.name
157156
const paths = cacheConfig.paths
158-
const restoreKey = `${config.baseCacheKey}-${name}-`
159-
const key = `${restoreKey}${hash}`
160157

161-
core.debug(`Attempting to restore ${name} cache from ${key}`)
158+
core.debug(`Attempting to restore ${name} cache from ${primaryKey}`)
162159

163160
const restoredKey = await cache.restoreCache(
164-
paths, key, [restoreKey],
161+
paths, primaryKey, restoreKeys,
165162
{ segmentTimeoutInMs: 300000 } // 5 minutes
166163
)
167164

168165
if (restoredKey) {
169166
core.info(`Successfully restored cache from ${restoredKey}`)
170167

171-
if (restoredKey === key) {
168+
if (cacheHit(restoredKey)) {
172169
core.saveState(`${name}-cache-hit`, 'true')
173170
}
174171
} else {
@@ -179,4 +176,28 @@ async function restoreCache(cacheConfig) {
179176
}())
180177
}
181178

179+
async function restoreCache(cacheConfig) {
180+
const hash = await glob.hashFiles(cacheConfig.files.join('\n'))
181+
const restoreKey = `${config.baseCacheKey}-${cacheConfig.name}-`
182+
const key = `${restoreKey}${hash}`
183+
await restoreCacheImpl(
184+
cacheConfig, key, [restoreKey],
185+
restoredKey => restoredKey === key
186+
)
187+
}
188+
189+
async function restoreDiskCache(cacheConfig) {
190+
const hash = await glob.hashFiles(cacheConfig.files.join('\n'))
191+
192+
// Since disk caches get updated on any change, each run has a unique key.
193+
// Therefore it can only be restored by prefix match, rather than exact key match.
194+
// When multiple prefix matches exist, the most recent is selected.
195+
const restoreKey = `${config.baseCacheKey}-${cacheConfig.name}-`
196+
const hashedRestoreKey = `${restoreKey}${hash}-`
197+
await restoreCacheImpl(
198+
cacheConfig, hashedRestoreKey, [hashedRestoreKey, restoreKey],
199+
restoredKey => restoredKey.startsWith(hashedRestoreKey)
200+
)
201+
}
202+
182203
run()

post.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs')
22
const path = require('path')
33
const cache = require('@actions/cache')
44
const core = require('@actions/core')
5+
const github = require('@actions/github')
56
const glob = require('@actions/glob')
67
const config = require('./config')
78
const gc = require('./gc')
@@ -80,7 +81,7 @@ async function saveDiskCache(cacheConfig) {
8081
// We don't want to follow symlinks as it's extremely slow on macOS.
8182
{ followSymbolicLinks: false }
8283
)
83-
const key = `${config.baseCacheKey}-${cacheConfig.name}-${hash}`
84+
const key = `${config.baseCacheKey}-${cacheConfig.name}-${hash}-${github.context.runId}.${github.context.runAttempt}`
8485
core.debug(`Attempting to save ${paths} cache to ${key}`)
8586
await cache.saveCache(paths, key)
8687
core.info('Successfully saved cache')

0 commit comments

Comments
 (0)