From 1dae85574671bf3939d2b9c524f1a5cfc3ca8e58 Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Tue, 6 Dec 2022 11:38:17 +0000 Subject: [PATCH] Add fallback to gzip compression if cache not found --- packages/cache/src/cache.ts | 39 +++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/packages/cache/src/cache.ts b/packages/cache/src/cache.ts index 609c7f94..d29eeda2 100644 --- a/packages/cache/src/cache.ts +++ b/packages/cache/src/cache.ts @@ -4,6 +4,8 @@ import * as utils from './internal/cacheUtils' import * as cacheHttpClient from './internal/cacheHttpClient' import {createTar, extractTar, listTar} from './internal/tar' import {DownloadOptions, UploadOptions} from './options' +import {CompressionMethod} from './internal/constants' +import {ArtifactCacheEntry} from './internal/contracts' export class ValidationError extends Error { constructor(message: string) { @@ -85,17 +87,38 @@ export async function restoreCache( checkKey(key) } - const compressionMethod = await utils.getCompressionMethod() + let cacheEntry: ArtifactCacheEntry | null + let compressionMethod = await utils.getCompressionMethod() let archivePath = '' try { - // path are needed to compute version - const cacheEntry = await cacheHttpClient.getCacheEntry(keys, paths, { - compressionMethod - }) + try { + // path are needed to compute version + cacheEntry = await cacheHttpClient.getCacheEntry(keys, paths, { + compressionMethod + }) - if (!cacheEntry?.archiveLocation) { - // Cache not found - return undefined + if (!cacheEntry?.archiveLocation) { + // Cache not found + return undefined + } + } catch (error) { + if ( + process.platform == 'win32' && + compressionMethod != CompressionMethod.Gzip + ) { + // On windows, we will try to download the cache entry with the same key + // but with different compression method. This is to support the old cache entry created + // by the old version of the cache action. + compressionMethod = CompressionMethod.Gzip + cacheEntry = await cacheHttpClient.getCacheEntry(keys, paths, { + compressionMethod + }) + if (!cacheEntry?.archiveLocation) { + throw error + } + } else { + throw error + } } archivePath = path.join(