2021-08-24 14:57:17 -04:00
|
|
|
import * as core from '@actions/core'
|
2021-09-06 13:16:08 -04:00
|
|
|
import * as cache from '@actions/cache'
|
2021-09-05 19:10:47 -04:00
|
|
|
import * as github from '@actions/github'
|
2021-09-05 23:35:17 -04:00
|
|
|
import * as crypto from 'crypto'
|
2021-09-27 23:05:17 -04:00
|
|
|
import * as path from 'path'
|
2021-10-04 17:59:08 -04:00
|
|
|
import * as fs from 'fs'
|
2021-08-24 14:57:17 -04:00
|
|
|
|
2021-09-12 16:26:38 -04:00
|
|
|
export function isCacheDisabled(): boolean {
|
|
|
|
return core.getBooleanInput('cache-disabled')
|
2021-08-24 14:57:17 -04:00
|
|
|
}
|
|
|
|
|
2021-09-12 16:26:38 -04:00
|
|
|
export function isCacheReadOnly(): boolean {
|
|
|
|
return core.getBooleanInput('cache-read-only')
|
2021-08-24 14:57:17 -04:00
|
|
|
}
|
2021-09-05 19:10:47 -04:00
|
|
|
|
2021-09-12 16:08:22 -04:00
|
|
|
export function isCacheDebuggingEnabled(): boolean {
|
|
|
|
return process.env['CACHE_DEBUG_ENABLED'] ? true : false
|
|
|
|
}
|
|
|
|
|
2021-10-16 11:44:35 -04:00
|
|
|
export function getCacheKeyPrefix(): string {
|
2021-10-16 10:33:42 -04:00
|
|
|
// Prefix can be used to force change all cache keys (defaults to cache protocol version)
|
2021-10-20 17:00:32 -04:00
|
|
|
return process.env['CACHE_KEY_PREFIX'] || 'v3-'
|
2021-10-16 11:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function generateCacheKey(cacheName: string): CacheKey {
|
|
|
|
const cacheKeyPrefix = getCacheKeyPrefix()
|
2021-09-05 19:10:47 -04:00
|
|
|
|
2021-09-05 23:35:17 -04:00
|
|
|
// 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}`
|
2021-09-05 19:10:47 -04:00
|
|
|
|
2021-09-05 23:35:17 -04: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
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
function determineJobContext(): string {
|
2021-09-07 17:13:16 -04:00
|
|
|
// By default, we hash the full `matrix` data for the run, to uniquely identify this job invocation
|
|
|
|
const workflowJobContext = core.getInput('workflow-job-context')
|
|
|
|
return hashStrings([workflowJobContext])
|
2021-09-05 19:10:47 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 23:35:17 -04:00
|
|
|
export function hashStrings(values: string[]): string {
|
|
|
|
const hash = crypto.createHash('md5')
|
|
|
|
for (const value of values) {
|
|
|
|
hash.update(value)
|
|
|
|
}
|
|
|
|
return hash.digest('hex')
|
2021-09-05 19:10:47 -04:00
|
|
|
}
|
|
|
|
|
2021-09-27 23:05:17 -04:00
|
|
|
export function hashFileNames(fileNames: string[]): string {
|
|
|
|
return hashStrings(
|
|
|
|
fileNames.map(x => x.replace(new RegExp(`\\${path.sep}`, 'g'), '/'))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-04 17:59:08 -04:00
|
|
|
/**
|
2021-10-15 16:54:29 -04:00
|
|
|
* Attempt to delete a file or directory, waiting to allow locks to be released
|
2021-10-04 17:59:08 -04:00
|
|
|
*/
|
|
|
|
export async function tryDelete(file: string): Promise<void> {
|
2021-10-15 16:54:29 -04:00
|
|
|
const stat = fs.lstatSync(file)
|
2021-10-04 17:59:08 -04:00
|
|
|
for (let count = 0; count < 3; count++) {
|
|
|
|
try {
|
2021-10-15 16:54:29 -04:00
|
|
|
if (stat.isDirectory()) {
|
|
|
|
fs.rmdirSync(file, {recursive: true})
|
|
|
|
} else {
|
|
|
|
fs.unlinkSync(file)
|
|
|
|
}
|
2021-10-04 17:59:08 -04:00
|
|
|
return
|
|
|
|
} catch (error) {
|
|
|
|
if (count === 2) {
|
|
|
|
throw error
|
|
|
|
} else {
|
|
|
|
core.warning(String(error))
|
|
|
|
await delay(1000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function delay(ms: number): Promise<void> {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms))
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:16:08 -04:00
|
|
|
class CacheKey {
|
2021-09-05 19:10:47 -04:00
|
|
|
key: string
|
|
|
|
restoreKeys: string[]
|
|
|
|
|
|
|
|
constructor(key: string, restoreKeys: string[]) {
|
|
|
|
this.key = key
|
|
|
|
this.restoreKeys = restoreKeys
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 13:16:08 -04:00
|
|
|
|
|
|
|
export abstract class AbstractCache {
|
|
|
|
private cacheName: string
|
|
|
|
private cacheDescription: string
|
|
|
|
private cacheKeyStateKey: string
|
|
|
|
private cacheResultStateKey: string
|
|
|
|
|
2021-09-12 16:08:22 -04:00
|
|
|
protected readonly cacheDebuggingEnabled: boolean
|
|
|
|
|
2021-09-06 13:16:08 -04:00
|
|
|
constructor(cacheName: string, cacheDescription: string) {
|
|
|
|
this.cacheName = cacheName
|
|
|
|
this.cacheDescription = cacheDescription
|
|
|
|
this.cacheKeyStateKey = `CACHE_KEY_${cacheName}`
|
|
|
|
this.cacheResultStateKey = `CACHE_RESULT_${cacheName}`
|
2021-09-12 16:08:22 -04:00
|
|
|
this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
|
2021-09-06 13:16:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async restore(): Promise<void> {
|
|
|
|
if (this.cacheOutputExists()) {
|
|
|
|
core.info(
|
|
|
|
`${this.cacheDescription} already exists. Not restoring from cache.`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheKey = generateCacheKey(this.cacheName)
|
|
|
|
|
|
|
|
core.saveState(this.cacheKeyStateKey, cacheKey.key)
|
|
|
|
|
2021-10-15 13:11:05 -04:00
|
|
|
this.debug(
|
|
|
|
`Requesting ${this.cacheDescription} with
|
|
|
|
key:${cacheKey.key}
|
|
|
|
restoreKeys:[${cacheKey.restoreKeys}]`
|
|
|
|
)
|
|
|
|
|
2021-09-14 15:30:23 -04:00
|
|
|
const cacheResult = await this.restoreCache(
|
2021-09-06 13:16:08 -04:00
|
|
|
this.getCachePath(),
|
|
|
|
cacheKey.key,
|
|
|
|
cacheKey.restoreKeys
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!cacheResult) {
|
|
|
|
core.info(
|
|
|
|
`${this.cacheDescription} cache not found. Will start with empty.`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
core.saveState(this.cacheResultStateKey, cacheResult)
|
|
|
|
|
|
|
|
core.info(
|
2021-09-15 17:48:55 -04:00
|
|
|
`Restored ${this.cacheDescription} from cache key: ${cacheResult}`
|
2021-09-06 13:16:08 -04:00
|
|
|
)
|
2021-09-15 13:20:33 -04:00
|
|
|
|
2021-10-14 14:19:24 -04:00
|
|
|
try {
|
|
|
|
await this.afterRestore()
|
|
|
|
} catch (error) {
|
|
|
|
core.warning(
|
|
|
|
`Restore ${this.cacheDescription} failed in 'afterRestore': ${error}`
|
|
|
|
)
|
|
|
|
}
|
2021-09-15 13:20:33 -04:00
|
|
|
|
2021-09-06 13:16:08 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:30:23 -04:00
|
|
|
protected async restoreCache(
|
|
|
|
cachePath: string[],
|
|
|
|
cacheKey: string,
|
|
|
|
cacheRestoreKeys: string[] = []
|
|
|
|
): Promise<string | undefined> {
|
|
|
|
try {
|
|
|
|
return await cache.restoreCache(
|
|
|
|
cachePath,
|
|
|
|
cacheKey,
|
|
|
|
cacheRestoreKeys
|
|
|
|
)
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof cache.ValidationError) {
|
|
|
|
// Validation errors should fail the build action
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
// Warn about any other error and continue
|
|
|
|
core.warning(`Failed to restore ${cacheKey}: ${error}`)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 13:20:33 -04:00
|
|
|
protected async afterRestore(): Promise<void> {}
|
|
|
|
|
2021-09-06 13:16:08 -04:00
|
|
|
async save(): Promise<void> {
|
|
|
|
if (!this.cacheOutputExists()) {
|
2021-09-12 16:08:22 -04:00
|
|
|
this.debug(`No ${this.cacheDescription} to cache.`)
|
2021-09-06 13:16:08 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheKey = core.getState(this.cacheKeyStateKey)
|
|
|
|
const cacheResult = core.getState(this.cacheResultStateKey)
|
|
|
|
|
|
|
|
if (!cacheKey) {
|
2021-09-12 16:08:22 -04:00
|
|
|
this.debug(
|
2021-09-06 13:16:08 -04:00
|
|
|
`${this.cacheDescription} existed prior to cache restore. Not saving.`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cacheResult && cacheKey === cacheResult) {
|
|
|
|
core.info(
|
|
|
|
`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:19:24 -04:00
|
|
|
try {
|
|
|
|
await this.beforeSave()
|
|
|
|
} catch (error) {
|
|
|
|
core.warning(
|
|
|
|
`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
2021-09-15 13:20:33 -04:00
|
|
|
|
2021-09-06 13:16:08 -04:00
|
|
|
core.info(
|
|
|
|
`Caching ${this.cacheDescription} with cache key: ${cacheKey}`
|
|
|
|
)
|
2021-09-14 15:30:23 -04:00
|
|
|
const cachePath = this.getCachePath()
|
|
|
|
await this.saveCache(cachePath, cacheKey)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-15 13:20:33 -04:00
|
|
|
protected async beforeSave(): Promise<void> {}
|
|
|
|
|
2021-09-14 15:30:23 -04:00
|
|
|
protected async saveCache(
|
|
|
|
cachePath: string[],
|
|
|
|
cacheKey: string
|
|
|
|
): Promise<void> {
|
2021-09-06 13:16:08 -04:00
|
|
|
try {
|
2021-09-14 15:30:23 -04:00
|
|
|
await cache.saveCache(cachePath, cacheKey)
|
2021-09-06 13:16:08 -04:00
|
|
|
} catch (error) {
|
2021-09-14 15:30:23 -04:00
|
|
|
if (error instanceof cache.ValidationError) {
|
|
|
|
// Validation errors should fail the build action
|
2021-09-06 13:16:08 -04:00
|
|
|
throw error
|
2021-09-14 15:30:23 -04:00
|
|
|
} else if (error instanceof cache.ReserveCacheError) {
|
|
|
|
// Reserve cache errors are expected if the artifact has been previously cached
|
|
|
|
this.debug(error.message)
|
|
|
|
} else {
|
|
|
|
// Warn about any other error and continue
|
|
|
|
core.warning(String(error))
|
2021-09-06 13:16:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 16:08:22 -04:00
|
|
|
protected debug(message: string): void {
|
|
|
|
if (this.cacheDebuggingEnabled) {
|
|
|
|
core.info(message)
|
|
|
|
} else {
|
|
|
|
core.debug(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:16:08 -04:00
|
|
|
protected abstract cacheOutputExists(): boolean
|
|
|
|
protected abstract getCachePath(): string[]
|
|
|
|
}
|