2020-05-06 11:10:18 -04:00
|
|
|
import * as core from '@actions/core'
|
2020-05-06 17:53:22 -04:00
|
|
|
import * as path from 'path'
|
2024-10-24 05:19:48 -07:00
|
|
|
import * as utils from './internal/cacheUtils'
|
2020-05-06 11:10:18 -04:00
|
|
|
import * as cacheHttpClient from './internal/cacheHttpClient'
|
2024-10-24 05:19:48 -07:00
|
|
|
import * as cacheTwirpClient from './internal/shared/cacheTwirpClient'
|
2026-07-15 13:13:58 -05:00
|
|
|
import {
|
|
|
|
|
getCacheServiceVersion,
|
|
|
|
|
isGhes,
|
|
|
|
|
getCacheMode,
|
|
|
|
|
isCacheReadable,
|
|
|
|
|
isCacheWritable
|
|
|
|
|
} from './internal/config'
|
2025-07-14 03:49:28 -07:00
|
|
|
import {DownloadOptions, UploadOptions} from './options'
|
|
|
|
|
import {createTar, extractTar, listTar} from './internal/tar'
|
2024-06-17 01:17:10 -07:00
|
|
|
import {
|
2024-09-24 03:17:44 -07:00
|
|
|
CreateCacheEntryRequest,
|
|
|
|
|
FinalizeCacheEntryUploadRequest,
|
|
|
|
|
FinalizeCacheEntryUploadResponse,
|
2024-11-14 07:11:12 -08:00
|
|
|
GetCacheEntryDownloadURLRequest
|
2024-09-24 03:17:44 -07:00
|
|
|
} from './generated/results/api/v1/cache'
|
2025-07-14 12:07:37 +00:00
|
|
|
import {HttpClientError} from '@actions/http-client'
|
2026-07-15 13:13:58 -05:00
|
|
|
import {CacheReadDeniedMessagePrefix} from './internal/constants'
|
2020-05-12 14:47:31 -04:00
|
|
|
export class ValidationError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = 'ValidationError'
|
2020-05-15 12:18:50 -04:00
|
|
|
Object.setPrototypeOf(this, ValidationError.prototype)
|
2020-05-12 14:47:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ReserveCacheError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = 'ReserveCacheError'
|
2020-05-15 12:18:50 -04:00
|
|
|
Object.setPrototypeOf(this, ReserveCacheError.prototype)
|
2020-05-12 14:47:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 11:26:08 -10:00
|
|
|
/**
|
|
|
|
|
* Stable prefix used by the cache receiver to signal that the token has
|
|
|
|
|
* no writable scopes (read-only cache policy). Consumers can match on
|
|
|
|
|
* this prefix to distinguish policy denials from ordinary contention.
|
|
|
|
|
*/
|
|
|
|
|
export const CACHE_WRITE_DENIED_PREFIX = 'cache write denied:'
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extends ReserveCacheError for source-compatibility: existing
|
|
|
|
|
* `instanceof ReserveCacheError` checks and `typedError.name ===
|
|
|
|
|
* ReserveCacheError.name` paths keep working, while consumers that want to
|
|
|
|
|
* distinguish a policy denial can check for CacheWriteDeniedError.name.
|
|
|
|
|
*/
|
|
|
|
|
export class CacheWriteDeniedError extends ReserveCacheError {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = 'CacheWriteDeniedError'
|
|
|
|
|
Object.setPrototypeOf(this, CacheWriteDeniedError.prototype)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 13:13:58 -05:00
|
|
|
// Re-exported from constants so consumers keep referencing it here; the shared
|
|
|
|
|
// value also drives detection in cacheHttpClient without duplicating the string.
|
|
|
|
|
export const CACHE_READ_DENIED_PREFIX = CacheReadDeniedMessagePrefix
|
|
|
|
|
|
|
|
|
|
// Raised when the cache backend denies a download URL because the run's token
|
|
|
|
|
// has no readable cache scopes. Caching is best-effort, so restoreCache logs a
|
|
|
|
|
// warning and reports a cache miss rather than rethrowing this.
|
|
|
|
|
export class CacheReadDeniedError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = 'CacheReadDeniedError'
|
|
|
|
|
Object.setPrototypeOf(this, CacheReadDeniedError.prototype)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 13:00:46 +00:00
|
|
|
export class FinalizeCacheError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = 'FinalizeCacheError'
|
|
|
|
|
Object.setPrototypeOf(this, FinalizeCacheError.prototype)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 17:53:22 -04:00
|
|
|
function checkPaths(paths: string[]): void {
|
|
|
|
|
if (!paths || paths.length === 0) {
|
2020-05-12 14:47:31 -04:00
|
|
|
throw new ValidationError(
|
2020-05-06 17:53:22 -04: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 14:47:31 -04:00
|
|
|
throw new ValidationError(
|
2020-05-06 17:53:22 -04:00
|
|
|
`Key Validation Error: ${key} cannot be larger than 512 characters.`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
const regex = /^[^,]*$/
|
|
|
|
|
if (!regex.test(key)) {
|
2020-05-12 14:47:31 -04:00
|
|
|
throw new ValidationError(
|
|
|
|
|
`Key Validation Error: ${key} cannot contain commas.`
|
|
|
|
|
)
|
2020-05-06 17:53:22 -04: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
|
|
|
|
|
*/
|
2024-10-09 04:32:57 -07:00
|
|
|
export function isFeatureAvailable(): boolean {
|
2025-07-14 10:32:34 +00:00
|
|
|
const cacheServiceVersion = getCacheServiceVersion()
|
|
|
|
|
|
|
|
|
|
// Check availability based on cache service version
|
|
|
|
|
switch (cacheServiceVersion) {
|
|
|
|
|
case 'v2':
|
|
|
|
|
// For v2, we need ACTIONS_RESULTS_URL
|
|
|
|
|
return !!process.env['ACTIONS_RESULTS_URL']
|
|
|
|
|
case 'v1':
|
|
|
|
|
default:
|
2025-07-14 13:39:13 +00:00
|
|
|
// For v1, we only need ACTIONS_CACHE_URL
|
|
|
|
|
return !!process.env['ACTIONS_CACHE_URL']
|
2025-07-14 10:32:34 +00:00
|
|
|
}
|
2024-10-09 04:32:57 -07:00
|
|
|
}
|
2022-03-29 22:19:10 +00:00
|
|
|
|
2020-05-06 11:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* Restores cache from keys
|
|
|
|
|
*
|
2020-05-06 17:53:22 -04:00
|
|
|
* @param paths a list of file paths to restore from the cache
|
2024-12-02 10:53:29 -08:00
|
|
|
* @param primaryKey an explicit key for restoring the cache. Lookup is done with prefix matching.
|
2024-12-02 10:55:33 -08:00
|
|
|
* @param restoreKeys an optional ordered list of keys to use for restoring the cache if no cache hit occurred for primaryKey
|
2020-07-10 10:09:32 -05:00
|
|
|
* @param downloadOptions cache download options
|
2023-01-04 12:16:25 +05:30
|
|
|
* @param enableCrossOsArchive an optional boolean enabled to restore on windows any cache created on any platform
|
2020-05-15 12:18:50 -04:00
|
|
|
* @returns string returns the key for the cache hit, otherwise returns undefined
|
2020-05-06 11:10:18 -04:00
|
|
|
*/
|
|
|
|
|
export async function restoreCache(
|
2020-05-06 17:53:22 -04:00
|
|
|
paths: string[],
|
2020-05-06 11:10:18 -04:00
|
|
|
primaryKey: string,
|
2020-07-10 10:09:32 -05:00
|
|
|
restoreKeys?: string[],
|
2023-01-04 12:16:25 +05:30
|
|
|
options?: DownloadOptions,
|
|
|
|
|
enableCrossOsArchive = false
|
2020-05-06 11:10:18 -04:00
|
|
|
): Promise<string | undefined> {
|
2024-11-25 04:08:47 -08:00
|
|
|
const cacheServiceVersion: string = getCacheServiceVersion()
|
|
|
|
|
core.debug(`Cache service version: ${cacheServiceVersion}`)
|
|
|
|
|
|
2020-05-06 17:53:22 -04:00
|
|
|
checkPaths(paths)
|
2020-05-06 11:10:18 -04:00
|
|
|
|
2026-07-15 13:13:58 -05:00
|
|
|
const cacheMode = getCacheMode()
|
|
|
|
|
if (!isCacheReadable(cacheMode)) {
|
|
|
|
|
core.info(
|
|
|
|
|
`Cache restore skipped: the effective cache-mode '${cacheMode}' does not permit reads.`
|
|
|
|
|
)
|
|
|
|
|
core.debug(
|
|
|
|
|
`Skipped restore for paths [${paths.join(', ')}] with primary key '${primaryKey}'.`
|
|
|
|
|
)
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 03:29:14 -07:00
|
|
|
switch (cacheServiceVersion) {
|
2024-11-14 03:22:03 -08:00
|
|
|
case 'v2':
|
2024-11-14 07:11:12 -08:00
|
|
|
return await restoreCacheV2(
|
2024-11-14 03:22:03 -08:00
|
|
|
paths,
|
|
|
|
|
primaryKey,
|
|
|
|
|
restoreKeys,
|
|
|
|
|
options,
|
|
|
|
|
enableCrossOsArchive
|
|
|
|
|
)
|
|
|
|
|
case 'v1':
|
2024-06-17 01:17:10 -07:00
|
|
|
default:
|
2024-11-14 07:11:12 -08:00
|
|
|
return await restoreCacheV1(
|
2024-11-14 03:22:03 -08:00
|
|
|
paths,
|
|
|
|
|
primaryKey,
|
|
|
|
|
restoreKeys,
|
|
|
|
|
options,
|
|
|
|
|
enableCrossOsArchive
|
|
|
|
|
)
|
2024-06-17 01:17:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 05:21:32 -07:00
|
|
|
/**
|
|
|
|
|
* Restores cache using the legacy Cache Service
|
2024-11-14 03:22:03 -08:00
|
|
|
*
|
2024-12-02 04:08:21 -08:00
|
|
|
* @param paths a list of file paths to restore from the cache
|
2024-12-02 10:53:29 -08:00
|
|
|
* @param primaryKey an explicit key for restoring the cache. Lookup is done with prefix matching.
|
2024-12-02 19:46:18 +01:00
|
|
|
* @param restoreKeys an optional ordered list of keys to use for restoring the cache if no cache hit occurred for primaryKey
|
2024-12-02 04:08:21 -08:00
|
|
|
* @param options cache download options
|
2024-12-02 19:46:11 +01:00
|
|
|
* @param enableCrossOsArchive an optional boolean enabled to restore on Windows any cache created on any platform
|
2024-12-02 04:08:21 -08:00
|
|
|
* @returns string returns the key for the cache hit, otherwise returns undefined
|
2024-10-21 05:21:32 -07:00
|
|
|
*/
|
2024-11-14 07:11:12 -08:00
|
|
|
async function restoreCacheV1(
|
2024-06-17 01:17:10 -07:00
|
|
|
paths: string[],
|
|
|
|
|
primaryKey: string,
|
|
|
|
|
restoreKeys?: string[],
|
|
|
|
|
options?: DownloadOptions,
|
|
|
|
|
enableCrossOsArchive = false
|
2024-11-14 03:42:14 -08:00
|
|
|
): Promise<string | undefined> {
|
2020-05-06 17:53:22 -04:00
|
|
|
restoreKeys = restoreKeys || []
|
|
|
|
|
const keys = [primaryKey, ...restoreKeys]
|
2020-05-06 11:10:18 -04:00
|
|
|
|
2020-05-06 17:53:22 -04:00
|
|
|
core.debug('Resolved Keys:')
|
|
|
|
|
core.debug(JSON.stringify(keys))
|
2020-05-06 11:10:18 -04:00
|
|
|
|
2020-05-06 17:53:22 -04:00
|
|
|
if (keys.length > 10) {
|
2020-05-12 14:47:31 -04:00
|
|
|
throw new ValidationError(
|
2020-05-06 17:53:22 -04:00
|
|
|
`Key Validation Error: Keys are limited to a maximum of 10.`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
for (const key of keys) {
|
|
|
|
|
checkKey(key)
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 16:00:28 +05:30
|
|
|
const compressionMethod = await utils.getCompressionMethod()
|
2022-06-24 10:46:20 +05:30
|
|
|
let archivePath = ''
|
|
|
|
|
try {
|
|
|
|
|
// path are needed to compute version
|
2026-07-15 13:13:58 -05:00
|
|
|
let cacheEntry
|
|
|
|
|
try {
|
|
|
|
|
cacheEntry = await cacheHttpClient.getCacheEntry(keys, paths, {
|
|
|
|
|
compressionMethod,
|
|
|
|
|
enableCrossOsArchive
|
|
|
|
|
})
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// The v1 artifact cache service returns HTTP 403 with a
|
|
|
|
|
// `cache read denied:` body when the run's token has no readable cache
|
|
|
|
|
// scopes. getCacheEntry lives in a dependency-free internal module and
|
|
|
|
|
// cannot import CacheReadDeniedError without a circular dependency, so it
|
|
|
|
|
// only surfaces the raw denial message; we classify it into the typed
|
|
|
|
|
// error here so the outer catch and consumers can dispatch on it.
|
|
|
|
|
const errorMessage = (error as Error)?.message ?? ''
|
|
|
|
|
if (errorMessage.includes(CACHE_READ_DENIED_PREFIX)) {
|
|
|
|
|
throw new CacheReadDeniedError(errorMessage)
|
|
|
|
|
}
|
|
|
|
|
throw error
|
|
|
|
|
}
|
2022-06-24 10:46:20 +05:30
|
|
|
if (!cacheEntry?.archiveLocation) {
|
2022-12-27 16:00:28 +05:30
|
|
|
// Cache not found
|
|
|
|
|
return undefined
|
2022-06-24 10:46:20 +05:30
|
|
|
}
|
2020-05-06 17:53:22 -04:00
|
|
|
|
2023-01-17 17:12:42 +01:00
|
|
|
if (options?.lookupOnly) {
|
|
|
|
|
core.info('Lookup only - skipping download')
|
2022-12-23 12:44:35 +01:00
|
|
|
return cacheEntry.cacheKey
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 10:46:20 +05:30
|
|
|
archivePath = path.join(
|
|
|
|
|
await utils.createTempDirectory(),
|
|
|
|
|
utils.getCacheFileName(compressionMethod)
|
|
|
|
|
)
|
|
|
|
|
core.debug(`Archive Path: ${archivePath}`)
|
2020-05-06 11:10:18 -04:00
|
|
|
|
2024-11-27 05:50:01 -08:00
|
|
|
// Download the cache from the cache entry
|
2020-07-10 10:09:32 -05:00
|
|
|
await cacheHttpClient.downloadCache(
|
|
|
|
|
cacheEntry.archiveLocation,
|
|
|
|
|
archivePath,
|
2024-11-27 05:50:01 -08:00
|
|
|
options
|
2020-07-10 10:09:32 -05:00
|
|
|
)
|
2020-05-06 17:53:22 -04:00
|
|
|
|
2020-11-26 01:56:57 +03:00
|
|
|
if (core.isDebug()) {
|
|
|
|
|
await listTar(archivePath, compressionMethod)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 18:09:44 +03:00
|
|
|
const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath)
|
2020-05-06 17:53:22 -04:00
|
|
|
core.info(
|
|
|
|
|
`Cache Size: ~${Math.round(
|
|
|
|
|
archiveFileSize / (1024 * 1024)
|
|
|
|
|
)} MB (${archiveFileSize} B)`
|
|
|
|
|
)
|
2020-05-06 11:10:18 -04:00
|
|
|
|
2020-05-06 17:53:22 -04:00
|
|
|
await extractTar(archivePath, compressionMethod)
|
2020-11-26 01:56:57 +03:00
|
|
|
core.info('Cache restored successfully')
|
2022-06-24 10:46:20 +05:30
|
|
|
|
|
|
|
|
return cacheEntry.cacheKey
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const typedError = error as Error
|
|
|
|
|
if (typedError.name === ValidationError.name) {
|
|
|
|
|
throw error
|
|
|
|
|
} else {
|
2025-07-14 12:07:37 +00:00
|
|
|
// warn on cache restore failure and continue build
|
2026-07-15 13:13:58 -05:00
|
|
|
// Log server errors (5xx) as errors, all other errors as warnings.
|
|
|
|
|
// A read denied by policy (CacheReadDeniedError) is not an HttpClientError
|
|
|
|
|
// so it falls here and is warned, treated as a cache miss.
|
2025-07-14 12:42:37 +00:00
|
|
|
if (
|
|
|
|
|
typedError instanceof HttpClientError &&
|
|
|
|
|
typeof typedError.statusCode === 'number' &&
|
|
|
|
|
typedError.statusCode >= 500
|
|
|
|
|
) {
|
|
|
|
|
core.error(`Failed to restore: ${(error as Error).message}`)
|
|
|
|
|
} else {
|
|
|
|
|
core.warning(`Failed to restore: ${(error as Error).message}`)
|
|
|
|
|
}
|
2022-06-24 10:46:20 +05:30
|
|
|
}
|
2020-05-06 17:53:22 -04:00
|
|
|
} finally {
|
|
|
|
|
// Try to delete the archive to save space
|
2020-05-06 11:10:18 -04:00
|
|
|
try {
|
2020-05-06 17:53:22 -04:00
|
|
|
await utils.unlinkFile(archivePath)
|
2020-05-06 11:10:18 -04:00
|
|
|
} catch (error) {
|
2020-05-06 17:53:22 -04:00
|
|
|
core.debug(`Failed to delete archive: ${error}`)
|
2020-05-06 11:10:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-05-06 17:53:22 -04:00
|
|
|
|
2022-06-24 10:46:20 +05:30
|
|
|
return undefined
|
2020-05-06 11:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-09 04:32:57 -07:00
|
|
|
/**
|
2024-12-02 04:08:21 -08:00
|
|
|
* Restores cache using Cache Service v2
|
2024-10-09 04:32:57 -07:00
|
|
|
*
|
|
|
|
|
* @param paths a list of file paths to restore from the cache
|
2024-12-02 10:53:29 -08:00
|
|
|
* @param primaryKey an explicit key for restoring the cache. Lookup is done with prefix matching
|
2024-12-02 10:55:33 -08:00
|
|
|
* @param restoreKeys an optional ordered list of keys to use for restoring the cache if no cache hit occurred for primaryKey
|
2024-10-09 04:32:57 -07:00
|
|
|
* @param downloadOptions cache download options
|
|
|
|
|
* @param enableCrossOsArchive an optional boolean enabled to restore on windows any cache created on any platform
|
|
|
|
|
* @returns string returns the key for the cache hit, otherwise returns undefined
|
|
|
|
|
*/
|
2024-11-14 07:11:12 -08:00
|
|
|
async function restoreCacheV2(
|
2024-06-17 01:17:10 -07:00
|
|
|
paths: string[],
|
|
|
|
|
primaryKey: string,
|
|
|
|
|
restoreKeys?: string[],
|
|
|
|
|
options?: DownloadOptions,
|
|
|
|
|
enableCrossOsArchive = false
|
2024-11-14 03:42:14 -08:00
|
|
|
): Promise<string | undefined> {
|
2024-11-28 07:42:07 -08:00
|
|
|
// Override UploadOptions to force the use of Azure
|
|
|
|
|
options = {
|
|
|
|
|
...options,
|
|
|
|
|
useAzureSdk: true
|
|
|
|
|
}
|
2024-06-17 01:17:10 -07:00
|
|
|
restoreKeys = restoreKeys || []
|
|
|
|
|
const keys = [primaryKey, ...restoreKeys]
|
|
|
|
|
|
2024-11-14 03:01:04 -08:00
|
|
|
core.debug('Resolved Keys:')
|
|
|
|
|
core.debug(JSON.stringify(keys))
|
2024-06-17 01:17:10 -07:00
|
|
|
|
|
|
|
|
if (keys.length > 10) {
|
|
|
|
|
throw new ValidationError(
|
|
|
|
|
`Key Validation Error: Keys are limited to a maximum of 10.`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
for (const key of keys) {
|
|
|
|
|
checkKey(key)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 04:32:57 -07:00
|
|
|
let archivePath = ''
|
2024-06-17 01:17:10 -07:00
|
|
|
try {
|
2024-10-09 04:32:57 -07:00
|
|
|
const twirpClient = cacheTwirpClient.internalCacheTwirpClient()
|
2024-09-24 03:17:44 -07:00
|
|
|
const compressionMethod = await utils.getCompressionMethod()
|
2024-10-09 04:32:57 -07:00
|
|
|
|
2024-09-24 03:17:44 -07:00
|
|
|
const request: GetCacheEntryDownloadURLRequest = {
|
|
|
|
|
key: primaryKey,
|
2024-11-14 03:34:13 -08:00
|
|
|
restoreKeys,
|
2024-09-24 03:17:44 -07:00
|
|
|
version: utils.getCacheVersion(
|
|
|
|
|
paths,
|
|
|
|
|
compressionMethod,
|
2024-11-14 03:22:03 -08:00
|
|
|
enableCrossOsArchive
|
|
|
|
|
)
|
2024-06-17 01:17:10 -07:00
|
|
|
}
|
2024-10-09 04:32:57 -07:00
|
|
|
|
2026-07-15 13:13:58 -05:00
|
|
|
let response
|
|
|
|
|
try {
|
|
|
|
|
response = await twirpClient.GetCacheEntryDownloadURL(request)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// The receiver returns twirp PermissionDenied (403) when the run's token
|
|
|
|
|
// has no readable cache scopes. The client wraps that 403, so the stable
|
|
|
|
|
// prefix is embedded in the message rather than leading it.
|
|
|
|
|
const errorMessage = (error as Error)?.message ?? ''
|
|
|
|
|
if (errorMessage.includes(CACHE_READ_DENIED_PREFIX)) {
|
|
|
|
|
throw new CacheReadDeniedError(errorMessage)
|
|
|
|
|
}
|
|
|
|
|
throw error
|
|
|
|
|
}
|
2024-06-17 01:17:10 -07:00
|
|
|
|
2024-09-24 03:17:44 -07:00
|
|
|
if (!response.ok) {
|
2025-03-15 10:13:43 +11:00
|
|
|
core.debug(
|
|
|
|
|
`Cache not found for version ${request.version} of keys: ${keys.join(
|
|
|
|
|
', '
|
|
|
|
|
)}`
|
|
|
|
|
)
|
2024-06-17 01:17:10 -07:00
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-14 03:45:17 -07:00
|
|
|
const isRestoreKeyMatch = request.key !== response.matchedKey
|
|
|
|
|
if (isRestoreKeyMatch) {
|
|
|
|
|
core.info(`Cache hit for restore-key: ${response.matchedKey}`)
|
|
|
|
|
} else {
|
|
|
|
|
core.info(`Cache hit for: ${response.matchedKey}`)
|
|
|
|
|
}
|
2024-06-17 02:35:25 -07:00
|
|
|
|
2024-10-09 04:32:57 -07:00
|
|
|
if (options?.lookupOnly) {
|
|
|
|
|
core.info('Lookup only - skipping download')
|
2024-11-25 05:47:51 -08:00
|
|
|
return response.matchedKey
|
2024-10-09 04:32:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
archivePath = path.join(
|
|
|
|
|
await utils.createTempDirectory(),
|
|
|
|
|
utils.getCacheFileName(compressionMethod)
|
|
|
|
|
)
|
|
|
|
|
core.debug(`Archive path: ${archivePath}`)
|
2024-11-14 06:49:55 -08:00
|
|
|
core.debug(`Starting download of archive to: ${archivePath}`)
|
2024-10-21 05:21:32 -07:00
|
|
|
|
2024-11-28 04:56:37 -08:00
|
|
|
await cacheHttpClient.downloadCache(
|
2024-11-25 07:34:52 -08:00
|
|
|
response.signedDownloadUrl,
|
2024-11-27 04:51:21 -08:00
|
|
|
archivePath,
|
2024-11-28 04:56:37 -08:00
|
|
|
options
|
2024-11-25 07:34:52 -08:00
|
|
|
)
|
2024-10-21 05:21:32 -07:00
|
|
|
|
2024-10-09 04:32:57 -07:00
|
|
|
const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath)
|
|
|
|
|
core.info(
|
|
|
|
|
`Cache Size: ~${Math.round(
|
|
|
|
|
archiveFileSize / (1024 * 1024)
|
|
|
|
|
)} MB (${archiveFileSize} B)`
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-21 05:21:32 -07:00
|
|
|
if (core.isDebug()) {
|
|
|
|
|
await listTar(archivePath, compressionMethod)
|
|
|
|
|
}
|
2024-10-09 04:32:57 -07:00
|
|
|
|
|
|
|
|
await extractTar(archivePath, compressionMethod)
|
|
|
|
|
core.info('Cache restored successfully')
|
|
|
|
|
|
2024-11-25 05:42:50 -08:00
|
|
|
return response.matchedKey
|
2024-06-17 01:17:10 -07:00
|
|
|
} catch (error) {
|
2024-11-22 09:01:59 -08:00
|
|
|
const typedError = error as Error
|
|
|
|
|
if (typedError.name === ValidationError.name) {
|
|
|
|
|
throw error
|
|
|
|
|
} else {
|
|
|
|
|
// Supress all non-validation cache related errors because caching should be optional
|
2026-07-15 13:13:58 -05:00
|
|
|
// Log server errors (5xx) as errors, all other errors as warnings.
|
|
|
|
|
// A read denied by policy (CacheReadDeniedError) is not an HttpClientError
|
|
|
|
|
// so it falls here and is warned, treated as a cache miss.
|
2025-07-14 12:42:37 +00:00
|
|
|
if (
|
|
|
|
|
typedError instanceof HttpClientError &&
|
|
|
|
|
typeof typedError.statusCode === 'number' &&
|
|
|
|
|
typedError.statusCode >= 500
|
|
|
|
|
) {
|
|
|
|
|
core.error(`Failed to restore: ${(error as Error).message}`)
|
|
|
|
|
} else {
|
|
|
|
|
core.warning(`Failed to restore: ${(error as Error).message}`)
|
|
|
|
|
}
|
2024-11-22 09:01:59 -08:00
|
|
|
}
|
2024-10-09 04:32:57 -07:00
|
|
|
} finally {
|
|
|
|
|
try {
|
2024-11-14 15:49:02 +01:00
|
|
|
if (archivePath) {
|
|
|
|
|
await utils.unlinkFile(archivePath)
|
|
|
|
|
}
|
2024-10-09 04:32:57 -07:00
|
|
|
} catch (error) {
|
|
|
|
|
core.debug(`Failed to delete archive: ${error}`)
|
|
|
|
|
}
|
2024-06-17 01:17:10 -07:00
|
|
|
}
|
2024-11-22 09:01:59 -08:00
|
|
|
|
|
|
|
|
return undefined
|
2024-06-17 01:17:10 -07:00
|
|
|
}
|
|
|
|
|
|
2020-05-06 11:10:18 -04:00
|
|
|
/**
|
2020-05-06 17:53:22 -04:00
|
|
|
* Saves a list of files with the specified key
|
2020-05-06 11:10:18 -04:00
|
|
|
*
|
2020-05-06 17:53:22 -04:00
|
|
|
* @param paths a list of file paths to be cached
|
2020-05-06 11:10:18 -04:00
|
|
|
* @param key an explicit key for restoring the cache
|
2023-01-04 12:16:25 +05:30
|
|
|
* @param enableCrossOsArchive an optional boolean enabled to save cache on windows which could be restored on any platform
|
2020-05-12 12:37:03 -04:00
|
|
|
* @param options cache upload options
|
2020-05-15 12:18:50 -04:00
|
|
|
* @returns number returns cacheId if the cache was saved successfully and throws an error if save fails
|
2020-05-06 11:10:18 -04:00
|
|
|
*/
|
2020-05-12 12:37:03 -04:00
|
|
|
export async function saveCache(
|
|
|
|
|
paths: string[],
|
|
|
|
|
key: string,
|
2023-01-04 12:16:25 +05:30
|
|
|
options?: UploadOptions,
|
|
|
|
|
enableCrossOsArchive = false
|
2020-05-12 12:37:03 -04:00
|
|
|
): Promise<number> {
|
2024-11-24 18:44:39 +00:00
|
|
|
const cacheServiceVersion: string = getCacheServiceVersion()
|
|
|
|
|
core.debug(`Cache service version: ${cacheServiceVersion}`)
|
2020-05-06 17:53:22 -04:00
|
|
|
checkPaths(paths)
|
|
|
|
|
checkKey(key)
|
2026-07-15 13:13:58 -05:00
|
|
|
|
|
|
|
|
const cacheMode = getCacheMode()
|
|
|
|
|
if (!isCacheWritable(cacheMode)) {
|
|
|
|
|
core.info(
|
|
|
|
|
`Cache save skipped: the effective cache-mode '${cacheMode}' does not permit writes.`
|
|
|
|
|
)
|
|
|
|
|
core.debug(
|
|
|
|
|
`Skipped save for paths [${paths.join(', ')}] with key '${key}'.`
|
|
|
|
|
)
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 03:29:14 -07:00
|
|
|
switch (cacheServiceVersion) {
|
2024-11-14 03:22:03 -08:00
|
|
|
case 'v2':
|
2024-11-14 07:11:12 -08:00
|
|
|
return await saveCacheV2(paths, key, options, enableCrossOsArchive)
|
2024-11-14 03:22:03 -08:00
|
|
|
case 'v1':
|
2024-06-13 03:16:59 -07:00
|
|
|
default:
|
2024-11-14 07:11:12 -08:00
|
|
|
return await saveCacheV1(paths, key, options, enableCrossOsArchive)
|
2024-05-29 08:31:54 -07:00
|
|
|
}
|
2024-06-13 03:16:59 -07:00
|
|
|
}
|
2024-05-29 08:31:54 -07:00
|
|
|
|
2024-10-21 05:21:32 -07:00
|
|
|
/**
|
|
|
|
|
* Save cache using the legacy Cache Service
|
2024-11-14 03:22:03 -08:00
|
|
|
*
|
|
|
|
|
* @param paths
|
|
|
|
|
* @param key
|
|
|
|
|
* @param options
|
|
|
|
|
* @param enableCrossOsArchive
|
|
|
|
|
* @returns
|
2024-10-21 05:21:32 -07:00
|
|
|
*/
|
2024-11-14 07:11:12 -08:00
|
|
|
async function saveCacheV1(
|
2024-06-13 03:16:59 -07:00
|
|
|
paths: string[],
|
|
|
|
|
key: string,
|
|
|
|
|
options?: UploadOptions,
|
|
|
|
|
enableCrossOsArchive = false
|
|
|
|
|
): Promise<number> {
|
2020-05-06 17:53:22 -04:00
|
|
|
const compressionMethod = await utils.getCompressionMethod()
|
2022-06-24 10:46:20 +05:30
|
|
|
let cacheId = -1
|
2020-05-06 17:53:22 -04:00
|
|
|
|
2020-05-12 14:47:31 -04:00
|
|
|
const cachePaths = await utils.resolvePaths(paths)
|
2020-05-06 17:53:22 -04:00
|
|
|
core.debug('Cache Paths:')
|
|
|
|
|
core.debug(`${JSON.stringify(cachePaths)}`)
|
|
|
|
|
|
2022-05-23 05:59:56 +00:00
|
|
|
if (cachePaths.length === 0) {
|
2022-05-23 05:39:42 +00:00
|
|
|
throw new Error(
|
2022-05-23 12:03:18 +00:00
|
|
|
`Path Validation Error: Path(s) specified in the action for caching do(es) not exist, hence no cache is being saved.`
|
2022-05-23 05:59:56 +00:00
|
|
|
)
|
2022-05-22 10:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-06 17:53:22 -04:00
|
|
|
const archiveFolder = await utils.createTempDirectory()
|
|
|
|
|
const archivePath = path.join(
|
|
|
|
|
archiveFolder,
|
|
|
|
|
utils.getCacheFileName(compressionMethod)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
core.debug(`Archive Path: ${archivePath}`)
|
|
|
|
|
|
2021-06-28 16:27:09 +01:00
|
|
|
try {
|
|
|
|
|
await createTar(archiveFolder, cachePaths, compressionMethod)
|
|
|
|
|
if (core.isDebug()) {
|
|
|
|
|
await listTar(archivePath, compressionMethod)
|
|
|
|
|
}
|
2022-04-01 01:41:12 +05:30
|
|
|
const fileSizeLimit = 10 * 1024 * 1024 * 1024 // 10GB per repo limit
|
2021-06-28 16:27:09 +01:00
|
|
|
const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath)
|
|
|
|
|
core.debug(`File Size: ${archiveFileSize}`)
|
2022-04-04 16:21:58 +05:30
|
|
|
|
|
|
|
|
// For GHES, this check will take place in ReserveCache API with enterprise file size limit
|
2024-11-21 04:01:44 -08:00
|
|
|
if (archiveFileSize > fileSizeLimit && !isGhes()) {
|
2022-04-01 01:41:12 +05:30
|
|
|
throw new Error(
|
|
|
|
|
`Cache size of ~${Math.round(
|
|
|
|
|
archiveFileSize / (1024 * 1024)
|
|
|
|
|
)} MB (${archiveFileSize} B) is over the 10GB limit, not saving cache.`
|
|
|
|
|
)
|
|
|
|
|
}
|
2020-05-06 11:10:18 -04:00
|
|
|
|
2022-04-04 16:21:58 +05:30
|
|
|
core.debug('Reserving Cache')
|
|
|
|
|
const reserveCacheResponse = await cacheHttpClient.reserveCache(
|
|
|
|
|
key,
|
|
|
|
|
paths,
|
|
|
|
|
{
|
|
|
|
|
compressionMethod,
|
2023-01-04 12:16:25 +05:30
|
|
|
enableCrossOsArchive,
|
2022-04-04 16:21:58 +05:30
|
|
|
cacheSize: archiveFileSize
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (reserveCacheResponse?.result?.cacheId) {
|
|
|
|
|
cacheId = reserveCacheResponse?.result?.cacheId
|
|
|
|
|
} else if (reserveCacheResponse?.statusCode === 400) {
|
2022-04-07 09:08:16 +00:00
|
|
|
throw new Error(
|
2022-04-04 16:21:58 +05:30
|
|
|
reserveCacheResponse?.error?.message ??
|
2025-07-14 03:49:28 -07:00
|
|
|
`Cache size of ~${Math.round(
|
|
|
|
|
archiveFileSize / (1024 * 1024)
|
|
|
|
|
)} MB (${archiveFileSize} B) is over the data cap limit, not saving cache.`
|
2022-04-04 16:21:58 +05:30
|
|
|
)
|
|
|
|
|
} else {
|
2026-06-17 11:26:08 -10:00
|
|
|
const detailMessage = reserveCacheResponse?.error?.message
|
|
|
|
|
if (detailMessage?.startsWith(CACHE_WRITE_DENIED_PREFIX)) {
|
|
|
|
|
throw new CacheWriteDeniedError(
|
|
|
|
|
`Unable to reserve cache with key ${key}. More details: ${detailMessage}`
|
|
|
|
|
)
|
|
|
|
|
}
|
2022-04-04 16:21:58 +05:30
|
|
|
throw new ReserveCacheError(
|
2026-06-17 11:26:08 -10:00
|
|
|
`Unable to reserve cache with key ${key}, another job may be creating this cache. More details: ${detailMessage}`
|
2022-04-04 16:21:58 +05:30
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 16:27:09 +01:00
|
|
|
core.debug(`Saving Cache (ID: ${cacheId})`)
|
2024-11-28 07:22:01 -08:00
|
|
|
await cacheHttpClient.saveCache(cacheId, archivePath, '', options)
|
2022-06-24 10:46:20 +05:30
|
|
|
} catch (error) {
|
|
|
|
|
const typedError = error as Error
|
|
|
|
|
if (typedError.name === ValidationError.name) {
|
|
|
|
|
throw error
|
2026-06-17 11:26:08 -10:00
|
|
|
} else if (typedError.name === CacheWriteDeniedError.name) {
|
|
|
|
|
core.warning(`Failed to save: ${typedError.message}`)
|
2022-06-24 10:46:20 +05:30
|
|
|
} else if (typedError.name === ReserveCacheError.name) {
|
|
|
|
|
core.info(`Failed to save: ${typedError.message}`)
|
|
|
|
|
} else {
|
2025-07-14 12:42:37 +00:00
|
|
|
// Log server errors (5xx) as errors, all other errors as warnings
|
|
|
|
|
if (
|
|
|
|
|
typedError instanceof HttpClientError &&
|
|
|
|
|
typeof typedError.statusCode === 'number' &&
|
|
|
|
|
typedError.statusCode >= 500
|
|
|
|
|
) {
|
|
|
|
|
core.error(`Failed to save: ${typedError.message}`)
|
|
|
|
|
} else {
|
|
|
|
|
core.warning(`Failed to save: ${typedError.message}`)
|
|
|
|
|
}
|
2022-06-24 10:46:20 +05:30
|
|
|
}
|
2021-06-28 16:27:09 +01:00
|
|
|
} 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 11:10:18 -04:00
|
|
|
|
2020-05-06 17:53:22 -04:00
|
|
|
return cacheId
|
2020-05-06 11:10:18 -04:00
|
|
|
}
|
2024-06-13 03:16:59 -07:00
|
|
|
|
2024-10-21 05:21:32 -07:00
|
|
|
/**
|
2024-12-02 04:08:21 -08:00
|
|
|
* Save cache using Cache Service v2
|
2024-11-14 03:22:03 -08:00
|
|
|
*
|
2024-12-02 04:08:21 -08:00
|
|
|
* @param paths a list of file paths to restore from the cache
|
|
|
|
|
* @param key an explicit key for restoring the cache
|
|
|
|
|
* @param options cache upload options
|
|
|
|
|
* @param enableCrossOsArchive an optional boolean enabled to save cache on windows which could be restored on any platform
|
2024-11-14 03:22:03 -08:00
|
|
|
* @returns
|
2024-10-21 05:21:32 -07:00
|
|
|
*/
|
2024-11-14 07:11:12 -08:00
|
|
|
async function saveCacheV2(
|
2024-06-13 03:16:59 -07:00
|
|
|
paths: string[],
|
|
|
|
|
key: string,
|
|
|
|
|
options?: UploadOptions,
|
|
|
|
|
enableCrossOsArchive = false
|
|
|
|
|
): Promise<number> {
|
2024-11-28 07:22:01 -08:00
|
|
|
// Override UploadOptions to force the use of Azure
|
2024-12-02 10:53:29 -08:00
|
|
|
// ...options goes first because we want to override the default values
|
|
|
|
|
// set in UploadOptions with these specific figures
|
2024-11-28 07:22:01 -08:00
|
|
|
options = {
|
|
|
|
|
...options,
|
2024-11-29 07:36:51 -08:00
|
|
|
uploadChunkSize: 64 * 1024 * 1024, // 64 MiB
|
2024-11-29 07:09:05 -08:00
|
|
|
uploadConcurrency: 8, // 8 workers for parallel upload
|
2024-11-28 07:22:01 -08:00
|
|
|
useAzureSdk: true
|
|
|
|
|
}
|
2024-09-24 03:17:44 -07:00
|
|
|
const compressionMethod = await utils.getCompressionMethod()
|
|
|
|
|
const twirpClient = cacheTwirpClient.internalCacheTwirpClient()
|
2024-10-21 05:21:32 -07:00
|
|
|
let cacheId = -1
|
|
|
|
|
|
|
|
|
|
const cachePaths = await utils.resolvePaths(paths)
|
|
|
|
|
core.debug('Cache Paths:')
|
|
|
|
|
core.debug(`${JSON.stringify(cachePaths)}`)
|
|
|
|
|
|
|
|
|
|
if (cachePaths.length === 0) {
|
2024-06-13 03:16:59 -07:00
|
|
|
throw new Error(
|
2024-10-21 05:21:32 -07:00
|
|
|
`Path Validation Error: Path(s) specified in the action for caching do(es) not exist, hence no cache is being saved.`
|
2024-06-13 03:16:59 -07:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 05:21:32 -07:00
|
|
|
const archiveFolder = await utils.createTempDirectory()
|
|
|
|
|
const archivePath = path.join(
|
|
|
|
|
archiveFolder,
|
|
|
|
|
utils.getCacheFileName(compressionMethod)
|
2024-06-13 03:16:59 -07:00
|
|
|
)
|
|
|
|
|
|
2024-10-21 05:21:32 -07:00
|
|
|
core.debug(`Archive Path: ${archivePath}`)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await createTar(archiveFolder, cachePaths, compressionMethod)
|
|
|
|
|
if (core.isDebug()) {
|
|
|
|
|
await listTar(archivePath, compressionMethod)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath)
|
|
|
|
|
core.debug(`File Size: ${archiveFileSize}`)
|
|
|
|
|
|
2024-12-02 04:08:21 -08:00
|
|
|
// Set the archive size in the options, will be used to display the upload progress
|
2024-12-02 03:55:57 -08:00
|
|
|
options.archiveSizeBytes = archiveFileSize
|
|
|
|
|
|
2024-10-21 05:21:32 -07:00
|
|
|
core.debug('Reserving Cache')
|
|
|
|
|
const version = utils.getCacheVersion(
|
|
|
|
|
paths,
|
|
|
|
|
compressionMethod,
|
|
|
|
|
enableCrossOsArchive
|
|
|
|
|
)
|
|
|
|
|
const request: CreateCacheEntryRequest = {
|
2024-11-14 03:34:13 -08:00
|
|
|
key,
|
|
|
|
|
version
|
2024-10-21 05:21:32 -07:00
|
|
|
}
|
2024-11-14 06:49:55 -08:00
|
|
|
|
2025-02-25 12:49:08 -05:00
|
|
|
let signedUploadUrl
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await twirpClient.CreateCacheEntry(request)
|
|
|
|
|
if (!response.ok) {
|
2026-06-17 11:26:08 -10:00
|
|
|
// Skip the redundant inner warning when the receiver signalled a
|
|
|
|
|
// policy denial: the outer catch arm below will log a single
|
|
|
|
|
// customer-facing warning.
|
|
|
|
|
if (
|
|
|
|
|
response.message &&
|
|
|
|
|
!response.message.startsWith(CACHE_WRITE_DENIED_PREFIX)
|
|
|
|
|
) {
|
2025-08-13 13:00:46 +00:00
|
|
|
core.warning(`Cache reservation failed: ${response.message}`)
|
|
|
|
|
}
|
|
|
|
|
throw new Error(response.message || 'Response was not ok')
|
2025-02-25 12:49:08 -05:00
|
|
|
}
|
|
|
|
|
signedUploadUrl = response.signedUploadUrl
|
|
|
|
|
} catch (error) {
|
|
|
|
|
core.debug(`Failed to reserve cache: ${error}`)
|
2026-06-17 11:26:08 -10:00
|
|
|
const errorMessage = (error as Error)?.message ?? ''
|
|
|
|
|
if (errorMessage.startsWith(CACHE_WRITE_DENIED_PREFIX)) {
|
|
|
|
|
throw new CacheWriteDeniedError(
|
|
|
|
|
`Unable to reserve cache with key ${key}. More details: ${errorMessage}`
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-10-21 05:21:32 -07:00
|
|
|
throw new ReserveCacheError(
|
|
|
|
|
`Unable to reserve cache with key ${key}, another job may be creating this cache.`
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-06-13 03:16:59 -07:00
|
|
|
|
2024-11-14 04:33:31 -08:00
|
|
|
core.debug(`Attempting to upload cache located at: ${archivePath}`)
|
2024-11-28 07:22:01 -08:00
|
|
|
await cacheHttpClient.saveCache(
|
|
|
|
|
cacheId,
|
|
|
|
|
archivePath,
|
2025-02-25 12:49:08 -05:00
|
|
|
signedUploadUrl,
|
2024-11-28 07:22:01 -08:00
|
|
|
options
|
2024-11-26 00:56:07 +00:00
|
|
|
)
|
2024-10-21 05:21:32 -07:00
|
|
|
|
|
|
|
|
const finalizeRequest: FinalizeCacheEntryUploadRequest = {
|
2024-11-14 03:34:13 -08:00
|
|
|
key,
|
|
|
|
|
version,
|
2024-11-14 03:22:03 -08:00
|
|
|
sizeBytes: `${archiveFileSize}`
|
2024-10-21 05:21:32 -07:00
|
|
|
}
|
|
|
|
|
|
2024-11-14 03:22:03 -08:00
|
|
|
const finalizeResponse: FinalizeCacheEntryUploadResponse =
|
|
|
|
|
await twirpClient.FinalizeCacheEntryUpload(finalizeRequest)
|
2024-11-14 04:47:27 -08:00
|
|
|
core.debug(`FinalizeCacheEntryUploadResponse: ${finalizeResponse.ok}`)
|
2024-10-21 05:21:32 -07:00
|
|
|
|
|
|
|
|
if (!finalizeResponse.ok) {
|
2025-08-13 13:00:46 +00:00
|
|
|
if (finalizeResponse.message) {
|
|
|
|
|
throw new FinalizeCacheError(finalizeResponse.message)
|
|
|
|
|
}
|
2024-10-21 05:21:32 -07:00
|
|
|
throw new Error(
|
|
|
|
|
`Unable to finalize cache with key ${key}, another job may be finalizing this cache.`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cacheId = parseInt(finalizeResponse.entryId)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const typedError = error as Error
|
2024-11-22 09:01:59 -08:00
|
|
|
if (typedError.name === ValidationError.name) {
|
|
|
|
|
throw error
|
2026-06-17 11:26:08 -10:00
|
|
|
} else if (typedError.name === CacheWriteDeniedError.name) {
|
|
|
|
|
core.warning(`Failed to save: ${typedError.message}`)
|
2024-11-22 09:01:59 -08:00
|
|
|
} else if (typedError.name === ReserveCacheError.name) {
|
|
|
|
|
core.info(`Failed to save: ${typedError.message}`)
|
2025-08-13 13:00:46 +00:00
|
|
|
} else if (typedError.name === FinalizeCacheError.name) {
|
|
|
|
|
core.warning(typedError.message)
|
2024-11-22 09:01:59 -08:00
|
|
|
} else {
|
2025-07-14 12:42:37 +00:00
|
|
|
// Log server errors (5xx) as errors, all other errors as warnings
|
|
|
|
|
if (
|
|
|
|
|
typedError instanceof HttpClientError &&
|
|
|
|
|
typeof typedError.statusCode === 'number' &&
|
|
|
|
|
typedError.statusCode >= 500
|
|
|
|
|
) {
|
|
|
|
|
core.error(`Failed to save: ${typedError.message}`)
|
|
|
|
|
} else {
|
|
|
|
|
core.warning(`Failed to save: ${typedError.message}`)
|
|
|
|
|
}
|
2024-11-22 09:01:59 -08:00
|
|
|
}
|
2024-10-21 05:21:32 -07:00
|
|
|
} finally {
|
|
|
|
|
// Try to delete the archive to save space
|
|
|
|
|
try {
|
|
|
|
|
await utils.unlinkFile(archivePath)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
core.debug(`Failed to delete archive: ${error}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cacheId
|
2024-11-14 03:22:03 -08:00
|
|
|
}
|