2021-08-24 14:57:17 -04:00
|
|
|
import * as core from '@actions/core'
|
2021-11-05 08:54:31 -04:00
|
|
|
import * as cache from '@actions/cache'
|
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-12-07 18:52:53 -05:00
|
|
|
const JOB_CONTEXT_PARAMETER = 'workflow-job-context'
|
2021-10-27 18:05:07 -04:00
|
|
|
const CACHE_DISABLED_PARAMETER = 'cache-disabled'
|
|
|
|
const CACHE_READONLY_PARAMETER = 'cache-read-only'
|
|
|
|
const CACHE_DEBUG_VAR = 'GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED'
|
|
|
|
const CACHE_PREFIX_VAR = 'GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX'
|
|
|
|
|
2021-09-12 16:26:38 -04:00
|
|
|
export function isCacheDisabled(): boolean {
|
2021-10-27 18:05:07 -04:00
|
|
|
return core.getBooleanInput(CACHE_DISABLED_PARAMETER)
|
2021-08-24 14:57:17 -04:00
|
|
|
}
|
|
|
|
|
2021-09-12 16:26:38 -04:00
|
|
|
export function isCacheReadOnly(): boolean {
|
2021-10-27 18:05:07 -04:00
|
|
|
return core.getBooleanInput(CACHE_READONLY_PARAMETER)
|
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 {
|
2021-10-27 18:05:07 -04:00
|
|
|
return process.env[CACHE_DEBUG_VAR] ? true : false
|
2021-09-12 16:08:22 -04:00
|
|
|
}
|
|
|
|
|
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-11-28 12:19:56 -05:00
|
|
|
return process.env[CACHE_PREFIX_VAR] || ''
|
2021-10-16 11:44:35 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 18:52:53 -05:00
|
|
|
export function determineJobContext(): string {
|
|
|
|
// By default, we hash the full `matrix` data for the run, to uniquely identify this job invocation
|
|
|
|
// The only way we can obtain the `matrix` data is via the `workflow-job-context` parameter in action.yml.
|
|
|
|
const workflowJobContext = core.getInput(JOB_CONTEXT_PARAMETER)
|
|
|
|
return hashStrings([workflowJobContext])
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hashFileNames(fileNames: string[]): string {
|
|
|
|
return hashStrings(fileNames.map(x => x.replace(new RegExp(`\\${path.sep}`, 'g'), '/')))
|
|
|
|
}
|
|
|
|
|
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-12-29 18:07:33 -05:00
|
|
|
export async function restoreCache(
|
|
|
|
cachePath: string[],
|
|
|
|
cacheKey: string,
|
|
|
|
cacheRestoreKeys: string[] = []
|
|
|
|
): Promise<cache.CacheEntry | undefined> {
|
|
|
|
try {
|
|
|
|
return await cache.restoreCache(cachePath, cacheKey, cacheRestoreKeys)
|
|
|
|
} catch (error) {
|
|
|
|
handleCacheFailure(error, `Failed to restore ${cacheKey}`)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function saveCache(cachePath: string[], cacheKey: string): Promise<cache.CacheEntry | undefined> {
|
|
|
|
try {
|
|
|
|
return await cache.saveCache(cachePath, cacheKey)
|
|
|
|
} catch (error) {
|
|
|
|
handleCacheFailure(error, `Failed to save cache entry ${cacheKey}`)
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cacheDebug(message: string): void {
|
|
|
|
if (isCacheDebuggingEnabled()) {
|
|
|
|
core.info(message)
|
|
|
|
} else {
|
|
|
|
core.debug(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 08:54:31 -04:00
|
|
|
export function handleCacheFailure(error: unknown, message: string): void {
|
|
|
|
if (error instanceof cache.ValidationError) {
|
|
|
|
// Fail on cache validation errors
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
if (error instanceof cache.ReserveCacheError) {
|
|
|
|
// Reserve cache errors are expected if the artifact has been previously cached
|
|
|
|
if (isCacheDebuggingEnabled()) {
|
2021-12-08 16:44:57 -05:00
|
|
|
core.info(`${message}: ${error}`)
|
2021-11-05 08:54:31 -04:00
|
|
|
} else {
|
2021-12-08 16:44:57 -05:00
|
|
|
core.debug(`${message}: ${error}`)
|
2021-11-05 08:54:31 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Warn on all other errors
|
|
|
|
core.warning(`${message}: ${error}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|