diff --git a/README.md b/README.md index 3adf639..789120b 100644 --- a/README.md +++ b/README.md @@ -94,8 +94,8 @@ The following optimizations and considerations are taken into account when cachi `src`, `.cache`, and any other unnecessary files. - Only the `/target/debug` profile is cached, as this profile is typically used for formatting, linting, and testing. -- The following sources are hashed for the generated cache key: `Cargo.lock`, Rust version, Rust - commit hash, and operating system. +- The following sources are hashed for the generated cache key: `$GITHUB_JOB`, `Cargo.lock`, Rust + version, Rust commit hash, and operating system. ## Compared to diff --git a/package.json b/package.json index f6bc22c..9d08faa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moonrepo/setup-rust", - "version": "0.3.0", + "version": "0.3.1", "description": "A GitHub action for setting up Rust and Cargo.", "main": "dist/index.js", "scripts": { @@ -20,6 +20,7 @@ "@actions/core": "^1.10.0", "@actions/exec": "^1.1.1", "@actions/glob": "^0.4.0", + "@actions/io": "^1.1.3", "@actions/tool-cache": "^2.0.1", "@ltd/j-toml": "^1.38.0" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6039583..e6af629 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,6 +13,9 @@ dependencies: '@actions/glob': specifier: ^0.4.0 version: 0.4.0 + '@actions/io': + specifier: ^1.1.3 + version: 1.1.3 '@actions/tool-cache': specifier: ^2.0.1 version: 2.0.1 diff --git a/src/cargo.ts b/src/cargo.ts index db480e5..3de6d80 100644 --- a/src/cargo.ts +++ b/src/cargo.ts @@ -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?