Skip to content

Commit

Permalink
Add support for lookup-only option
Browse files Browse the repository at this point in the history
This adds the lookup-only option to check if the cache exists without downloading it.
  • Loading branch information
beagleknight committed Jul 25, 2023
1 parent 55703e3 commit 5f7dbe6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ inputs:
description: "Use github actions/cache as fallback"
required: false
default: "true"
lookup-only:
description: 'Check if a cache entry exists for the given input(s) (key, restore-keys) without downloading the cache'
default: 'false'
required: false
# zip-option:
# description: zip options
# required: false
Expand Down
25 changes: 14 additions & 11 deletions dist/restore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107395,6 +107395,7 @@ function restoreCache() {
const paths = (0, utils_1.getInputAsArray)("path");
const restoreKeys = (0, utils_1.getInputAsArray)("restore-keys");
const local = core.getInput("local");
const lookupOnly = (0, utils_1.getInputAsBoolean)('lookup-only');
try {
const compressionMethod = yield utils.getCompressionMethod();
const cacheFileName = utils.getCacheFileName(compressionMethod);
Expand All @@ -107404,7 +107405,7 @@ function restoreCache() {
core.saveState(state_1.State.AccessKey, core.getInput("accessKey"));
core.saveState(state_1.State.SecretKey, core.getInput("secretKey"));
core.saveState(state_1.State.SessionToken, core.getInput("sessionToken"));
if (local) {
if (local && !lookupOnly) {
core.info('Local cache is enabled');
const localKey = path.join(local, key, cacheFileName);
core.info(`Looking for exact match: ${localKey}`);
Expand All @@ -107427,17 +107428,19 @@ function restoreCache() {
}
const mc = (0, utils_1.newMinio)();
const { item: obj, matchingKey } = yield (0, utils_1.findObject)(mc, bucket, key, restoreKeys, compressionMethod);
core.debug("found cache object");
(0, 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()) {
yield (0, tar_1.listTar)(archivePath, compressionMethod);
}
core.info(`Cache Size: ${(0, utils_1.formatSize)(obj.size)} (${obj.size} bytes)`);
yield (0, tar_1.extractTar)(archivePath, compressionMethod);
if (!lookupOnly) {
core.debug("found cache object");
(0, 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()) {
yield (0, tar_1.listTar)(archivePath, compressionMethod);
}
core.info(`Cache Size: ${(0, utils_1.formatSize)(obj.size)} (${obj.size} bytes)`);
yield (0, tar_1.extractTar)(archivePath, compressionMethod);
core.info("Cache restored from s3 successfully");
}
(0, utils_1.setCacheHitOutput)(matchingKey === key);
core.info("Cache restored from s3 successfully");
}
catch (e) {
core.info("Restore s3 cache failed: " + e.message);
Expand Down
31 changes: 18 additions & 13 deletions src/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async function restoreCache() {
const paths = getInputAsArray("path");
const restoreKeys = getInputAsArray("restore-keys");
const local = core.getInput("local");
const lookupOnly = getInputAsBoolean('lookup-only');

try {
const compressionMethod = await utils.getCompressionMethod();
Expand All @@ -42,7 +43,7 @@ async function restoreCache() {
core.saveState(State.SecretKey, core.getInput("secretKey"));
core.saveState(State.SessionToken, core.getInput("sessionToken"));

if (local) {
if (local && !lookupOnly) {
core.info('Local cache is enabled')

const localKey = path.join(local, key, cacheFileName)
Expand Down Expand Up @@ -78,22 +79,26 @@ async function restoreCache() {
restoreKeys,
compressionMethod
);
core.debug("found cache object");
saveMatchedKey(matchingKey);
core.info(
`Downloading cache from s3 to ${archivePath}. bucket: ${bucket}, object: ${obj.name}`
);
await mc.fGetObject(bucket, obj.name, archivePath);

if (!lookupOnly) {
core.debug("found cache object");
saveMatchedKey(matchingKey);
core.info(
`Downloading cache from s3 to ${archivePath}. bucket: ${bucket}, object: ${obj.name}`
);
await mc.fGetObject(bucket, obj.name, archivePath);

if (core.isDebug()) {
await listTar(archivePath, compressionMethod);
}

if (core.isDebug()) {
await listTar(archivePath, compressionMethod);
}
core.info(`Cache Size: ${formatSize(obj.size)} (${obj.size} bytes)`);

core.info(`Cache Size: ${formatSize(obj.size)} (${obj.size} bytes)`);
await extractTar(archivePath, compressionMethod);
core.info("Cache restored from s3 successfully");
}

await extractTar(archivePath, compressionMethod);
setCacheHitOutput(matchingKey === key);
core.info("Cache restored from s3 successfully");
} catch (e: any) {
core.info("Restore s3 cache failed: " + e.message);
setCacheHitOutput(false);
Expand Down

0 comments on commit 5f7dbe6

Please sign in to comment.