2021-08-24 12:57:17 -06:00
|
|
|
import * as core from '@actions/core'
|
2021-09-06 11:16:08 -06:00
|
|
|
import * as cache from '@actions/cache'
|
2021-09-05 17:10:47 -06:00
|
|
|
import * as github from '@actions/github'
|
2021-09-05 21:35:17 -06:00
|
|
|
import * as crypto from 'crypto'
|
2021-09-27 21:05:17 -06:00
|
|
|
import * as path from 'path'
|
2021-10-04 23:59:08 +02:00
|
|
|
import * as fs from 'fs'
|
2021-08-24 12:57:17 -06:00
|
|
|
|
2021-10-29 07:44:45 -06:00
|
|
|
const CACHE_PROTOCOL_VERSION = 'v4-'
|
|
|
|
|
2021-10-27 16:05:07 -06:00
|
|
|
const CACHE_DISABLED_PARAMETER = 'cache-disabled'
|
|
|
|
const CACHE_READONLY_PARAMETER = 'cache-read-only'
|
|
|
|
const JOB_CONTEXT_PARAMETER = 'workflow-job-context'
|
|
|
|
const CACHE_DEBUG_VAR = 'GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED'
|
|
|
|
const CACHE_PREFIX_VAR = 'GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX'
|
|
|
|
|
2021-09-12 14:26:38 -06:00
|
|
|
export function isCacheDisabled(): boolean {
|
2021-10-27 16:05:07 -06:00
|
|
|
return core.getBooleanInput(CACHE_DISABLED_PARAMETER)
|
2021-08-24 12:57:17 -06:00
|
|
|
}
|
|
|
|
|
2021-09-12 14:26:38 -06:00
|
|
|
export function isCacheReadOnly(): boolean {
|
2021-10-27 16:05:07 -06:00
|
|
|
return core.getBooleanInput(CACHE_READONLY_PARAMETER)
|
2021-08-24 12:57:17 -06:00
|
|
|
}
|
2021-09-05 17:10:47 -06:00
|
|
|
|
2021-09-12 14:08:22 -06:00
|
|
|
export function isCacheDebuggingEnabled(): boolean {
|
2021-10-27 16:05:07 -06:00
|
|
|
return process.env[CACHE_DEBUG_VAR] ? true : false
|
2021-09-12 14:08:22 -06:00
|
|
|
}
|
|
|
|
|
2021-10-16 09:44:35 -06:00
|
|
|
export function getCacheKeyPrefix(): string {
|
2021-10-16 08:33:42 -06:00
|
|
|
// Prefix can be used to force change all cache keys (defaults to cache protocol version)
|
2021-10-29 07:44:45 -06:00
|
|
|
return process.env[CACHE_PREFIX_VAR] || CACHE_PROTOCOL_VERSION
|
2021-10-16 09:44:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function generateCacheKey(cacheName: string): CacheKey {
|
|
|
|
const cacheKeyPrefix = getCacheKeyPrefix()
|
2021-09-05 17:10:47 -06:00
|
|
|
|
2021-09-05 21:35:17 -06: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 17:10:47 -06:00
|
|
|
|
2021-09-05 21:35:17 -06: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}`
|
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
return new CacheKey(cacheKey, [cacheKeyForJobContext, cacheKeyForJob, cacheKeyForOs])
|
2021-09-05 21:35:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function determineJobContext(): string {
|
2021-09-07 15:13:16 -06:00
|
|
|
// By default, we hash the full `matrix` data for the run, to uniquely identify this job invocation
|
2021-10-27 16:05:07 -06:00
|
|
|
const workflowJobContext = core.getInput(JOB_CONTEXT_PARAMETER)
|
2021-09-07 15:13:16 -06:00
|
|
|
return hashStrings([workflowJobContext])
|
2021-09-05 17:10:47 -06:00
|
|
|
}
|
|
|
|
|
2021-09-05 21:35:17 -06: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 17:10:47 -06:00
|
|
|
}
|
|
|
|
|
2021-09-27 21:05:17 -06:00
|
|
|
export function hashFileNames(fileNames: string[]): string {
|
2021-10-29 07:34:44 -06:00
|
|
|
return hashStrings(fileNames.map(x => x.replace(new RegExp(`\\${path.sep}`, 'g'), '/')))
|
2021-09-27 21:05:17 -06:00
|
|
|
}
|
|
|
|
|
2021-10-04 23:59:08 +02:00
|
|
|
/**
|
2021-10-15 14:54:29 -06:00
|
|
|
* Attempt to delete a file or directory, waiting to allow locks to be released
|
2021-10-04 23:59:08 +02:00
|
|
|
*/
|
|
|
|
export async function tryDelete(file: string): Promise<void> {
|
2021-10-15 14:54:29 -06:00
|
|
|
const stat = fs.lstatSync(file)
|
2021-10-04 23:59:08 +02:00
|
|
|
for (let count = 0; count < 3; count++) {
|
|
|
|
try {
|
2021-10-15 14:54:29 -06:00
|
|
|
if (stat.isDirectory()) {
|
|
|
|
fs.rmdirSync(file, {recursive: true})
|
|
|
|
} else {
|
|
|
|
fs.unlinkSync(file)
|
|
|
|
}
|
2021-10-04 23:59:08 +02: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 11:16:08 -06:00
|
|
|
class CacheKey {
|
2021-09-05 17:10:47 -06:00
|
|
|
key: string
|
|
|
|
restoreKeys: string[]
|
|
|
|
|
|
|
|
constructor(key: string, restoreKeys: string[]) {
|
|
|
|
this.key = key
|
|
|
|
this.restoreKeys = restoreKeys
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 11:16:08 -06:00
|
|
|
|
2021-10-29 08:44:08 -06:00
|
|
|
export class CachingReport {
|
2021-10-29 10:19:35 -06:00
|
|
|
cacheEntryReports: CacheEntryReport[] = []
|
2021-10-29 08:44:08 -06:00
|
|
|
|
2021-10-29 10:19:35 -06:00
|
|
|
get fullyRestored(): boolean {
|
|
|
|
return this.cacheEntryReports.every(x => !x.wasRequestedButNotRestored())
|
|
|
|
}
|
|
|
|
|
|
|
|
addEntryReport(name: string): CacheEntryReport {
|
|
|
|
const report = new CacheEntryReport(name)
|
|
|
|
this.cacheEntryReports.push(report)
|
|
|
|
return report
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CacheEntryReport {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
markRequested(key: string, restoreKeys: string[] = []): void {
|
|
|
|
this.requestedKey = key
|
|
|
|
this.requestedRestoreKeys = restoreKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
markRestored(key: string): void {
|
|
|
|
this.restoredKey = key
|
2021-10-29 08:44:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:16:08 -06:00
|
|
|
export abstract class AbstractCache {
|
|
|
|
private cacheName: string
|
|
|
|
private cacheDescription: string
|
|
|
|
private cacheKeyStateKey: string
|
|
|
|
private cacheResultStateKey: string
|
|
|
|
|
2021-09-12 14:08:22 -06:00
|
|
|
protected readonly cacheDebuggingEnabled: boolean
|
|
|
|
|
2021-09-06 11:16:08 -06: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 14:08:22 -06:00
|
|
|
this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
|
2021-09-06 11:16:08 -06:00
|
|
|
}
|
|
|
|
|
2021-10-29 10:19:35 -06:00
|
|
|
async restore(report: CachingReport): Promise<void> {
|
2021-09-06 11:16:08 -06:00
|
|
|
if (this.cacheOutputExists()) {
|
2021-10-29 07:34:44 -06:00
|
|
|
core.info(`${this.cacheDescription} already exists. Not restoring from cache.`)
|
2021-10-29 10:19:35 -06:00
|
|
|
return
|
2021-09-06 11:16:08 -06:00
|
|
|
}
|
|
|
|
|
2021-10-29 08:44:08 -06:00
|
|
|
const cacheKey = this.prepareCacheKey()
|
2021-10-29 10:19:35 -06:00
|
|
|
const entryReport = report.addEntryReport(this.cacheName)
|
|
|
|
entryReport.markRequested(cacheKey.key, cacheKey.restoreKeys)
|
2021-09-06 11:16:08 -06:00
|
|
|
|
2021-10-15 11:11:05 -06:00
|
|
|
this.debug(
|
|
|
|
`Requesting ${this.cacheDescription} with
|
|
|
|
key:${cacheKey.key}
|
|
|
|
restoreKeys:[${cacheKey.restoreKeys}]`
|
|
|
|
)
|
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
const cacheResult = await this.restoreCache(this.getCachePath(), cacheKey.key, cacheKey.restoreKeys)
|
2021-09-06 11:16:08 -06:00
|
|
|
|
|
|
|
if (!cacheResult) {
|
2021-10-29 07:34:44 -06:00
|
|
|
core.info(`${this.cacheDescription} cache not found. Will start with empty.`)
|
2021-10-29 10:19:35 -06:00
|
|
|
return
|
2021-09-06 11:16:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
core.saveState(this.cacheResultStateKey, cacheResult)
|
2021-10-29 10:19:35 -06:00
|
|
|
entryReport.markRestored(cacheResult)
|
2021-10-29 07:34:44 -06:00
|
|
|
core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult}`)
|
2021-09-15 11:20:33 -06:00
|
|
|
|
2021-10-14 12:19:24 -06:00
|
|
|
try {
|
2021-10-29 08:44:08 -06:00
|
|
|
await this.afterRestore(report)
|
2021-10-14 12:19:24 -06:00
|
|
|
} catch (error) {
|
2021-10-29 07:34:44 -06:00
|
|
|
core.warning(`Restore ${this.cacheDescription} failed in 'afterRestore': ${error}`)
|
2021-10-14 12:19:24 -06:00
|
|
|
}
|
2021-10-29 08:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
prepareCacheKey(): CacheKey {
|
|
|
|
const cacheKey = generateCacheKey(this.cacheName)
|
|
|
|
|
|
|
|
core.saveState(this.cacheKeyStateKey, cacheKey.key)
|
|
|
|
return cacheKey
|
2021-09-06 11:16:08 -06:00
|
|
|
}
|
|
|
|
|
2021-09-14 13:30:23 -06:00
|
|
|
protected async restoreCache(
|
|
|
|
cachePath: string[],
|
|
|
|
cacheKey: string,
|
|
|
|
cacheRestoreKeys: string[] = []
|
|
|
|
): Promise<string | undefined> {
|
|
|
|
try {
|
2021-10-29 07:34:44 -06:00
|
|
|
return await cache.restoreCache(cachePath, cacheKey, cacheRestoreKeys)
|
2021-09-14 13:30:23 -06:00
|
|
|
} 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-10-29 08:44:08 -06:00
|
|
|
protected async afterRestore(_report: CachingReport): Promise<void> {}
|
2021-09-15 11:20:33 -06:00
|
|
|
|
2021-09-06 11:16:08 -06:00
|
|
|
async save(): Promise<void> {
|
|
|
|
if (!this.cacheOutputExists()) {
|
2021-09-12 14:08:22 -06:00
|
|
|
this.debug(`No ${this.cacheDescription} to cache.`)
|
2021-09-06 11:16:08 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheKey = core.getState(this.cacheKeyStateKey)
|
|
|
|
const cacheResult = core.getState(this.cacheResultStateKey)
|
|
|
|
|
|
|
|
if (!cacheKey) {
|
2021-10-29 07:34:44 -06:00
|
|
|
this.debug(`${this.cacheDescription} existed prior to cache restore. Not saving.`)
|
2021-09-06 11:16:08 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cacheResult && cacheKey === cacheResult) {
|
2021-10-29 07:34:44 -06:00
|
|
|
core.info(`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`)
|
2021-09-06 11:16:08 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-14 12:19:24 -06:00
|
|
|
try {
|
|
|
|
await this.beforeSave()
|
|
|
|
} catch (error) {
|
2021-10-29 07:34:44 -06:00
|
|
|
core.warning(`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`)
|
2021-10-14 12:19:24 -06:00
|
|
|
return
|
|
|
|
}
|
2021-09-15 11:20:33 -06:00
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKey}`)
|
2021-09-14 13:30:23 -06:00
|
|
|
const cachePath = this.getCachePath()
|
|
|
|
await this.saveCache(cachePath, cacheKey)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-15 11:20:33 -06:00
|
|
|
protected async beforeSave(): Promise<void> {}
|
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
protected async saveCache(cachePath: string[], cacheKey: string): Promise<void> {
|
2021-09-06 11:16:08 -06:00
|
|
|
try {
|
2021-09-14 13:30:23 -06:00
|
|
|
await cache.saveCache(cachePath, cacheKey)
|
2021-09-06 11:16:08 -06:00
|
|
|
} catch (error) {
|
2021-09-14 13:30:23 -06:00
|
|
|
if (error instanceof cache.ValidationError) {
|
|
|
|
// Validation errors should fail the build action
|
2021-09-06 11:16:08 -06:00
|
|
|
throw error
|
2021-09-14 13:30:23 -06: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 11:16:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 14:08:22 -06:00
|
|
|
protected debug(message: string): void {
|
|
|
|
if (this.cacheDebuggingEnabled) {
|
|
|
|
core.info(message)
|
|
|
|
} else {
|
|
|
|
core.debug(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:16:08 -06:00
|
|
|
protected abstract cacheOutputExists(): boolean
|
|
|
|
protected abstract getCachePath(): string[]
|
|
|
|
}
|