Skip to content

Commit

Permalink
not saving cache on exact key hit
Browse files Browse the repository at this point in the history
this replicates the behavior of GitHub's cache action
  • Loading branch information
Roy Razon committed Feb 9, 2022
1 parent f55d97d commit 1cc2bf2
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 14 deletions.
41 changes: 36 additions & 5 deletions dist/restore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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");
});
Expand Down Expand Up @@ -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;


/***/ }),
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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()) {
Expand Down
42 changes: 38 additions & 4 deletions dist/save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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");
});
Expand Down Expand Up @@ -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;


/***/ }),
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion src/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isGhes,
newMinio,
setCacheHitOutput,
saveMatchedKey,
} from "./utils";

process.on("uncaughtException", (e) => core.info("warning: " + e.message));
Expand All @@ -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}`
);
Expand Down
7 changes: 6 additions & 1 deletion src/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 3 additions & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum State {
"MatchedKey" = "matched-key"
}
30 changes: 27 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<minio.BucketItem> {
): Promise<FindObjectResult> {
core.debug("Restore keys: " + JSON.stringify(keys));
for (const key of keys) {
const fn = utils.getCacheFileName(compressionMethod);
Expand All @@ -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");
}
Expand Down Expand Up @@ -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
}

0 comments on commit 1cc2bf2

Please sign in to comment.