Update api.

This commit is contained in:
Miles Johnson 2023-11-17 10:04:06 -08:00
parent d87f327046
commit d7d917b850
3 changed files with 32 additions and 20 deletions

View file

@ -10,7 +10,9 @@ inputs:
description: 'Toggle caching of ~/.cargo/registry and /target/<cache-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'

View file

@ -3,7 +3,14 @@ import { saveCache } from './src/cargo';
async function run() {
try {
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);
}

View file

@ -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,16 +42,22 @@ 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);
if (core.getInput('cache-base')) {
core.debug('Using warmup strategy, not hashing Cargo.lock, GITHUB_WORKFLOW, or GITHUB_JOB');
}
// When warming up, these add far too much granularity to the cache key
else {
const lockHash = await glob.hashFiles('Cargo.lock');
core.debug(`Hashing Cargo.lock = ${lockHash}`);
hasher.update(lockHash);
const workflow = process.env.GITHUB_WORKFLOW;
if (workflow) {
@ -69,6 +71,7 @@ export async function getPrimaryCacheKey() {
core.debug(`Hashing GITHUB_JOB = ${job}`);
hasher.update(job);
}
}
return `${getCachePrefixes()[0]}-${hasher.digest('hex')}`;
}