From af6c167d5db0f8285b4f464372fdfb5b0dc2537a Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Tue, 28 Jan 2025 20:59:32 -0800 Subject: [PATCH] restore from secondary branch --- action.yml | 4 ++++ src/config.ts | 23 ++++++++++++++++------- src/restore.ts | 16 ++++++++++------ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/action.yml b/action.yml index 0f1b024..36b03c5 100644 --- a/action.yml +++ b/action.yml @@ -52,6 +52,10 @@ inputs: description: "Determines whether to cache incremental builds - speeding up builds for more disk usage. Defaults to false." required: 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: cache-hit: description: "A boolean value that indicates an exact match was found." diff --git a/src/config.ts b/src/config.ts index ecae9fb..062e95e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -21,8 +21,16 @@ const HASH_LENGTH = 8; export class CacheConfig { /** All the paths we want to cache */ public cachePaths: Array = []; + + /** All the paths we want to cache for incremental builds */ + public incrementalPaths: Array = []; + /** The primary cache key */ public cacheKey = ""; + + /** The primary cache key for incremental builds */ + public incrementalKey = ""; + /** * 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 @@ -255,15 +263,16 @@ export class CacheConfig { keyFiles.push(...parsedKeyFiles); 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}`; 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")]; if (self.cacheBin) { self.cachePaths = [ @@ -286,7 +295,7 @@ export class CacheConfig { if (self.incremental) { if (cacheTargets === "true") { for (const target of self.workspaces.map((ws) => ws.target)) { - self.cachePaths.push(path.join(target, "incremental")); + self.incrementalPaths.push(path.join(target, "incremental")); } } } diff --git a/src/restore.ts b/src/restore.ts index f592d26..c459ca5 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -39,20 +39,24 @@ async function run() { 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], { - lookupOnly, - }); + const restoreKey = await cacheProvider.cache.restoreCache(config.cachePaths.slice(), key, [config.restoreKey], { lookupOnly }); + if (restoreKey) { const match = restoreKey === key; core.info(`${lookupOnly ? "Found" : "Restored from"} cache key "${restoreKey}" full match: ${match}.`); if (config.incremental) { - core.debug("restoring incremental builds"); - for (const workspace of config.workspaces) { - await restoreIncremental(workspace.target); + const incrementalKey = await cacheProvider.cache.restoreCache(config.incrementalPaths.slice(), config.incrementalKey, [config.restoreKey], { lookupOnly }); + core.debug(`restoring incremental builds from ${incrementalKey}`); + + if (incrementalKey) { + for (const workspace of config.workspaces) { + await restoreIncremental(workspace.target); + } } }