2020-05-06 15:10:18 +00:00
|
|
|
import * as core from '@actions/core'
|
2020-05-06 21:53:22 +00:00
|
|
|
import * as path from 'path'
|
2020-05-06 15:10:18 +00:00
|
|
|
import * as utils from './internal/cacheUtils'
|
|
|
|
import * as cacheHttpClient from './internal/cacheHttpClient'
|
2020-11-25 22:56:57 +00:00
|
|
|
import {createTar, extractTar, listTar} from './internal/tar'
|
2020-07-10 15:09:32 +00:00
|
|
|
import {DownloadOptions, UploadOptions} from './options'
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-12 18:47:31 +00:00
|
|
|
export class ValidationError extends Error {
|
|
|
|
constructor(message: string) {
|
|
|
|
super(message)
|
|
|
|
this.name = 'ValidationError'
|
2020-05-15 16:18:50 +00:00
|
|
|
Object.setPrototypeOf(this, ValidationError.prototype)
|
2020-05-12 18:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ReserveCacheError extends Error {
|
|
|
|
constructor(message: string) {
|
|
|
|
super(message)
|
|
|
|
this.name = 'ReserveCacheError'
|
2020-05-15 16:18:50 +00:00
|
|
|
Object.setPrototypeOf(this, ReserveCacheError.prototype)
|
2020-05-12 18:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
function checkPaths(paths: string[]): void {
|
|
|
|
if (!paths || paths.length === 0) {
|
2020-05-12 18:47:31 +00:00
|
|
|
throw new ValidationError(
|
2020-05-06 21:53:22 +00:00
|
|
|
`Path Validation Error: At least one directory or file path is required`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkKey(key: string): void {
|
|
|
|
if (key.length > 512) {
|
2020-05-12 18:47:31 +00:00
|
|
|
throw new ValidationError(
|
2020-05-06 21:53:22 +00:00
|
|
|
`Key Validation Error: ${key} cannot be larger than 512 characters.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const regex = /^[^,]*$/
|
|
|
|
if (!regex.test(key)) {
|
2020-05-12 18:47:31 +00:00
|
|
|
throw new ValidationError(
|
|
|
|
`Key Validation Error: ${key} cannot contain commas.`
|
|
|
|
)
|
2020-05-06 21:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 22:19:10 +00:00
|
|
|
/**
|
|
|
|
* isFeatureAvailable to check the presence of Actions cache service
|
|
|
|
*
|
|
|
|
* @returns boolean return true if Actions cache service feature is available, otherwise false
|
|
|
|
*/
|
|
|
|
|
2022-03-29 22:20:22 +00:00
|
|
|
export function isFeatureAvailable(): boolean {
|
2022-03-29 22:19:10 +00:00
|
|
|
return !!process.env['ACTIONS_CACHE_URL']
|
|
|
|
}
|
|
|
|
|
2020-05-06 15:10:18 +00:00
|
|
|
/**
|
|
|
|
* Restores cache from keys
|
|
|
|
*
|
2020-05-06 21:53:22 +00:00
|
|
|
* @param paths a list of file paths to restore from the cache
|
2020-05-06 15:10:18 +00:00
|
|
|
* @param primaryKey an explicit key for restoring the cache
|
|
|
|
* @param restoreKeys an optional ordered list of keys to use for restoring the cache if no cache hit occurred for key
|
2020-07-10 15:09:32 +00:00
|
|
|
* @param downloadOptions cache download options
|
2020-05-15 16:18:50 +00:00
|
|
|
* @returns string returns the key for the cache hit, otherwise returns undefined
|
2020-05-06 15:10:18 +00:00
|
|
|
*/
|
|
|
|
export async function restoreCache(
|
2020-05-06 21:53:22 +00:00
|
|
|
paths: string[],
|
2020-05-06 15:10:18 +00:00
|
|
|
primaryKey: string,
|
2020-07-10 15:09:32 +00:00
|
|
|
restoreKeys?: string[],
|
|
|
|
options?: DownloadOptions
|
2020-05-06 15:10:18 +00:00
|
|
|
): Promise<string | undefined> {
|
2020-05-06 21:53:22 +00:00
|
|
|
checkPaths(paths)
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
restoreKeys = restoreKeys || []
|
|
|
|
const keys = [primaryKey, ...restoreKeys]
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
core.debug('Resolved Keys:')
|
|
|
|
core.debug(JSON.stringify(keys))
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
if (keys.length > 10) {
|
2020-05-12 18:47:31 +00:00
|
|
|
throw new ValidationError(
|
2020-05-06 21:53:22 +00:00
|
|
|
`Key Validation Error: Keys are limited to a maximum of 10.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
for (const key of keys) {
|
|
|
|
checkKey(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
const compressionMethod = await utils.getCompressionMethod()
|
|
|
|
|
|
|
|
// path are needed to compute version
|
|
|
|
const cacheEntry = await cacheHttpClient.getCacheEntry(keys, paths, {
|
|
|
|
compressionMethod
|
|
|
|
})
|
|
|
|
if (!cacheEntry?.archiveLocation) {
|
2020-05-15 16:18:50 +00:00
|
|
|
// Cache not found
|
2020-05-06 21:53:22 +00:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
const archivePath = path.join(
|
|
|
|
await utils.createTempDirectory(),
|
|
|
|
utils.getCacheFileName(compressionMethod)
|
|
|
|
)
|
|
|
|
core.debug(`Archive Path: ${archivePath}`)
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
try {
|
|
|
|
// Download the cache from the cache entry
|
2020-07-10 15:09:32 +00:00
|
|
|
await cacheHttpClient.downloadCache(
|
|
|
|
cacheEntry.archiveLocation,
|
|
|
|
archivePath,
|
|
|
|
options
|
|
|
|
)
|
2020-05-06 21:53:22 +00:00
|
|
|
|
2020-11-25 22:56:57 +00:00
|
|
|
if (core.isDebug()) {
|
|
|
|
await listTar(archivePath, compressionMethod)
|
|
|
|
}
|
|
|
|
|
2021-05-03 15:09:44 +00:00
|
|
|
const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath)
|
2020-05-06 21:53:22 +00:00
|
|
|
core.info(
|
|
|
|
`Cache Size: ~${Math.round(
|
|
|
|
archiveFileSize / (1024 * 1024)
|
|
|
|
)} MB (${archiveFileSize} B)`
|
|
|
|
)
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
await extractTar(archivePath, compressionMethod)
|
2020-11-25 22:56:57 +00:00
|
|
|
core.info('Cache restored successfully')
|
2020-05-06 21:53:22 +00:00
|
|
|
} finally {
|
|
|
|
// Try to delete the archive to save space
|
2020-05-06 15:10:18 +00:00
|
|
|
try {
|
2020-05-06 21:53:22 +00:00
|
|
|
await utils.unlinkFile(archivePath)
|
2020-05-06 15:10:18 +00:00
|
|
|
} catch (error) {
|
2020-05-06 21:53:22 +00:00
|
|
|
core.debug(`Failed to delete archive: ${error}`)
|
2020-05-06 15:10:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 21:53:22 +00:00
|
|
|
|
|
|
|
return cacheEntry.cacheKey
|
2020-05-06 15:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-06 21:53:22 +00:00
|
|
|
* Saves a list of files with the specified key
|
2020-05-06 15:10:18 +00:00
|
|
|
*
|
2020-05-06 21:53:22 +00:00
|
|
|
* @param paths a list of file paths to be cached
|
2020-05-06 15:10:18 +00:00
|
|
|
* @param key an explicit key for restoring the cache
|
2020-05-12 16:37:03 +00:00
|
|
|
* @param options cache upload options
|
2020-05-15 16:18:50 +00:00
|
|
|
* @returns number returns cacheId if the cache was saved successfully and throws an error if save fails
|
2020-05-06 15:10:18 +00:00
|
|
|
*/
|
2020-05-12 16:37:03 +00:00
|
|
|
export async function saveCache(
|
|
|
|
paths: string[],
|
|
|
|
key: string,
|
|
|
|
options?: UploadOptions
|
|
|
|
): Promise<number> {
|
2020-05-06 21:53:22 +00:00
|
|
|
checkPaths(paths)
|
|
|
|
checkKey(key)
|
|
|
|
|
|
|
|
const compressionMethod = await utils.getCompressionMethod()
|
2022-03-29 22:17:04 +00:00
|
|
|
let cacheId = null
|
2020-05-06 21:53:22 +00:00
|
|
|
|
2020-05-12 18:47:31 +00:00
|
|
|
const cachePaths = await utils.resolvePaths(paths)
|
2020-05-06 21:53:22 +00:00
|
|
|
core.debug('Cache Paths:')
|
|
|
|
core.debug(`${JSON.stringify(cachePaths)}`)
|
|
|
|
|
|
|
|
const archiveFolder = await utils.createTempDirectory()
|
|
|
|
const archivePath = path.join(
|
|
|
|
archiveFolder,
|
|
|
|
utils.getCacheFileName(compressionMethod)
|
|
|
|
)
|
|
|
|
|
|
|
|
core.debug(`Archive Path: ${archivePath}`)
|
|
|
|
|
2021-06-28 15:27:09 +00:00
|
|
|
try {
|
|
|
|
await createTar(archiveFolder, cachePaths, compressionMethod)
|
|
|
|
if (core.isDebug()) {
|
|
|
|
await listTar(archivePath, compressionMethod)
|
|
|
|
}
|
2020-05-06 21:53:22 +00:00
|
|
|
|
2021-06-28 15:27:09 +00:00
|
|
|
const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath)
|
|
|
|
core.debug(`File Size: ${archiveFileSize}`)
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2022-03-30 07:22:56 +00:00
|
|
|
const cacheSize = archiveFileSize
|
2022-03-29 22:17:04 +00:00
|
|
|
core.debug('Reserving Cache')
|
2022-03-31 10:41:54 +00:00
|
|
|
const reserveCacheResponse = await cacheHttpClient.reserveCache(
|
|
|
|
key,
|
|
|
|
paths,
|
|
|
|
{
|
|
|
|
compressionMethod,
|
|
|
|
cacheSize
|
|
|
|
}
|
|
|
|
)
|
2022-03-30 07:22:56 +00:00
|
|
|
|
2022-03-31 10:41:54 +00:00
|
|
|
if (
|
|
|
|
reserveCacheResponse?.statusCode === 400 &&
|
|
|
|
reserveCacheResponse?.typeKey === 'InvalidReserveCacheRequestException'
|
|
|
|
) {
|
2022-03-29 22:17:04 +00:00
|
|
|
throw new ReserveCacheError(
|
2022-03-31 09:42:59 +00:00
|
|
|
reserveCacheResponse?.message ??
|
2022-03-31 10:41:54 +00:00
|
|
|
`Cache size of ~${Math.round(
|
|
|
|
archiveFileSize / (1024 * 1024)
|
|
|
|
)} MB (${archiveFileSize} B) is over the data cap limit, not saving cache.`
|
2022-03-29 22:17:04 +00:00
|
|
|
)
|
|
|
|
}
|
2022-03-30 07:22:56 +00:00
|
|
|
|
2022-03-31 10:41:54 +00:00
|
|
|
if (reserveCacheResponse?.result?.cacheId) {
|
2022-03-30 07:22:56 +00:00
|
|
|
cacheId = reserveCacheResponse?.result?.cacheId
|
2022-03-31 10:41:54 +00:00
|
|
|
} else {
|
2022-03-29 22:17:04 +00:00
|
|
|
throw new ReserveCacheError(
|
2022-03-30 07:22:56 +00:00
|
|
|
`Unable to reserve cache with key ${key}, another job may be creating this cache.`
|
2022-03-29 22:17:04 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:22:56 +00:00
|
|
|
core.debug(`Cache ID: ${cacheId}`)
|
2021-06-28 15:27:09 +00:00
|
|
|
core.debug(`Saving Cache (ID: ${cacheId})`)
|
|
|
|
await cacheHttpClient.saveCache(cacheId, archivePath, options)
|
|
|
|
} finally {
|
|
|
|
// Try to delete the archive to save space
|
|
|
|
try {
|
|
|
|
await utils.unlinkFile(archivePath)
|
|
|
|
} catch (error) {
|
|
|
|
core.debug(`Failed to delete archive: ${error}`)
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
return cacheId
|
2020-05-06 15:10:18 +00:00
|
|
|
}
|