From 29a562ab28a0e967d7c7bba9bae1dd6c8cf710e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Thu, 23 Oct 2025 12:42:38 +0900 Subject: [PATCH] optimize: skip cache cleaning and save if already cached --- dist/restore/index.js | 50 +++++++++++++++++++++++++--------- dist/save/index.js | 63 ++++++++++++++++++++++++++++++++++--------- src/save.ts | 13 +++++++++ 3 files changed, 102 insertions(+), 24 deletions(-) diff --git a/dist/restore/index.js b/dist/restore/index.js index ba5f018..6e8c7ea 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -2737,7 +2737,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.saveCache = exports.restoreCache = exports.isFeatureAvailable = exports.ReserveCacheError = exports.ValidationError = void 0; +exports.saveCache = exports.restoreCache = exports.isFeatureAvailable = exports.FinalizeCacheError = exports.ReserveCacheError = exports.ValidationError = void 0; const core = __importStar(__nccwpck_require__(37484)); const path = __importStar(__nccwpck_require__(16928)); const utils = __importStar(__nccwpck_require__(98299)); @@ -2745,7 +2745,6 @@ const cacheHttpClient = __importStar(__nccwpck_require__(73171)); const cacheTwirpClient = __importStar(__nccwpck_require__(96819)); const config_1 = __nccwpck_require__(17606); const tar_1 = __nccwpck_require__(95321); -const constants_1 = __nccwpck_require__(58287); const http_client_1 = __nccwpck_require__(54844); class ValidationError extends Error { constructor(message) { @@ -2763,6 +2762,14 @@ class ReserveCacheError extends Error { } } exports.ReserveCacheError = ReserveCacheError; +class FinalizeCacheError extends Error { + constructor(message) { + super(message); + this.name = 'FinalizeCacheError'; + Object.setPrototypeOf(this, FinalizeCacheError.prototype); + } +} +exports.FinalizeCacheError = FinalizeCacheError; function checkPaths(paths) { if (!paths || paths.length === 0) { throw new ValidationError(`Path Validation Error: At least one directory or file path is required`); @@ -3139,10 +3146,6 @@ function saveCacheV2(paths, key, options, enableCrossOsArchive = false) { } const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath); core.debug(`File Size: ${archiveFileSize}`); - // For GHES, this check will take place in ReserveCache API with enterprise file size limit - if (archiveFileSize > constants_1.CacheFileSizeLimit && !(0, config_1.isGhes)()) { - throw new Error(`Cache size of ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B) is over the 10GB limit, not saving cache.`); - } // Set the archive size in the options, will be used to display the upload progress options.archiveSizeBytes = archiveFileSize; core.debug('Reserving Cache'); @@ -3155,7 +3158,10 @@ function saveCacheV2(paths, key, options, enableCrossOsArchive = false) { try { const response = yield twirpClient.CreateCacheEntry(request); if (!response.ok) { - throw new Error('Response was not ok'); + if (response.message) { + core.warning(`Cache reservation failed: ${response.message}`); + } + throw new Error(response.message || 'Response was not ok'); } signedUploadUrl = response.signedUploadUrl; } @@ -3173,6 +3179,9 @@ function saveCacheV2(paths, key, options, enableCrossOsArchive = false) { const finalizeResponse = yield twirpClient.FinalizeCacheEntryUpload(finalizeRequest); core.debug(`FinalizeCacheEntryUploadResponse: ${finalizeResponse.ok}`); if (!finalizeResponse.ok) { + if (finalizeResponse.message) { + throw new FinalizeCacheError(finalizeResponse.message); + } throw new Error(`Unable to finalize cache with key ${key}, another job may be finalizing this cache.`); } cacheId = parseInt(finalizeResponse.entryId); @@ -3185,6 +3194,9 @@ function saveCacheV2(paths, key, options, enableCrossOsArchive = false) { else if (typedError.name === ReserveCacheError.name) { core.info(`Failed to save: ${typedError.message}`); } + else if (typedError.name === FinalizeCacheError.name) { + core.warning(typedError.message); + } else { // Log server errors (5xx) as errors, all other errors as warnings if (typedError instanceof http_client_1.HttpClientError && @@ -3296,11 +3308,12 @@ class CreateCacheEntryResponse$Type extends runtime_5.MessageType { constructor() { super("github.actions.results.api.v1.CreateCacheEntryResponse", [ { no: 1, name: "ok", kind: "scalar", T: 8 /*ScalarType.BOOL*/ }, - { no: 2, name: "signed_upload_url", kind: "scalar", T: 9 /*ScalarType.STRING*/ } + { no: 2, name: "signed_upload_url", kind: "scalar", T: 9 /*ScalarType.STRING*/ }, + { no: 3, name: "message", kind: "scalar", T: 9 /*ScalarType.STRING*/ } ]); } create(value) { - const message = { ok: false, signedUploadUrl: "" }; + const message = { ok: false, signedUploadUrl: "", message: "" }; globalThis.Object.defineProperty(message, runtime_4.MESSAGE_TYPE, { enumerable: false, value: this }); if (value !== undefined) (0, runtime_3.reflectionMergePartial)(this, message, value); @@ -3317,6 +3330,9 @@ class CreateCacheEntryResponse$Type extends runtime_5.MessageType { case /* string signed_upload_url */ 2: message.signedUploadUrl = reader.string(); break; + case /* string message */ 3: + message.message = reader.string(); + break; default: let u = options.readUnknownField; if (u === "throw") @@ -3335,6 +3351,9 @@ class CreateCacheEntryResponse$Type extends runtime_5.MessageType { /* string signed_upload_url = 2; */ if (message.signedUploadUrl !== "") writer.tag(2, runtime_1.WireType.LengthDelimited).string(message.signedUploadUrl); + /* string message = 3; */ + if (message.message !== "") + writer.tag(3, runtime_1.WireType.LengthDelimited).string(message.message); let u = options.writeUnknownFields; if (u !== false) (u == true ? runtime_2.UnknownFieldHandler.onWrite : u)(this.typeName, message, writer); @@ -3418,11 +3437,12 @@ class FinalizeCacheEntryUploadResponse$Type extends runtime_5.MessageType { constructor() { super("github.actions.results.api.v1.FinalizeCacheEntryUploadResponse", [ { no: 1, name: "ok", kind: "scalar", T: 8 /*ScalarType.BOOL*/ }, - { no: 2, name: "entry_id", kind: "scalar", T: 3 /*ScalarType.INT64*/ } + { no: 2, name: "entry_id", kind: "scalar", T: 3 /*ScalarType.INT64*/ }, + { no: 3, name: "message", kind: "scalar", T: 9 /*ScalarType.STRING*/ } ]); } create(value) { - const message = { ok: false, entryId: "0" }; + const message = { ok: false, entryId: "0", message: "" }; globalThis.Object.defineProperty(message, runtime_4.MESSAGE_TYPE, { enumerable: false, value: this }); if (value !== undefined) (0, runtime_3.reflectionMergePartial)(this, message, value); @@ -3439,6 +3459,9 @@ class FinalizeCacheEntryUploadResponse$Type extends runtime_5.MessageType { case /* int64 entry_id */ 2: message.entryId = reader.int64().toString(); break; + case /* string message */ 3: + message.message = reader.string(); + break; default: let u = options.readUnknownField; if (u === "throw") @@ -3457,6 +3480,9 @@ class FinalizeCacheEntryUploadResponse$Type extends runtime_5.MessageType { /* int64 entry_id = 2; */ if (message.entryId !== "0") writer.tag(2, runtime_1.WireType.Varint).int64(message.entryId); + /* string message = 3; */ + if (message.message !== "") + writer.tag(3, runtime_1.WireType.LengthDelimited).string(message.message); let u = options.writeUnknownFields; if (u !== false) (u == true ? runtime_2.UnknownFieldHandler.onWrite : u)(this.typeName, message, writer); @@ -147306,7 +147332,7 @@ module.exports = axios; /***/ ((module) => { "use strict"; -module.exports = /*#__PURE__*/JSON.parse('{"name":"@actions/cache","version":"4.0.5","preview":true,"description":"Actions cache lib","keywords":["github","actions","cache"],"homepage":"https://github.com/actions/toolkit/tree/main/packages/cache","license":"MIT","main":"lib/cache.js","types":"lib/cache.d.ts","directories":{"lib":"lib","test":"__tests__"},"files":["lib","!.DS_Store"],"publishConfig":{"access":"public"},"repository":{"type":"git","url":"git+https://github.com/actions/toolkit.git","directory":"packages/cache"},"scripts":{"audit-moderate":"npm install && npm audit --json --audit-level=moderate > audit.json","test":"echo \\"Error: run tests from root\\" && exit 1","tsc":"tsc"},"bugs":{"url":"https://github.com/actions/toolkit/issues"},"dependencies":{"@actions/core":"^1.11.1","@actions/exec":"^1.0.1","@actions/glob":"^0.1.0","@protobuf-ts/runtime-rpc":"^2.11.1","@actions/http-client":"^2.1.1","@actions/io":"^1.0.1","@azure/abort-controller":"^1.1.0","@azure/ms-rest-js":"^2.6.0","@azure/storage-blob":"^12.13.0","semver":"^6.3.1"},"devDependencies":{"@types/node":"^22.13.9","@types/semver":"^6.0.0","@protobuf-ts/plugin":"^2.9.4","typescript":"^5.2.2"}}'); +module.exports = /*#__PURE__*/JSON.parse('{"name":"@actions/cache","version":"4.1.0","preview":true,"description":"Actions cache lib","keywords":["github","actions","cache"],"homepage":"https://github.com/actions/toolkit/tree/main/packages/cache","license":"MIT","main":"lib/cache.js","types":"lib/cache.d.ts","directories":{"lib":"lib","test":"__tests__"},"files":["lib","!.DS_Store"],"publishConfig":{"access":"public"},"repository":{"type":"git","url":"git+https://github.com/actions/toolkit.git","directory":"packages/cache"},"scripts":{"audit-moderate":"npm install && npm audit --json --audit-level=moderate > audit.json","test":"echo \\"Error: run tests from root\\" && exit 1","tsc":"tsc"},"bugs":{"url":"https://github.com/actions/toolkit/issues"},"dependencies":{"@actions/core":"^1.11.1","@actions/exec":"^1.0.1","@actions/glob":"^0.1.0","@protobuf-ts/runtime-rpc":"^2.11.1","@actions/http-client":"^2.1.1","@actions/io":"^1.0.1","@azure/abort-controller":"^1.1.0","@azure/ms-rest-js":"^2.6.0","@azure/storage-blob":"^12.13.0","semver":"^6.3.1"},"devDependencies":{"@types/node":"^22.13.9","@types/semver":"^6.0.0","@protobuf-ts/plugin":"^2.9.4","typescript":"^5.2.2"}}'); /***/ }), diff --git a/dist/save/index.js b/dist/save/index.js index de72f4d..10e19d8 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -2737,7 +2737,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.saveCache = exports.restoreCache = exports.isFeatureAvailable = exports.ReserveCacheError = exports.ValidationError = void 0; +exports.saveCache = exports.restoreCache = exports.isFeatureAvailable = exports.FinalizeCacheError = exports.ReserveCacheError = exports.ValidationError = void 0; const core = __importStar(__nccwpck_require__(37484)); const path = __importStar(__nccwpck_require__(16928)); const utils = __importStar(__nccwpck_require__(98299)); @@ -2745,7 +2745,6 @@ const cacheHttpClient = __importStar(__nccwpck_require__(73171)); const cacheTwirpClient = __importStar(__nccwpck_require__(96819)); const config_1 = __nccwpck_require__(17606); const tar_1 = __nccwpck_require__(95321); -const constants_1 = __nccwpck_require__(58287); const http_client_1 = __nccwpck_require__(54844); class ValidationError extends Error { constructor(message) { @@ -2763,6 +2762,14 @@ class ReserveCacheError extends Error { } } exports.ReserveCacheError = ReserveCacheError; +class FinalizeCacheError extends Error { + constructor(message) { + super(message); + this.name = 'FinalizeCacheError'; + Object.setPrototypeOf(this, FinalizeCacheError.prototype); + } +} +exports.FinalizeCacheError = FinalizeCacheError; function checkPaths(paths) { if (!paths || paths.length === 0) { throw new ValidationError(`Path Validation Error: At least one directory or file path is required`); @@ -3139,10 +3146,6 @@ function saveCacheV2(paths, key, options, enableCrossOsArchive = false) { } const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath); core.debug(`File Size: ${archiveFileSize}`); - // For GHES, this check will take place in ReserveCache API with enterprise file size limit - if (archiveFileSize > constants_1.CacheFileSizeLimit && !(0, config_1.isGhes)()) { - throw new Error(`Cache size of ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B) is over the 10GB limit, not saving cache.`); - } // Set the archive size in the options, will be used to display the upload progress options.archiveSizeBytes = archiveFileSize; core.debug('Reserving Cache'); @@ -3155,7 +3158,10 @@ function saveCacheV2(paths, key, options, enableCrossOsArchive = false) { try { const response = yield twirpClient.CreateCacheEntry(request); if (!response.ok) { - throw new Error('Response was not ok'); + if (response.message) { + core.warning(`Cache reservation failed: ${response.message}`); + } + throw new Error(response.message || 'Response was not ok'); } signedUploadUrl = response.signedUploadUrl; } @@ -3173,6 +3179,9 @@ function saveCacheV2(paths, key, options, enableCrossOsArchive = false) { const finalizeResponse = yield twirpClient.FinalizeCacheEntryUpload(finalizeRequest); core.debug(`FinalizeCacheEntryUploadResponse: ${finalizeResponse.ok}`); if (!finalizeResponse.ok) { + if (finalizeResponse.message) { + throw new FinalizeCacheError(finalizeResponse.message); + } throw new Error(`Unable to finalize cache with key ${key}, another job may be finalizing this cache.`); } cacheId = parseInt(finalizeResponse.entryId); @@ -3185,6 +3194,9 @@ function saveCacheV2(paths, key, options, enableCrossOsArchive = false) { else if (typedError.name === ReserveCacheError.name) { core.info(`Failed to save: ${typedError.message}`); } + else if (typedError.name === FinalizeCacheError.name) { + core.warning(typedError.message); + } else { // Log server errors (5xx) as errors, all other errors as warnings if (typedError instanceof http_client_1.HttpClientError && @@ -3296,11 +3308,12 @@ class CreateCacheEntryResponse$Type extends runtime_5.MessageType { constructor() { super("github.actions.results.api.v1.CreateCacheEntryResponse", [ { no: 1, name: "ok", kind: "scalar", T: 8 /*ScalarType.BOOL*/ }, - { no: 2, name: "signed_upload_url", kind: "scalar", T: 9 /*ScalarType.STRING*/ } + { no: 2, name: "signed_upload_url", kind: "scalar", T: 9 /*ScalarType.STRING*/ }, + { no: 3, name: "message", kind: "scalar", T: 9 /*ScalarType.STRING*/ } ]); } create(value) { - const message = { ok: false, signedUploadUrl: "" }; + const message = { ok: false, signedUploadUrl: "", message: "" }; globalThis.Object.defineProperty(message, runtime_4.MESSAGE_TYPE, { enumerable: false, value: this }); if (value !== undefined) (0, runtime_3.reflectionMergePartial)(this, message, value); @@ -3317,6 +3330,9 @@ class CreateCacheEntryResponse$Type extends runtime_5.MessageType { case /* string signed_upload_url */ 2: message.signedUploadUrl = reader.string(); break; + case /* string message */ 3: + message.message = reader.string(); + break; default: let u = options.readUnknownField; if (u === "throw") @@ -3335,6 +3351,9 @@ class CreateCacheEntryResponse$Type extends runtime_5.MessageType { /* string signed_upload_url = 2; */ if (message.signedUploadUrl !== "") writer.tag(2, runtime_1.WireType.LengthDelimited).string(message.signedUploadUrl); + /* string message = 3; */ + if (message.message !== "") + writer.tag(3, runtime_1.WireType.LengthDelimited).string(message.message); let u = options.writeUnknownFields; if (u !== false) (u == true ? runtime_2.UnknownFieldHandler.onWrite : u)(this.typeName, message, writer); @@ -3418,11 +3437,12 @@ class FinalizeCacheEntryUploadResponse$Type extends runtime_5.MessageType { constructor() { super("github.actions.results.api.v1.FinalizeCacheEntryUploadResponse", [ { no: 1, name: "ok", kind: "scalar", T: 8 /*ScalarType.BOOL*/ }, - { no: 2, name: "entry_id", kind: "scalar", T: 3 /*ScalarType.INT64*/ } + { no: 2, name: "entry_id", kind: "scalar", T: 3 /*ScalarType.INT64*/ }, + { no: 3, name: "message", kind: "scalar", T: 9 /*ScalarType.STRING*/ } ]); } create(value) { - const message = { ok: false, entryId: "0" }; + const message = { ok: false, entryId: "0", message: "" }; globalThis.Object.defineProperty(message, runtime_4.MESSAGE_TYPE, { enumerable: false, value: this }); if (value !== undefined) (0, runtime_3.reflectionMergePartial)(this, message, value); @@ -3439,6 +3459,9 @@ class FinalizeCacheEntryUploadResponse$Type extends runtime_5.MessageType { case /* int64 entry_id */ 2: message.entryId = reader.int64().toString(); break; + case /* string message */ 3: + message.message = reader.string(); + break; default: let u = options.readUnknownField; if (u === "throw") @@ -3457,6 +3480,9 @@ class FinalizeCacheEntryUploadResponse$Type extends runtime_5.MessageType { /* int64 entry_id = 2; */ if (message.entryId !== "0") writer.tag(2, runtime_1.WireType.Varint).int64(message.entryId); + /* string message = 3; */ + if (message.message !== "") + writer.tag(3, runtime_1.WireType.LengthDelimited).string(message.message); let u = options.writeUnknownFields; if (u !== false) (u == true ? runtime_2.UnknownFieldHandler.onWrite : u)(this.typeName, message, writer); @@ -147306,7 +147332,7 @@ module.exports = axios; /***/ ((module) => { "use strict"; -module.exports = /*#__PURE__*/JSON.parse('{"name":"@actions/cache","version":"4.0.5","preview":true,"description":"Actions cache lib","keywords":["github","actions","cache"],"homepage":"https://github.com/actions/toolkit/tree/main/packages/cache","license":"MIT","main":"lib/cache.js","types":"lib/cache.d.ts","directories":{"lib":"lib","test":"__tests__"},"files":["lib","!.DS_Store"],"publishConfig":{"access":"public"},"repository":{"type":"git","url":"git+https://github.com/actions/toolkit.git","directory":"packages/cache"},"scripts":{"audit-moderate":"npm install && npm audit --json --audit-level=moderate > audit.json","test":"echo \\"Error: run tests from root\\" && exit 1","tsc":"tsc"},"bugs":{"url":"https://github.com/actions/toolkit/issues"},"dependencies":{"@actions/core":"^1.11.1","@actions/exec":"^1.0.1","@actions/glob":"^0.1.0","@protobuf-ts/runtime-rpc":"^2.11.1","@actions/http-client":"^2.1.1","@actions/io":"^1.0.1","@azure/abort-controller":"^1.1.0","@azure/ms-rest-js":"^2.6.0","@azure/storage-blob":"^12.13.0","semver":"^6.3.1"},"devDependencies":{"@types/node":"^22.13.9","@types/semver":"^6.0.0","@protobuf-ts/plugin":"^2.9.4","typescript":"^5.2.2"}}'); +module.exports = /*#__PURE__*/JSON.parse('{"name":"@actions/cache","version":"4.1.0","preview":true,"description":"Actions cache lib","keywords":["github","actions","cache"],"homepage":"https://github.com/actions/toolkit/tree/main/packages/cache","license":"MIT","main":"lib/cache.js","types":"lib/cache.d.ts","directories":{"lib":"lib","test":"__tests__"},"files":["lib","!.DS_Store"],"publishConfig":{"access":"public"},"repository":{"type":"git","url":"git+https://github.com/actions/toolkit.git","directory":"packages/cache"},"scripts":{"audit-moderate":"npm install && npm audit --json --audit-level=moderate > audit.json","test":"echo \\"Error: run tests from root\\" && exit 1","tsc":"tsc"},"bugs":{"url":"https://github.com/actions/toolkit/issues"},"dependencies":{"@actions/core":"^1.11.1","@actions/exec":"^1.0.1","@actions/glob":"^0.1.0","@protobuf-ts/runtime-rpc":"^2.11.1","@actions/http-client":"^2.1.1","@actions/io":"^1.0.1","@azure/abort-controller":"^1.1.0","@azure/ms-rest-js":"^2.6.0","@azure/storage-blob":"^12.13.0","semver":"^6.3.1"},"devDependencies":{"@types/node":"^22.13.9","@types/semver":"^6.0.0","@protobuf-ts/plugin":"^2.9.4","typescript":"^5.2.2"}}'); /***/ }), @@ -149346,6 +149372,19 @@ async function run() { const config = CacheConfig.fromState(); config.printInfo(cacheProvider); core.info(""); + try { + core.info(`... Checking cache for key "${config.cacheKey}" ...`); + const cacheKey = await cacheProvider.cache.restoreCache(config.cachePaths.slice(), config.cacheKey, [], { + lookupOnly: true, + }); + if (cacheKey) { + core.info(`... Skipping since cache found for key "${cacheKey}".`); + return; + } + } + catch (e) { + // ignore if something went wrong + } // TODO: remove this once https://github.com/actions/toolkit/pull/553 lands if (process.env["RUNNER_OS"] == "macOS") { await macOsWorkaround(); diff --git a/src/save.ts b/src/save.ts index 10fe79d..c9d49fb 100644 --- a/src/save.ts +++ b/src/save.ts @@ -31,6 +31,19 @@ async function run() { config.printInfo(cacheProvider); core.info(""); + try { + core.info(`... Checking cache for key "${config.cacheKey}" ...`); + const cacheKey= await cacheProvider.cache.restoreCache(config.cachePaths.slice(), config.cacheKey, [], { + lookupOnly: true, + }); + if (cacheKey) { + core.info(`... Skipping since cache found for key "${cacheKey}".`); + return; + } + } catch (e) { + // ignore if something went wrong + } + // TODO: remove this once https://github.com/actions/toolkit/pull/553 lands if (process.env["RUNNER_OS"] == "macOS") { await macOsWorkaround();