From d7d917b850766d73bff88315fe07923045733902 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Fri, 17 Nov 2023 10:04:06 -0800 Subject: [PATCH] Update api. --- action.yml | 4 +++- post.ts | 9 ++++++++- src/cache.ts | 39 +++++++++++++++++++++------------------ 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/action.yml b/action.yml index b0c249f..2c0ba87 100644 --- a/action.yml +++ b/action.yml @@ -10,7 +10,9 @@ inputs: description: 'Toggle caching of ~/.cargo/registry and /target/ directories.' default: true cache-base: - description: 'Base branch or ref to save cache. Other branches and refs will restore from this base.' + description: + 'Base branch or ref to save a warmup cache. Other branches and refs will restore from this + base.' cache-target: description: 'Name of the target profile to cache.' default: 'debug' diff --git a/post.ts b/post.ts index 077c816..0ff73c1 100644 --- a/post.ts +++ b/post.ts @@ -3,7 +3,14 @@ import { saveCache } from './src/cargo'; async function run() { try { - await saveCache(); + const base = core.getInput('cache-base'); + + // Only save the cache for the following 2 scenarios: + // - If not using the base warmup strategy. + // - If using the base warmup strategy, and the current ref matches. + if (!base || (base && !!(process.env.GITHUB_REF_NAME ?? '').match(base))) { + await saveCache(); + } } catch (error: unknown) { core.setFailed((error as Error).message); } diff --git a/src/cache.ts b/src/cache.ts index 8df0543..4856440 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -14,10 +14,6 @@ export function isCacheEnabled(): boolean { return core.getBooleanInput('cache') && cache.isFeatureAvailable(); } -export function isUsingBaseWarmupStrategy(): boolean { - return false; -} - export function getCacheTarget(): string { return core.getInput('cache-target') || 'debug'; } @@ -46,28 +42,35 @@ export async function getPrimaryCacheKey() { core.debug(`Hashing Rust commit hash = ${RUST_HASH}`); hasher.update(RUST_HASH); - const lockHash = await glob.hashFiles('Cargo.lock'); - - core.debug(`Hashing Cargo.lock = ${lockHash}`); - hasher.update(lockHash); - const cacheTarget = getCacheTarget(); core.debug(`Hashing target profile = ${cacheTarget}`); hasher.update(cacheTarget); - const workflow = process.env.GITHUB_WORKFLOW; - - if (workflow) { - core.debug(`Hashing GITHUB_WORKFLOW = ${workflow}`); - hasher.update(workflow); + if (core.getInput('cache-base')) { + core.debug('Using warmup strategy, not hashing Cargo.lock, GITHUB_WORKFLOW, or GITHUB_JOB'); } - const job = process.env.GITHUB_JOB; + // When warming up, these add far too much granularity to the cache key + else { + const lockHash = await glob.hashFiles('Cargo.lock'); - if (job) { - core.debug(`Hashing GITHUB_JOB = ${job}`); - hasher.update(job); + core.debug(`Hashing Cargo.lock = ${lockHash}`); + hasher.update(lockHash); + + const workflow = process.env.GITHUB_WORKFLOW; + + if (workflow) { + core.debug(`Hashing GITHUB_WORKFLOW = ${workflow}`); + hasher.update(workflow); + } + + const job = process.env.GITHUB_JOB; + + if (job) { + core.debug(`Hashing GITHUB_JOB = ${job}`); + hasher.update(job); + } } return `${getCachePrefixes()[0]}-${hasher.digest('hex')}`;