diff --git a/dist/restore/index.js b/dist/restore/index.js index 8c765ad..bc3765f 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -6572,10 +6572,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.listObjects = exports.findObject = exports.setCacheHitOutput = exports.formatSize = exports.getInputAsInt = exports.getInputAsArray = exports.getInputAsBoolean = exports.newMinio = exports.isGhes = void 0; +exports.isExactKeyMatch = exports.saveMatchedKey = exports.listObjects = exports.findObject = exports.setCacheHitOutput = exports.formatSize = exports.getInputAsInt = exports.getInputAsArray = exports.getInputAsBoolean = exports.newMinio = exports.isGhes = void 0; const utils = __importStar(__webpack_require__(15)); const core = __importStar(__webpack_require__(470)); const minio = __importStar(__webpack_require__(223)); +const state_1 = __webpack_require__(179); function isGhes() { const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; @@ -6640,8 +6641,9 @@ function findObject(mc, bucket, keys, compressionMethod) { continue; } const sorted = objects.sort((a, b) => b.lastModified.getTime() - a.lastModified.getTime()); - core.debug(`Using latest ${JSON.stringify(sorted[0])}`); - return sorted[0]; + const result = { item: sorted[0], matchingKey: key }; + core.debug(`Using latest ${JSON.stringify(result)}`); + return result; } throw new Error("Cache item not found"); }); @@ -6670,6 +6672,21 @@ function listObjects(mc, bucket, prefix) { }); } exports.listObjects = listObjects; +function saveMatchedKey(matchedKey) { + return core.saveState(state_1.State.MatchedKey, matchedKey); +} +exports.saveMatchedKey = saveMatchedKey; +function getMatchedKey() { + return core.getState(state_1.State.MatchedKey); +} +function isExactKeyMatch() { + const matchedKey = getMatchedKey(); + const inputKey = core.getInput("key", { required: true }); + const result = getMatchedKey() === inputKey; + core.debug(`isExactKeyMatch: matchedKey=${matchedKey} inputKey=${inputKey}, result=${result}`); + return result; +} +exports.isExactKeyMatch = isExactKeyMatch; /***/ }), @@ -6931,7 +6948,20 @@ module.exports = from; /***/ }), /* 177 */, /* 178 */, -/* 179 */, +/* 179 */ +/***/ (function(__unusedmodule, exports) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +exports.State = void 0; +var State; +(function (State) { + State["MatchedKey"] = "matched-key"; +})(State = exports.State || (exports.State = {})); + + +/***/ }), /* 180 */, /* 181 */, /* 182 */, @@ -80811,8 +80841,9 @@ function restoreCache() { const cacheFileName = utils.getCacheFileName(compressionMethod); const archivePath = path.join(yield utils.createTempDirectory(), cacheFileName); const keys = [key, ...restoreKeys]; - const obj = yield utils_1.findObject(mc, bucket, keys, compressionMethod); + const { item: obj, matchingKey } = yield utils_1.findObject(mc, bucket, keys, compressionMethod); core.debug("found cache object"); + utils_1.saveMatchedKey(matchingKey); core.info(`Downloading cache from s3 to ${archivePath}. bucket: ${bucket}, object: ${obj.name}`); yield mc.fGetObject(bucket, obj.name, archivePath); if (core.isDebug()) { diff --git a/dist/save/index.js b/dist/save/index.js index 427bb59..7de64fe 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -6572,10 +6572,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.listObjects = exports.findObject = exports.setCacheHitOutput = exports.formatSize = exports.getInputAsInt = exports.getInputAsArray = exports.getInputAsBoolean = exports.newMinio = exports.isGhes = void 0; +exports.isExactKeyMatch = exports.saveMatchedKey = exports.listObjects = exports.findObject = exports.setCacheHitOutput = exports.formatSize = exports.getInputAsInt = exports.getInputAsArray = exports.getInputAsBoolean = exports.newMinio = exports.isGhes = void 0; const utils = __importStar(__webpack_require__(15)); const core = __importStar(__webpack_require__(470)); const minio = __importStar(__webpack_require__(223)); +const state_1 = __webpack_require__(179); function isGhes() { const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; @@ -6640,8 +6641,9 @@ function findObject(mc, bucket, keys, compressionMethod) { continue; } const sorted = objects.sort((a, b) => b.lastModified.getTime() - a.lastModified.getTime()); - core.debug(`Using latest ${JSON.stringify(sorted[0])}`); - return sorted[0]; + const result = { item: sorted[0], matchingKey: key }; + core.debug(`Using latest ${JSON.stringify(result)}`); + return result; } throw new Error("Cache item not found"); }); @@ -6670,6 +6672,21 @@ function listObjects(mc, bucket, prefix) { }); } exports.listObjects = listObjects; +function saveMatchedKey(matchedKey) { + return core.saveState(state_1.State.MatchedKey, matchedKey); +} +exports.saveMatchedKey = saveMatchedKey; +function getMatchedKey() { + return core.getState(state_1.State.MatchedKey); +} +function isExactKeyMatch() { + const matchedKey = getMatchedKey(); + const inputKey = core.getInput("key", { required: true }); + const result = getMatchedKey() === inputKey; + core.debug(`isExactKeyMatch: matchedKey=${matchedKey} inputKey=${inputKey}, result=${result}`); + return result; +} +exports.isExactKeyMatch = isExactKeyMatch; /***/ }), @@ -6931,7 +6948,20 @@ module.exports = from; /***/ }), /* 177 */, /* 178 */, -/* 179 */, +/* 179 */ +/***/ (function(__unusedmodule, exports) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +exports.State = void 0; +var State; +(function (State) { + State["MatchedKey"] = "matched-key"; +})(State = exports.State || (exports.State = {})); + + +/***/ }), /* 180 */, /* 181 */, /* 182 */, @@ -76653,6 +76683,10 @@ process.on("uncaughtException", (e) => core.info("warning: " + e.message)); function saveCache() { return __awaiter(this, void 0, void 0, function* () { try { + if (utils_1.isExactKeyMatch()) { + core.info("Cache was exact key match, not saving"); + return; + } const bucket = core.getInput("bucket", { required: true }); const key = core.getInput("key", { required: true }); const useFallback = utils_1.getInputAsBoolean("use-fallback"); diff --git a/src/restore.ts b/src/restore.ts index 9abe919..a00cf4e 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -11,6 +11,7 @@ import { isGhes, newMinio, setCacheHitOutput, + saveMatchedKey, } from "./utils"; process.on("uncaughtException", (e) => core.info("warning: " + e.message)); @@ -34,8 +35,9 @@ async function restoreCache() { ); const keys = [key, ...restoreKeys]; - const obj = await findObject(mc, bucket, keys, compressionMethod); + const { item: obj, matchingKey } = await findObject(mc, bucket, keys, compressionMethod); core.debug("found cache object"); + saveMatchedKey(matchingKey); core.info( `Downloading cache from s3 to ${archivePath}. bucket: ${bucket}, object: ${obj.name}` ); diff --git a/src/save.ts b/src/save.ts index ad98705..734c51d 100644 --- a/src/save.ts +++ b/src/save.ts @@ -3,12 +3,17 @@ import * as utils from "@actions/cache/lib/internal/cacheUtils"; import { createTar, listTar } from "@actions/cache/lib/internal/tar"; import * as core from "@actions/core"; import * as path from "path"; -import { getInputAsArray, getInputAsBoolean, isGhes, newMinio } from "./utils"; +import { getInputAsArray, getInputAsBoolean, isGhes, newMinio, isExactKeyMatch } from "./utils"; process.on("uncaughtException", (e) => core.info("warning: " + e.message)); async function saveCache() { try { + if (isExactKeyMatch()) { + core.info("Cache was exact key match, not saving"); + return + } + const bucket = core.getInput("bucket", { required: true }); const key = core.getInput("key", { required: true }); const useFallback = getInputAsBoolean("use-fallback"); diff --git a/src/state.ts b/src/state.ts new file mode 100644 index 0000000..fdabb09 --- /dev/null +++ b/src/state.ts @@ -0,0 +1,3 @@ +export enum State { + "MatchedKey" = "matched-key" +} diff --git a/src/utils.ts b/src/utils.ts index e39e721..b6a305d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,6 +2,7 @@ import { CompressionMethod } from "@actions/cache/lib/internal/constants"; import * as utils from "@actions/cache/lib/internal/cacheUtils"; import * as core from "@actions/core"; import * as minio from "minio"; +import {State} from "./state"; export function isGhes(): boolean { const ghUrl = new URL( @@ -66,12 +67,17 @@ export function setCacheHitOutput(isCacheHit: boolean): void { core.setOutput("cache-hit", isCacheHit.toString()); } +type FindObjectResult = { + item: minio.BucketItem + matchingKey: string +} + export async function findObject( mc: minio.Client, bucket: string, keys: string[], compressionMethod: CompressionMethod -): Promise { +): Promise { core.debug("Restore keys: " + JSON.stringify(keys)); for (const key of keys) { const fn = utils.getCacheFileName(compressionMethod); @@ -85,8 +91,9 @@ export async function findObject( const sorted = objects.sort( (a, b) => b.lastModified.getTime() - a.lastModified.getTime() ); - core.debug(`Using latest ${JSON.stringify(sorted[0])}`); - return sorted[0]; + const result = { item: sorted[0], matchingKey: key }; + core.debug(`Using latest ${JSON.stringify(result)}`); + return result; } throw new Error("Cache item not found"); } @@ -117,3 +124,20 @@ export function listObjects( }, 10000); }); } + +export function saveMatchedKey(matchedKey: string) { + return core.saveState(State.MatchedKey, matchedKey); +} + +function getMatchedKey() { + return core.getState(State.MatchedKey); +} + +export function isExactKeyMatch(): boolean { + const matchedKey = getMatchedKey(); + const inputKey = core.getInput("key", { required: true }); + const result = getMatchedKey() === inputKey + core.debug(`isExactKeyMatch: matchedKey=${matchedKey} inputKey=${inputKey}, result=${result}`) + return result +} +