From 45647689407e7fb224e06d066dde6aefa67a365f Mon Sep 17 00:00:00 2001 From: Rob Cowsill <42620235+rcowsill@users.noreply.github.com> Date: Mon, 28 Jun 2021 16:27:09 +0100 Subject: [PATCH] Delete temporary archive after cache upload (#792) This is to avoid filling the SSD while saving multiple large caches --- packages/cache/src/cache.ts | 41 ++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/cache/src/cache.ts b/packages/cache/src/cache.ts index 5e333dcc..951ef29c 100644 --- a/packages/cache/src/cache.ts +++ b/packages/cache/src/cache.ts @@ -166,24 +166,33 @@ export async function saveCache( core.debug(`Archive Path: ${archivePath}`) - await createTar(archiveFolder, cachePaths, compressionMethod) - if (core.isDebug()) { - await listTar(archivePath, compressionMethod) - } + try { + await createTar(archiveFolder, cachePaths, compressionMethod) + if (core.isDebug()) { + await listTar(archivePath, compressionMethod) + } - const fileSizeLimit = 5 * 1024 * 1024 * 1024 // 5GB per repo limit - const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath) - core.debug(`File Size: ${archiveFileSize}`) - if (archiveFileSize > fileSizeLimit) { - throw new Error( - `Cache size of ~${Math.round( - archiveFileSize / (1024 * 1024) - )} MB (${archiveFileSize} B) is over the 5GB limit, not saving cache.` - ) - } + const fileSizeLimit = 5 * 1024 * 1024 * 1024 // 5GB per repo limit + const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath) + core.debug(`File Size: ${archiveFileSize}`) + if (archiveFileSize > fileSizeLimit) { + throw new Error( + `Cache size of ~${Math.round( + archiveFileSize / (1024 * 1024) + )} MB (${archiveFileSize} B) is over the 5GB limit, not saving cache.` + ) + } - core.debug(`Saving Cache (ID: ${cacheId})`) - await cacheHttpClient.saveCache(cacheId, archivePath, options) + 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}`) + } + } return cacheId }