Skip to content

Commit

Permalink
Update api.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 17, 2023
1 parent d87f327 commit d7d917b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
4 changes: 3 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 8 additions & 1 deletion post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
39 changes: 21 additions & 18 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down Expand Up @@ -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');

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);
if (job) {
core.debug(`Hashing GITHUB_JOB = ${job}`);
hasher.update(job);
}
}

return `${getCachePrefixes()[0]}-${hasher.digest('hex')}`;
Expand Down

0 comments on commit d7d917b

Please sign in to comment.