diff --git a/packages/cache/src/cache.ts b/packages/cache/src/cache.ts index 9356e838..6847cf0d 100644 --- a/packages/cache/src/cache.ts +++ b/packages/cache/src/cache.ts @@ -43,16 +43,6 @@ function checkKey(key: string): void { } } -/** - * isFeatureAvailable to check the presence of Actions cache service - * - * @returns boolean return true if Actions cache service feature is available, otherwise false - */ - -export function isFeatureAvailable(): boolean { - return !!process.env['ACTIONS_CACHE_URL'] -} - /** * Restores cache from keys * @@ -152,17 +142,7 @@ export async function saveCache( checkKey(key) const compressionMethod = await utils.getCompressionMethod() - - core.debug('Reserving Cache') - const cacheId = await cacheHttpClient.reserveCache(key, paths, { - compressionMethod - }) - if (cacheId === -1) { - throw new ReserveCacheError( - `Unable to reserve cache with key ${key}, another job may be creating this cache.` - ) - } - core.debug(`Cache ID: ${cacheId}`) + let cacheId = null const cachePaths = await utils.resolvePaths(paths) core.debug('Cache Paths:') @@ -185,7 +165,7 @@ export async function saveCache( const fileSizeLimit = 10 * 1024 * 1024 * 1024 // 10GB per repo limit const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath) core.debug(`File Size: ${archiveFileSize}`) - if (archiveFileSize > fileSizeLimit) { + if (archiveFileSize > fileSizeLimit && !utils.isGhes()) { throw new Error( `Cache size of ~${Math.round( archiveFileSize / (1024 * 1024) @@ -193,6 +173,24 @@ export async function saveCache( ) } + const cacheSize = utils.isGhes() ? archiveFileSize : undefined + core.debug('Reserving Cache') + cacheId = await cacheHttpClient.reserveCache(key, paths, { + compressionMethod, + cacheSize + }) + if (cacheId === -1) { + throw new ReserveCacheError( + `Unable to reserve cache with key ${key}, another job may be creating this cache.` + ) + } + if (cacheId === -2) { + throw new ReserveCacheError( + `Cache size of ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B) is over the data cap limit, not saving cache.` + ) + } + core.debug(`Cache ID: ${cacheId}`) + core.debug(`Saving Cache (ID: ${cacheId})`) await cacheHttpClient.saveCache(cacheId, archivePath, options) } finally { diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index 65d7bd08..e0f33bb0 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -31,7 +31,12 @@ import { const versionSalt = '1.0' function getCacheApiUrl(resource: string): string { - const baseUrl: string = process.env['ACTIONS_CACHE_URL'] || '' + // Ideally we just use ACTIONS_CACHE_URL + const baseUrl: string = ( + process.env['ACTIONS_CACHE_URL'] || + process.env['ACTIONS_RUNTIME_URL'] || + '' + ).replace('pipelines', 'artifactcache') if (!baseUrl) { throw new Error('Cache Service Url not found, unable to restore cache.') } @@ -149,7 +154,8 @@ export async function reserveCache( const reserveCacheRequest: ReserveCacheRequest = { key, - version + version, + cacheSize: options?.cacheSize } const response = await retryTypedResponse('reserveCache', async () => httpClient.postJson( @@ -157,6 +163,9 @@ export async function reserveCache( reserveCacheRequest ) ) + if(response?.statusCode === 400){ + return -2 + } return response?.result?.cacheId ?? -1 } diff --git a/packages/cache/src/internal/cacheUtils.ts b/packages/cache/src/internal/cacheUtils.ts index 41096102..aefb87b1 100644 --- a/packages/cache/src/internal/cacheUtils.ts +++ b/packages/cache/src/internal/cacheUtils.ts @@ -123,3 +123,10 @@ export function assertDefined(name: string, value?: T): T { return value } + +export function isGhes(): boolean { + const ghUrl = new URL( + process.env["GITHUB_SERVER_URL"] || "https://github.com" + ); + return ghUrl.hostname.toUpperCase() !== "GITHUB.COM"; +} diff --git a/packages/cache/src/internal/contracts.d.ts b/packages/cache/src/internal/contracts.d.ts index 80484769..ca6ea499 100644 --- a/packages/cache/src/internal/contracts.d.ts +++ b/packages/cache/src/internal/contracts.d.ts @@ -14,6 +14,7 @@ export interface CommitCacheRequest { export interface ReserveCacheRequest { key: string version?: string + cacheSize?: number } export interface ReserveCacheResponse { @@ -22,4 +23,5 @@ export interface ReserveCacheResponse { export interface InternalCacheOptions { compressionMethod?: CompressionMethod + cacheSize?: number }