2021-10-30 13:15:20 +00:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
import * as cache from '@actions/cache'
|
|
|
|
import * as github from '@actions/github'
|
2021-12-07 23:52:53 +00:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
2021-12-07 19:29:37 +00:00
|
|
|
import {CacheListener} from './cache-reporting'
|
2021-12-07 23:52:53 +00:00
|
|
|
import {isCacheDebuggingEnabled, getCacheKeyPrefix, determineJobContext, handleCacheFailure} from './cache-utils'
|
2021-10-30 13:15:20 +00:00
|
|
|
|
2021-11-28 17:19:56 +00:00
|
|
|
const CACHE_PROTOCOL_VERSION = 'v5-'
|
2021-12-07 23:52:53 +00:00
|
|
|
|
|
|
|
export const META_FILE_DIR = '.gradle-build-action'
|
|
|
|
export const PROJECT_ROOTS_FILE = 'project-roots.txt'
|
2021-10-30 13:15:20 +00:00
|
|
|
|
2021-11-28 17:19:56 +00:00
|
|
|
/**
|
|
|
|
* Represents a key used to restore a cache entry.
|
|
|
|
* The Github Actions cache will first try for an exact match on the key.
|
|
|
|
* If that fails, it will try for a prefix match on any of the restoreKeys.
|
|
|
|
*/
|
|
|
|
class CacheKey {
|
|
|
|
key: string
|
|
|
|
restoreKeys: string[]
|
|
|
|
|
|
|
|
constructor(key: string, restoreKeys: string[]) {
|
|
|
|
this.key = key
|
|
|
|
this.restoreKeys = restoreKeys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a cache key specific to the current job execution.
|
|
|
|
* The key is constructed from the following inputs:
|
|
|
|
* - A user-defined prefix (optional)
|
|
|
|
* - The cache protocol version
|
|
|
|
* - The name of the cache
|
|
|
|
* - The runner operating system
|
|
|
|
* - The name of the Job being executed
|
|
|
|
* - The matrix values for the Job being executed (job context)
|
|
|
|
* - The SHA of the commit being executed
|
|
|
|
*
|
|
|
|
* Caches are restored by trying to match the these key prefixes in order:
|
|
|
|
* - The full key with SHA
|
|
|
|
* - A previous key for this Job + matrix
|
|
|
|
* - Any previous key for this Job (any matrix)
|
|
|
|
* - Any previous key for this cache on the current OS
|
|
|
|
*/
|
2021-10-30 13:15:20 +00:00
|
|
|
function generateCacheKey(cacheName: string): CacheKey {
|
2021-11-28 17:19:56 +00:00
|
|
|
const cacheKeyBase = `${getCacheKeyPrefix()}${CACHE_PROTOCOL_VERSION}${cacheName}`
|
2021-10-30 13:15:20 +00:00
|
|
|
|
|
|
|
// At the most general level, share caches for all executions on the same OS
|
|
|
|
const runnerOs = process.env['RUNNER_OS'] || ''
|
2021-11-28 17:19:56 +00:00
|
|
|
const cacheKeyForOs = `${cacheKeyBase}|${runnerOs}`
|
2021-10-30 13:15:20 +00:00
|
|
|
|
|
|
|
// Prefer caches that run this job
|
|
|
|
const cacheKeyForJob = `${cacheKeyForOs}|${github.context.job}`
|
|
|
|
|
|
|
|
// Prefer (even more) jobs that run this job with the same context (matrix)
|
|
|
|
const cacheKeyForJobContext = `${cacheKeyForJob}[${determineJobContext()}]`
|
|
|
|
|
|
|
|
// Exact match on Git SHA
|
|
|
|
const cacheKey = `${cacheKeyForJobContext}-${github.context.sha}`
|
|
|
|
|
|
|
|
return new CacheKey(cacheKey, [cacheKeyForJobContext, cacheKeyForJob, cacheKeyForOs])
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class AbstractCache {
|
|
|
|
private cacheName: string
|
|
|
|
private cacheDescription: string
|
|
|
|
private cacheKeyStateKey: string
|
|
|
|
private cacheResultStateKey: string
|
|
|
|
|
2021-12-07 23:52:53 +00:00
|
|
|
protected readonly gradleUserHome: string
|
2021-10-30 13:15:20 +00:00
|
|
|
protected readonly cacheDebuggingEnabled: boolean
|
|
|
|
|
2021-12-07 23:52:53 +00:00
|
|
|
constructor(gradleUserHome: string, cacheName: string, cacheDescription: string) {
|
|
|
|
this.gradleUserHome = gradleUserHome
|
2021-10-30 13:15:20 +00:00
|
|
|
this.cacheName = cacheName
|
|
|
|
this.cacheDescription = cacheDescription
|
|
|
|
this.cacheKeyStateKey = `CACHE_KEY_${cacheName}`
|
|
|
|
this.cacheResultStateKey = `CACHE_RESULT_${cacheName}`
|
|
|
|
this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
|
|
|
|
}
|
|
|
|
|
2021-12-07 23:52:53 +00:00
|
|
|
init(): void {
|
|
|
|
const actionCacheDir = path.resolve(this.gradleUserHome, '.gradle-build-action')
|
|
|
|
fs.mkdirSync(actionCacheDir, {recursive: true})
|
|
|
|
|
|
|
|
const initScriptsDir = path.resolve(this.gradleUserHome, 'init.d')
|
|
|
|
fs.mkdirSync(initScriptsDir, {recursive: true})
|
|
|
|
|
|
|
|
this.initializeGradleUserHome(this.gradleUserHome, initScriptsDir)
|
|
|
|
}
|
|
|
|
|
2021-11-28 17:19:56 +00:00
|
|
|
/**
|
|
|
|
* Restores the cache entry, finding the closest match to the currently running job.
|
|
|
|
*/
|
2021-10-30 13:21:27 +00:00
|
|
|
async restore(listener: CacheListener): Promise<void> {
|
2021-11-28 17:19:56 +00:00
|
|
|
const entryListener = listener.entry(this.cacheDescription)
|
2021-10-30 13:15:20 +00:00
|
|
|
|
|
|
|
const cacheKey = this.prepareCacheKey()
|
|
|
|
|
|
|
|
this.debug(
|
|
|
|
`Requesting ${this.cacheDescription} with
|
2021-12-07 19:56:36 +00:00
|
|
|
key:${cacheKey.key}
|
|
|
|
restoreKeys:[${cacheKey.restoreKeys}]`
|
2021-10-30 13:15:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const cacheResult = await this.restoreCache(this.getCachePath(), cacheKey.key, cacheKey.restoreKeys)
|
2021-11-28 17:19:56 +00:00
|
|
|
entryListener.markRequested(cacheKey.key, cacheKey.restoreKeys)
|
2021-10-30 13:15:20 +00:00
|
|
|
|
|
|
|
if (!cacheResult) {
|
2021-11-27 23:07:07 +00:00
|
|
|
core.info(`${this.cacheDescription} cache not found. Will initialize empty.`)
|
2021-10-30 13:15:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-01 02:35:28 +00:00
|
|
|
core.saveState(this.cacheResultStateKey, cacheResult.key)
|
2021-11-28 17:19:56 +00:00
|
|
|
entryListener.markRestored(cacheResult.key, cacheResult.size)
|
|
|
|
|
2021-11-01 02:35:28 +00:00
|
|
|
core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult.key}`)
|
2021-10-30 13:15:20 +00:00
|
|
|
|
|
|
|
try {
|
2021-10-30 13:21:27 +00:00
|
|
|
await this.afterRestore(listener)
|
2021-10-30 13:15:20 +00:00
|
|
|
} catch (error) {
|
|
|
|
core.warning(`Restore ${this.cacheDescription} failed in 'afterRestore': ${error}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareCacheKey(): CacheKey {
|
|
|
|
const cacheKey = generateCacheKey(this.cacheName)
|
|
|
|
core.saveState(this.cacheKeyStateKey, cacheKey.key)
|
|
|
|
return cacheKey
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async restoreCache(
|
|
|
|
cachePath: string[],
|
|
|
|
cacheKey: string,
|
|
|
|
cacheRestoreKeys: string[] = []
|
2021-10-30 18:17:41 +00:00
|
|
|
): Promise<cache.CacheEntry | undefined> {
|
2021-10-30 13:15:20 +00:00
|
|
|
try {
|
|
|
|
return await cache.restoreCache(cachePath, cacheKey, cacheRestoreKeys)
|
|
|
|
} catch (error) {
|
2021-11-05 12:54:31 +00:00
|
|
|
handleCacheFailure(error, `Failed to restore ${cacheKey}`)
|
2021-10-30 13:15:20 +00:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 13:21:27 +00:00
|
|
|
protected async afterRestore(_listener: CacheListener): Promise<void> {}
|
2021-10-30 13:15:20 +00:00
|
|
|
|
2021-11-28 17:19:56 +00:00
|
|
|
/**
|
2021-12-07 19:56:36 +00:00
|
|
|
* Saves the cache entry based on the current cache key unless the cache was restored with the exact key,
|
|
|
|
* in which case we cannot overwrite it.
|
2021-11-28 17:19:56 +00:00
|
|
|
*
|
|
|
|
* If the cache entry was restored with a partial match on a restore key, then
|
|
|
|
* it is saved with the exact key.
|
|
|
|
*/
|
2021-10-30 13:21:27 +00:00
|
|
|
async save(listener: CacheListener): Promise<void> {
|
2021-11-28 17:19:56 +00:00
|
|
|
// Retrieve the state set in the previous 'restore' step.
|
|
|
|
const cacheKeyFromRestore = core.getState(this.cacheKeyStateKey)
|
|
|
|
const cacheResultFromRestore = core.getState(this.cacheResultStateKey)
|
2021-10-30 13:15:20 +00:00
|
|
|
|
2021-11-28 17:19:56 +00:00
|
|
|
if (cacheResultFromRestore && cacheKeyFromRestore === cacheResultFromRestore) {
|
|
|
|
core.info(`Cache hit occurred on the cache key ${cacheKeyFromRestore}, not saving cache.`)
|
2021-10-30 13:15:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-10-30 13:21:27 +00:00
|
|
|
await this.beforeSave(listener)
|
2021-10-30 13:15:20 +00:00
|
|
|
} catch (error) {
|
|
|
|
core.warning(`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-28 17:19:56 +00:00
|
|
|
core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKeyFromRestore}`)
|
2021-10-30 13:15:20 +00:00
|
|
|
const cachePath = this.getCachePath()
|
2021-11-28 17:19:56 +00:00
|
|
|
const savedEntry = await this.saveCache(cachePath, cacheKeyFromRestore)
|
2021-10-30 13:15:20 +00:00
|
|
|
|
2021-10-30 18:17:41 +00:00
|
|
|
if (savedEntry) {
|
|
|
|
listener.entry(this.cacheDescription).markSaved(savedEntry.key, savedEntry.size)
|
|
|
|
}
|
2021-10-30 13:15:20 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-30 13:21:27 +00:00
|
|
|
protected async beforeSave(_listener: CacheListener): Promise<void> {}
|
2021-10-30 13:15:20 +00:00
|
|
|
|
2021-10-30 18:17:41 +00:00
|
|
|
protected async saveCache(cachePath: string[], cacheKey: string): Promise<cache.CacheEntry | undefined> {
|
2021-10-30 13:15:20 +00:00
|
|
|
try {
|
2021-10-30 18:17:41 +00:00
|
|
|
return await cache.saveCache(cachePath, cacheKey)
|
2021-10-30 13:15:20 +00:00
|
|
|
} catch (error) {
|
2021-11-05 12:54:31 +00:00
|
|
|
handleCacheFailure(error, `Failed to save cache entry ${cacheKey}`)
|
2021-10-30 13:15:20 +00:00
|
|
|
}
|
2021-10-30 18:17:41 +00:00
|
|
|
return undefined
|
2021-10-30 13:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected debug(message: string): void {
|
|
|
|
if (this.cacheDebuggingEnabled) {
|
|
|
|
core.info(message)
|
|
|
|
} else {
|
|
|
|
core.debug(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract getCachePath(): string[]
|
2021-12-07 23:52:53 +00:00
|
|
|
protected abstract initializeGradleUserHome(gradleUserHome: string, initScriptsDir: string): void
|
2021-10-30 13:15:20 +00:00
|
|
|
}
|