2021-10-30 07:15:20 -06:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
import * as cache from '@actions/cache'
|
|
|
|
import * as github from '@actions/github'
|
2021-11-05 06:54:31 -06:00
|
|
|
import {isCacheDebuggingEnabled, getCacheKeyPrefix, hashStrings, handleCacheFailure} from './cache-utils'
|
2021-10-30 07:15:20 -06:00
|
|
|
|
|
|
|
const JOB_CONTEXT_PARAMETER = 'workflow-job-context'
|
|
|
|
|
|
|
|
function generateCacheKey(cacheName: string): CacheKey {
|
|
|
|
const cacheKeyPrefix = getCacheKeyPrefix()
|
|
|
|
|
|
|
|
// At the most general level, share caches for all executions on the same OS
|
|
|
|
const runnerOs = process.env['RUNNER_OS'] || ''
|
|
|
|
const cacheKeyForOs = `${cacheKeyPrefix}${cacheName}|${runnerOs}`
|
|
|
|
|
|
|
|
// 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])
|
|
|
|
}
|
|
|
|
|
|
|
|
function determineJobContext(): string {
|
|
|
|
// By default, we hash the full `matrix` data for the run, to uniquely identify this job invocation
|
|
|
|
const workflowJobContext = core.getInput(JOB_CONTEXT_PARAMETER)
|
|
|
|
return hashStrings([workflowJobContext])
|
|
|
|
}
|
|
|
|
|
|
|
|
class CacheKey {
|
|
|
|
key: string
|
|
|
|
restoreKeys: string[]
|
|
|
|
|
|
|
|
constructor(key: string, restoreKeys: string[]) {
|
|
|
|
this.key = key
|
|
|
|
this.restoreKeys = restoreKeys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
export class CacheListener {
|
|
|
|
cacheEntries: CacheEntryListener[] = []
|
2021-10-30 07:15:20 -06:00
|
|
|
|
|
|
|
get fullyRestored(): boolean {
|
2021-10-30 07:21:27 -06:00
|
|
|
return this.cacheEntries.every(x => !x.wasRequestedButNotRestored())
|
2021-10-30 07:15:20 -06:00
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
entry(name: string): CacheEntryListener {
|
|
|
|
for (const entry of this.cacheEntries) {
|
|
|
|
if (entry.entryName === name) {
|
|
|
|
return entry
|
2021-10-30 07:15:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
const newEntry = new CacheEntryListener(name)
|
|
|
|
this.cacheEntries.push(newEntry)
|
|
|
|
return newEntry
|
2021-10-30 07:15:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
stringify(): string {
|
|
|
|
return JSON.stringify(this)
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
static rehydrate(stringRep: string): CacheListener {
|
|
|
|
const rehydrated: CacheListener = Object.assign(new CacheListener(), JSON.parse(stringRep))
|
|
|
|
const entries = rehydrated.cacheEntries
|
|
|
|
for (let index = 0; index < entries.length; index++) {
|
|
|
|
const rawEntry = entries[index]
|
|
|
|
entries[index] = Object.assign(new CacheEntryListener(rawEntry.entryName), rawEntry)
|
2021-10-30 07:15:20 -06:00
|
|
|
}
|
|
|
|
return rehydrated
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
export class CacheEntryListener {
|
2021-10-30 07:15:20 -06:00
|
|
|
entryName: string
|
|
|
|
requestedKey: string | undefined
|
|
|
|
requestedRestoreKeys: string[] | undefined
|
|
|
|
restoredKey: string | undefined
|
|
|
|
restoredSize: number | undefined
|
|
|
|
|
|
|
|
savedKey: string | undefined
|
|
|
|
savedSize: number | undefined
|
|
|
|
|
|
|
|
constructor(entryName: string) {
|
|
|
|
this.entryName = entryName
|
|
|
|
}
|
|
|
|
|
|
|
|
wasRequestedButNotRestored(): boolean {
|
|
|
|
return this.requestedKey !== undefined && this.restoredKey === undefined
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
markRequested(key: string, restoreKeys: string[] = []): CacheEntryListener {
|
2021-10-30 07:15:20 -06:00
|
|
|
this.requestedKey = key
|
|
|
|
this.requestedRestoreKeys = restoreKeys
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2021-10-30 12:17:41 -06:00
|
|
|
markRestored(key: string, size: number | undefined): CacheEntryListener {
|
2021-10-30 07:15:20 -06:00
|
|
|
this.restoredKey = key
|
2021-10-30 12:17:41 -06:00
|
|
|
this.restoredSize = size
|
2021-10-30 07:15:20 -06:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2021-10-30 12:17:41 -06:00
|
|
|
markSaved(key: string, size: number | undefined): CacheEntryListener {
|
2021-10-30 07:15:20 -06:00
|
|
|
this.savedKey = key
|
2021-10-30 12:17:41 -06:00
|
|
|
this.savedSize = size
|
2021-10-30 07:15:20 -06:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class AbstractCache {
|
|
|
|
private cacheName: string
|
|
|
|
private cacheDescription: string
|
|
|
|
private cacheKeyStateKey: string
|
|
|
|
private cacheResultStateKey: string
|
|
|
|
|
|
|
|
protected readonly cacheDebuggingEnabled: boolean
|
|
|
|
|
|
|
|
constructor(cacheName: string, cacheDescription: string) {
|
|
|
|
this.cacheName = cacheName
|
|
|
|
this.cacheDescription = cacheDescription
|
|
|
|
this.cacheKeyStateKey = `CACHE_KEY_${cacheName}`
|
|
|
|
this.cacheResultStateKey = `CACHE_RESULT_${cacheName}`
|
|
|
|
this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
async restore(listener: CacheListener): Promise<void> {
|
2021-10-30 07:15:20 -06:00
|
|
|
if (this.cacheOutputExists()) {
|
|
|
|
core.info(`${this.cacheDescription} already exists. Not restoring from cache.`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheKey = this.prepareCacheKey()
|
2021-10-30 07:39:21 -06:00
|
|
|
const entryReport = listener.entry(this.cacheDescription)
|
2021-10-30 07:15:20 -06:00
|
|
|
entryReport.markRequested(cacheKey.key, cacheKey.restoreKeys)
|
|
|
|
|
|
|
|
this.debug(
|
|
|
|
`Requesting ${this.cacheDescription} with
|
|
|
|
key:${cacheKey.key}
|
|
|
|
restoreKeys:[${cacheKey.restoreKeys}]`
|
|
|
|
)
|
|
|
|
|
|
|
|
const cacheResult = await this.restoreCache(this.getCachePath(), cacheKey.key, cacheKey.restoreKeys)
|
|
|
|
|
|
|
|
if (!cacheResult) {
|
|
|
|
core.info(`${this.cacheDescription} cache not found. Will start with empty.`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-31 20:35:28 -06:00
|
|
|
core.saveState(this.cacheResultStateKey, cacheResult.key)
|
2021-10-30 12:17:41 -06:00
|
|
|
entryReport.markRestored(cacheResult.key, cacheResult.size)
|
2021-10-31 20:35:28 -06:00
|
|
|
core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult.key}`)
|
2021-10-30 07:15:20 -06:00
|
|
|
|
|
|
|
try {
|
2021-10-30 07:21:27 -06:00
|
|
|
await this.afterRestore(listener)
|
2021-10-30 07:15:20 -06: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 12:17:41 -06:00
|
|
|
): Promise<cache.CacheEntry | undefined> {
|
2021-10-30 07:15:20 -06:00
|
|
|
try {
|
|
|
|
return await cache.restoreCache(cachePath, cacheKey, cacheRestoreKeys)
|
|
|
|
} catch (error) {
|
2021-11-05 06:54:31 -06:00
|
|
|
handleCacheFailure(error, `Failed to restore ${cacheKey}`)
|
2021-10-30 07:15:20 -06:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
protected async afterRestore(_listener: CacheListener): Promise<void> {}
|
2021-10-30 07:15:20 -06:00
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
async save(listener: CacheListener): Promise<void> {
|
2021-10-30 07:15:20 -06:00
|
|
|
if (!this.cacheOutputExists()) {
|
2021-11-05 08:35:45 -06:00
|
|
|
core.info(`No ${this.cacheDescription} to cache.`)
|
2021-10-30 07:15:20 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheKey = core.getState(this.cacheKeyStateKey)
|
|
|
|
const cacheResult = core.getState(this.cacheResultStateKey)
|
|
|
|
|
|
|
|
if (!cacheKey) {
|
2021-11-05 08:35:45 -06:00
|
|
|
core.info(`${this.cacheDescription} existed prior to cache restore. Not saving.`)
|
2021-10-30 07:15:20 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cacheResult && cacheKey === cacheResult) {
|
|
|
|
core.info(`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-10-30 07:21:27 -06:00
|
|
|
await this.beforeSave(listener)
|
2021-10-30 07:15:20 -06:00
|
|
|
} catch (error) {
|
|
|
|
core.warning(`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKey}`)
|
|
|
|
const cachePath = this.getCachePath()
|
2021-10-30 12:17:41 -06:00
|
|
|
const savedEntry = await this.saveCache(cachePath, cacheKey)
|
2021-10-30 07:15:20 -06:00
|
|
|
|
2021-10-30 12:17:41 -06:00
|
|
|
if (savedEntry) {
|
|
|
|
listener.entry(this.cacheDescription).markSaved(savedEntry.key, savedEntry.size)
|
|
|
|
}
|
2021-10-30 07:15:20 -06:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
protected async beforeSave(_listener: CacheListener): Promise<void> {}
|
2021-10-30 07:15:20 -06:00
|
|
|
|
2021-10-30 12:17:41 -06:00
|
|
|
protected async saveCache(cachePath: string[], cacheKey: string): Promise<cache.CacheEntry | undefined> {
|
2021-10-30 07:15:20 -06:00
|
|
|
try {
|
2021-10-30 12:17:41 -06:00
|
|
|
return await cache.saveCache(cachePath, cacheKey)
|
2021-10-30 07:15:20 -06:00
|
|
|
} catch (error) {
|
2021-11-05 06:54:31 -06:00
|
|
|
handleCacheFailure(error, `Failed to save cache entry ${cacheKey}`)
|
2021-10-30 07:15:20 -06:00
|
|
|
}
|
2021-10-30 12:17:41 -06:00
|
|
|
return undefined
|
2021-10-30 07:15:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
protected debug(message: string): void {
|
|
|
|
if (this.cacheDebuggingEnabled) {
|
|
|
|
core.info(message)
|
|
|
|
} else {
|
|
|
|
core.debug(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract cacheOutputExists(): boolean
|
|
|
|
protected abstract getCachePath(): string[]
|
|
|
|
}
|