@@ -11,10 +11,10 @@ const core = __nccwpck_require__(7484)
1111const github = __nccwpck_require__(3228)
1212
1313const bazeliskVersion = core.getInput('bazelisk-version')
14+ const cachePrefix = core.getInput('cache-prefix')
1415const cacheVersion = core.getInput('cache-version')
1516const externalCacheConfig = yaml.parse(core.getInput('external-cache'))
1617const moduleRoot = core.getInput('module-root')
17- const maxDiskCacheSize = core.getInput('max-disk-cache-size')
1818
1919const homeDir = os.homedir()
2020const arch = os.arch()
@@ -48,32 +48,19 @@ switch (platform) {
4848 break
4949}
5050
51- const baseCacheKey = `setup-bazel-${cacheVersion}-${platform}`
51+ const baseCacheKey = cachePrefix ? `setup-bazel-${cachePrefix}` : `setup-bazel-${cacheVersion}-${platform}`
5252const bazelrc = core.getMultilineInput('bazelrc')
5353
54- const diskCacheConfig = core.getInput('disk-cache')
55- const diskCacheEnabled = diskCacheConfig !== 'false'
56- let diskCacheName = 'disk'
54+ const diskCacheEnabled = core.getInput('disk-cache')
55+ const maxDiskCacheSize = core.getInput('max-disk-cache-size')
5756if (diskCacheEnabled) {
5857 bazelrc.push(`common --disk_cache=${bazelDisk}`)
59- if (diskCacheName !== 'true') {
60- diskCacheName = `${diskCacheName}-${diskCacheConfig}`
61- }
6258}
6359
64- const repositoryCacheConfig = core.getInput('repository-cache')
65- const repositoryCacheEnabled = repositoryCacheConfig !== 'false'
66- let repositoryCacheFiles = [
67- `${moduleRoot}/MODULE.bazel`,
68- `${moduleRoot}/WORKSPACE.bazel`,
69- `${moduleRoot}/WORKSPACE.bzlmod`,
70- `${moduleRoot}/WORKSPACE`
71- ]
60+ const repositoryCacheEnabled = core.getInput('repository-cache')
61+ const maxRepositoryCacheSize = core.getInput('max-repository-cache-size')
7262if (repositoryCacheEnabled) {
7363 bazelrc.push(`common --repository_cache=${bazelRepository}`)
74- if (repositoryCacheConfig !== 'true') {
75- repositoryCacheFiles = Array(repositoryCacheConfig).flat()
76- }
7764}
7865
7966const googleCredentials = core.getInput('google-credentials')
@@ -146,12 +133,8 @@ module.exports = {
146133 bazelrc,
147134 diskCache: {
148135 enabled: diskCacheEnabled,
149- files: [
150- ...repositoryCacheFiles,
151- `${moduleRoot}/**/BUILD.bazel`,
152- `${moduleRoot}/**/BUILD`
153- ],
154- name: diskCacheName,
136+ maxSize: maxDiskCacheSize,
137+ name: 'disk',
155138 paths: [bazelDisk]
156139 },
157140 maxDiskCacheSize,
@@ -167,7 +150,7 @@ module.exports = {
167150 },
168151 repositoryCache: {
169152 enabled: repositoryCacheEnabled,
170- files: repositoryCacheFiles ,
153+ maxSize: maxRepositoryCacheSize ,
171154 name: 'repository',
172155 paths: [bazelRepository]
173156 },
@@ -183,27 +166,23 @@ const crypto = __nccwpck_require__(6982)
183166const fs = __nccwpck_require__(9896)
184167const path = __nccwpck_require__(6928)
185168const core = __nccwpck_require__(7484)
186- const config = __nccwpck_require__(700)
187-
188- const diskCachePath = config.diskCache.paths[0]
189- const diskCacheHash = diskCachePath + '.sha256'
190169
191- function init() {
192- if (!config.diskCache .enabled) {
170+ function init(cacheConfig ) {
171+ if (!cacheConfig .enabled) {
193172 return
194173 }
195174
196- core.startGroup(" Computing initial disk cache hash" )
197- fs.writeFileSync(diskCacheHash, computeDiskCacheHash( ))
175+ core.startGroup(` Computing initial ${cacheConfig.name} cache hash` )
176+ fs.writeFileSync(cacheConfig.path + '.sha256', computeCacheHash(cacheConfig.path ))
198177 core.endGroup()
199178}
200179
201- function run() {
202- if (!fs.existsSync(diskCachePath )) {
180+ function run(cacheConfig ) {
181+ if (!fs.existsSync(cacheConfig.path )) {
203182 return
204183 }
205184
206- const files = fs.readdirSync(diskCachePath , { withFileTypes: true, recursive: true })
185+ const files = fs.readdirSync(cacheConfig.path , { withFileTypes: true, recursive: true })
207186 .filter(d => d.isFile())
208187 .map(d => {
209188 const file = path.join(d.path, d.name)
@@ -212,34 +191,38 @@ function run() {
212191 })
213192 .sort((a, b) => b.mtime - a.mtime)
214193
215- core.startGroup(`Running disk cache garbage collection`)
216- const deleteThreshold = config.maxDiskCacheSize * 1024 ** 3
194+ core.startGroup(`Running ${cacheConfig.name} cache garbage collection`)
195+ const deleteThreshold = cacheConfig.maxSize * 1024 ** 3
217196 let cacheSize = 0
218197 let reclaimed = 0
219198 for (const { file, size } of files) {
220- cacheSize += size
221- if (cacheSize >= deleteThreshold) {
199+ if (cacheSize + size >= deleteThreshold) {
222200 fs.unlinkSync(file)
223201 reclaimed++
202+ } else {
203+ cacheSize += size
224204 }
225205 }
226206 core.info(`Reclaimed ${reclaimed} files`)
227207 core.endGroup()
208+
209+ return cacheChanged(cacheConfig)
228210}
229211
230- function cacheChanged() {
231- core.startGroup(`Checking disk cache for changes`)
232- const changed = fs.readFileSync(diskCacheHash) != computeDiskCacheHash()
212+ function cacheChanged(cacheConfig) {
213+ core.startGroup(`Checking ${cacheConfig.name} cache for changes`)
214+ const hash = computeCacheHash(cacheConfig.path)
215+ const changed = fs.readFileSync(cacheConfig.path + '.sha256') != hash
233216 core.info(`Cache has changes: ${changed}`)
234217 core.endGroup()
235- return changed
218+ return changed ? hash : undefined
236219}
237220
238- function computeDiskCacheHash( ) {
221+ function computeCacheHash(path ) {
239222 let hash = crypto.createHash('sha256')
240223
241- if (fs.existsSync(diskCachePath )) {
242- const files = fs.readdirSync(diskCachePath , { withFileTypes: true, recursive: true })
224+ if (fs.existsSync(path )) {
225+ const files = fs.readdirSync(path , { withFileTypes: true, recursive: true })
243226 .filter(d => d.isFile())
244227 .map(d => d.path)
245228 .sort()
@@ -255,7 +238,6 @@ function computeDiskCacheHash() {
255238module.exports = {
256239 init,
257240 run,
258- cacheChanged,
259241}
260242
261243
@@ -103296,11 +103278,9 @@ async function setupBazel() {
103296103278
103297103279 await setupBazelisk()
103298103280 await restoreCache(config.bazeliskCache)
103299- await restoreDiskCache (config.diskCache)
103300- await restoreCache (config.repositoryCache)
103281+ await restoreGcCache (config.diskCache)
103282+ await restoreGcCache (config.repositoryCache)
103301103283 await restoreExternalCaches(config.externalCache)
103302-
103303- gc.init()
103304103284}
103305103285
103306103286async function setupBazelisk() {
@@ -103456,18 +103436,17 @@ async function restoreCache(cacheConfig) {
103456103436 )
103457103437}
103458103438
103459- async function restoreDiskCache(cacheConfig) {
103460- const hash = await glob.hashFiles(cacheConfig.files.join('\n'))
103461-
103439+ async function restoreGcCache(cacheConfig) {
103462103440 // Since disk caches get updated on any change, each run has a unique key.
103463103441 // Therefore it can only be restored by prefix match, rather than exact key match.
103464103442 // When multiple prefix matches exist, the most recent is selected.
103465103443 const restoreKey = `${config.baseCacheKey}-${cacheConfig.name}-`
103466- const hashedRestoreKey = `${restoreKey}${hash}-`
103467103444 await restoreCacheImpl(
103468- cacheConfig, hashedRestoreKey , [hashedRestoreKey, restoreKey],
103445+ cacheConfig, restoreKey , [restoreKey],
103469103446 restoredKey => restoredKey.startsWith(hashedRestoreKey)
103470103447 )
103448+
103449+ gc.init(cacheConfig)
103471103450}
103472103451
103473103452run()
0 commit comments