Skip to content

Commit

Permalink
Adds an option to do lookup-only of the cache (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
danlec authored Nov 8, 2024
1 parent 68b3cb7 commit c00f302
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ sensible defaults.
# default: "false"
cache-all-crates: ""

# Determiners whether the cache should be saved.
# Determines whether the cache should be saved.
# If `false`, the cache is only restored.
# Useful for jobs where the matrix is additive e.g. additional Cargo features,
# or when only runs from `master` should be saved to the cache.
Expand All @@ -69,6 +69,12 @@ sensible defaults.
# To only cache runs from `master`:
save-if: ${{ github.ref == 'refs/heads/master' }}

# Determines whether the cache should be restored.
# If `true` the cache key will be checked and the `cache-hit` output will be set
# but the cache itself won't be restored
# default: "false"
lookup-only: ""

# Specifies what to use as the backend providing cache
# Can be set to either "github" or "buildjet"
# default: "github"
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ inputs:
description: "Determines which provider to use for caching. Options are github or buildjet, defaults to github."
required: false
default: "github"
lookup-only:
description: "Check if a cache entry exists without downloading the cache"
required: false
default: "false"
outputs:
cache-hit:
description: "A boolean value that indicates an exact match was found."
Expand Down
7 changes: 4 additions & 3 deletions dist/restore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77876,20 +77876,21 @@ async function run() {
if (cacheOnFailure !== "true") {
cacheOnFailure = "false";
}
var lookupOnly = lib_core.getInput("lookup-only").toLowerCase() === "true";
lib_core.exportVariable("CACHE_ON_FAILURE", cacheOnFailure);
lib_core.exportVariable("CARGO_INCREMENTAL", 0);
const config = await CacheConfig.new();
config.printInfo(cacheProvider);
lib_core.info("");
lib_core.info(`... Restoring cache ...`);
lib_core.info(`... ${lookupOnly ? "Checking" : "Restoring"} cache ...`);
const key = config.cacheKey;
// Pass a copy of cachePaths to avoid mutating the original array as reported by:
// https://github.com/actions/toolkit/pull/1378
// TODO: remove this once the underlying bug is fixed.
const restoreKey = await cacheProvider.cache.restoreCache(config.cachePaths.slice(), key, [config.restoreKey]);
const restoreKey = await cacheProvider.cache.restoreCache(config.cachePaths.slice(), key, [config.restoreKey], { lookupOnly });
if (restoreKey) {
const match = restoreKey === key;
lib_core.info(`Restored from cache key "${restoreKey}" full match: ${match}.`);
lib_core.info(`${lookupOnly ? "Found" : "Restored from"} cache key "${restoreKey}" full match: ${match}.`);
if (!match) {
// pre-clean the target directory on cache mismatch
for (const workspace of config.workspaces) {
Expand Down
17 changes: 14 additions & 3 deletions src/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,33 @@ async function run() {
if (cacheOnFailure !== "true") {
cacheOnFailure = "false";
}
var lookupOnly = core.getInput("lookup-only").toLowerCase() === "true";

core.exportVariable("CACHE_ON_FAILURE", cacheOnFailure);
core.exportVariable("CARGO_INCREMENTAL", 0);

const config = await CacheConfig.new();
config.printInfo(cacheProvider);
core.info("");

core.info(`... Restoring cache ...`);
core.info(`... ${lookupOnly ? "Checking" : "Restoring"} cache ...`);
const key = config.cacheKey;
// Pass a copy of cachePaths to avoid mutating the original array as reported by:
// https://github.com/actions/toolkit/pull/1378
// TODO: remove this once the underlying bug is fixed.
const restoreKey = await cacheProvider.cache.restoreCache(config.cachePaths.slice(), key, [config.restoreKey]);
const restoreKey = await cacheProvider.cache.restoreCache(
config.cachePaths.slice(),
key,
[config.restoreKey],
{ lookupOnly }
);
if (restoreKey) {
const match = restoreKey === key;
core.info(`Restored from cache key "${restoreKey}" full match: ${match}.`);
core.info(
`${
lookupOnly ? "Found" : "Restored from"
} cache key "${restoreKey}" full match: ${match}.`
);
if (!match) {
// pre-clean the target directory on cache mismatch
for (const workspace of config.workspaces) {
Expand Down

0 comments on commit c00f302

Please sign in to comment.