fix: Fix caching issues.

This commit is contained in:
Miles Johnson 2023-04-17 15:35:03 -07:00
parent b183549b9a
commit c49c6099c9
4 changed files with 22 additions and 14 deletions

View file

@ -1,16 +1,13 @@
/* eslint-disable node/no-unsupported-features/node-builtins */
import crypto from 'crypto';
import fs from 'fs';
import os from 'os';
import path from 'path';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as glob from '@actions/glob';
import * as io from '@actions/io';
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
export const CARGO_HOME = process.env.CARGO_HOME || path.join(os.homedir(), '.cargo');
export const CARGO_HOME = process.env.CARGO_HOME ?? path.join(os.homedir(), '.cargo');
export const CACHE_ENABLED = core.getBooleanInput('cache') || cache.isFeatureAvailable();
@ -21,25 +18,32 @@ export async function getPrimaryCacheKey() {
return CACHE_KEY;
}
const hasher = crypto.createHash('sha1');
core.info('Generating cache key');
const rustVersion = core.getState('rust-version');
core.debug(`Hashing Rust version = ${rustVersion}`);
hasher.update(rustVersion);
const rustHash = core.getState('rust-hash');
core.debug(`Hashing Rust commit hash = ${rustHash}`);
hasher.update(rustHash);
const lockHash = await glob.hashFiles('Cargo.lock');
core.debug(`Hashing Cargo.lock = ${lockHash}`);
const hasher = crypto.createHash('sha1');
hasher.update(rustVersion);
hasher.update(rustHash);
hasher.update(lockHash);
const job = process.env.GITHUB_JOB;
if (job) {
core.debug(`Hashing GITHUB_JOB = ${job}`);
hasher.update(job);
}
// eslint-disable-next-line require-atomic-updates
CACHE_KEY = `setup-rustcargo-${process.platform}-${hasher.digest('hex')}`;
@ -51,7 +55,7 @@ export function getPathsToCache(): string[] {
// ~/.cargo/registry
path.join(CARGO_HOME, 'registry'),
// /workspace/target/debug
path.join(process.cwd(), 'target/debug'),
path.join(process.env.GITHUB_WORKSPACE ?? process.cwd(), 'target/debug'),
];
}
@ -68,7 +72,7 @@ export async function cleanCargoRegistry() {
for await (const file of globber.globGenerator()) {
core.debug(`Deleting ${file}`);
await fs.promises.unlink(file);
await io.rmRF(file);
}
// .cargo/registry/cache - Do nothing?