3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2025-06-08 17:43:25 +00:00

restore from secondary branch

This commit is contained in:
Jonathan Kelley 2025-01-28 20:59:32 -08:00
parent 8c044fe7cc
commit af6c167d5d
No known key found for this signature in database
GPG key ID: 1FBB50F7EB0A08BE
3 changed files with 30 additions and 13 deletions

View file

@ -52,6 +52,10 @@ inputs:
description: "Determines whether to cache incremental builds - speeding up builds for more disk usage. Defaults to false." description: "Determines whether to cache incremental builds - speeding up builds for more disk usage. Defaults to false."
required: false required: false
default: "false" default: "false"
incremental-key:
description: "The key to use for incremental builds. Used on a per-branch basis"
required: false
default: ${{ github.ref }}
outputs: outputs:
cache-hit: cache-hit:
description: "A boolean value that indicates an exact match was found." description: "A boolean value that indicates an exact match was found."

View file

@ -21,8 +21,16 @@ const HASH_LENGTH = 8;
export class CacheConfig { export class CacheConfig {
/** All the paths we want to cache */ /** All the paths we want to cache */
public cachePaths: Array<string> = []; public cachePaths: Array<string> = [];
/** All the paths we want to cache for incremental builds */
public incrementalPaths: Array<string> = [];
/** The primary cache key */ /** The primary cache key */
public cacheKey = ""; public cacheKey = "";
/** The primary cache key for incremental builds */
public incrementalKey = "";
/** /**
* The secondary (restore) key that only contains the prefix and environment * The secondary (restore) key that only contains the prefix and environment
* This should be used if the primary cacheKey is not available - IE pulling from main on a branch * This should be used if the primary cacheKey is not available - IE pulling from main on a branch
@ -255,15 +263,16 @@ export class CacheConfig {
keyFiles.push(...parsedKeyFiles); keyFiles.push(...parsedKeyFiles);
self.keyFiles = sort_and_uniq(keyFiles); self.keyFiles = sort_and_uniq(keyFiles);
// todo(jon): make sure we differentiate incrementals on different branches
// we can use just a single cache per incremental branch
if (self.incremental) {
key += `-incremental`;
}
key += `-${lockHash}`; key += `-${lockHash}`;
self.cacheKey = key; self.cacheKey = key;
if (self.incremental) {
// wire the incremental key to be just for this branch
const branchName = core.getInput("incremental-key") || "-shared";
const incrementalKey = key + `-incremental` + branchName;
self.incrementalKey = incrementalKey;
}
self.cachePaths = [path.join(CARGO_HOME, "registry"), path.join(CARGO_HOME, "git")]; self.cachePaths = [path.join(CARGO_HOME, "registry"), path.join(CARGO_HOME, "git")];
if (self.cacheBin) { if (self.cacheBin) {
self.cachePaths = [ self.cachePaths = [
@ -286,7 +295,7 @@ export class CacheConfig {
if (self.incremental) { if (self.incremental) {
if (cacheTargets === "true") { if (cacheTargets === "true") {
for (const target of self.workspaces.map((ws) => ws.target)) { for (const target of self.workspaces.map((ws) => ws.target)) {
self.cachePaths.push(path.join(target, "incremental")); self.incrementalPaths.push(path.join(target, "incremental"));
} }
} }
} }

View file

@ -39,20 +39,24 @@ async function run() {
core.info(`... ${lookupOnly ? "Checking" : "Restoring"} cache ...`); core.info(`... ${lookupOnly ? "Checking" : "Restoring"} cache ...`);
const key = config.cacheKey; const key = config.cacheKey;
// Pass a copy of cachePaths to avoid mutating the original array as reported by: // Pass a copy of cachePaths to avoid mutating the original array as reported by:
// https://github.com/actions/toolkit/pull/1378 // https://github.com/actions/toolkit/pull/1378
// TODO: remove this once the underlying bug is fixed. // 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 });
lookupOnly,
});
if (restoreKey) { if (restoreKey) {
const match = restoreKey === key; const match = restoreKey === key;
core.info(`${lookupOnly ? "Found" : "Restored from"} cache key "${restoreKey}" full match: ${match}.`); core.info(`${lookupOnly ? "Found" : "Restored from"} cache key "${restoreKey}" full match: ${match}.`);
if (config.incremental) { if (config.incremental) {
core.debug("restoring incremental builds"); const incrementalKey = await cacheProvider.cache.restoreCache(config.incrementalPaths.slice(), config.incrementalKey, [config.restoreKey], { lookupOnly });
for (const workspace of config.workspaces) { core.debug(`restoring incremental builds from ${incrementalKey}`);
await restoreIncremental(workspace.target);
if (incrementalKey) {
for (const workspace of config.workspaces) {
await restoreIncremental(workspace.target);
}
} }
} }