From d87f3270460f848c537bef559303f7270b94e712 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Fri, 17 Nov 2023 09:51:54 -0800 Subject: [PATCH] Create cache file. --- CHANGELOG.md | 4 +++ action.yml | 2 ++ index.ts | 3 +- src/cache.ts | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ src/cargo.ts | 79 ++++++++-------------------------------------------- 5 files changed, 94 insertions(+), 68 deletions(-) create mode 100644 src/cache.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index ff22f18..cfec579 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.1.0 + +- Updated dependencies. + # 1.0.3 - Include `GITHUB_WORKFLOW` in cache key. diff --git a/action.yml b/action.yml index ec0afb7..b0c249f 100644 --- a/action.yml +++ b/action.yml @@ -9,6 +9,8 @@ inputs: cache: 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.' cache-target: description: 'Name of the target profile to cache.' default: 'debug' diff --git a/index.ts b/index.ts index 61f52ce..ff8f768 100644 --- a/index.ts +++ b/index.ts @@ -5,7 +5,8 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; import * as io from '@actions/io'; import * as tc from '@actions/tool-cache'; -import { CARGO_HOME, installBins, restoreCache } from './src/cargo'; +import { CARGO_HOME } from './src/cache'; +import { installBins, restoreCache } from './src/cargo'; import { installToolchain } from './src/rust'; export async function installRustup() { diff --git a/src/cache.ts b/src/cache.ts new file mode 100644 index 0000000..8df0543 --- /dev/null +++ b/src/cache.ts @@ -0,0 +1,74 @@ +import crypto from 'node:crypto'; +import os from 'node:os'; +import path from 'node:path'; +import * as cache from '@actions/cache'; +import * as core from '@actions/core'; +import * as glob from '@actions/glob'; +import { RUST_HASH, RUST_VERSION } from './rust'; + +export const CARGO_HOME = process.env.CARGO_HOME ?? path.join(os.homedir(), '.cargo'); + +export const WORKSPACE_ROOT = process.env.GITHUB_WORKSPACE ?? process.cwd(); + +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'; +} + +export function getCachePaths(): string[] { + return [ + // ~/.cargo/registry + path.join(CARGO_HOME, 'registry'), + // /workspace/target/debug + path.join(WORKSPACE_ROOT, 'target', getCacheTarget()), + ]; +} + +export function getCachePrefixes(): string[] { + return [`setup-rustcargo-v1-${process.platform}`, 'setup-rustcargo-v1']; +} + +export async function getPrimaryCacheKey() { + const hasher = crypto.createHash('sha1'); + + core.info('Generating cache key'); + + core.debug(`Hashing Rust version = ${RUST_VERSION}`); + hasher.update(RUST_VERSION); + + 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); + } + + const job = process.env.GITHUB_JOB; + + if (job) { + core.debug(`Hashing GITHUB_JOB = ${job}`); + hasher.update(job); + } + + return `${getCachePrefixes()[0]}-${hasher.digest('hex')}`; +} diff --git a/src/cargo.ts b/src/cargo.ts index 172ab0c..c053438 100644 --- a/src/cargo.ts +++ b/src/cargo.ts @@ -1,6 +1,4 @@ -import crypto from 'node:crypto'; import fs from 'node:fs'; -import os from 'node:os'; import path from 'node:path'; import { family } from 'detect-libc'; import * as cache from '@actions/cache'; @@ -8,14 +6,16 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; import * as glob from '@actions/glob'; import * as tc from '@actions/tool-cache'; +import { + CARGO_HOME, + getCachePaths, + getCachePrefixes, + getCacheTarget, + getPrimaryCacheKey, + isCacheEnabled, + WORKSPACE_ROOT, +} from './cache'; import { rmrf } from './fs'; -import { RUST_HASH, RUST_VERSION } from './rust'; - -export const CARGO_HOME = process.env.CARGO_HOME ?? path.join(os.homedir(), '.cargo'); - -export const WORKSPACE_ROOT = process.env.GITHUB_WORKSPACE ?? process.cwd(); - -export const CACHE_ENABLED = core.getBooleanInput('cache') && cache.isFeatureAvailable(); export async function downloadAndInstallBinstall(binDir: string) { core.info('cargo-binstall does not exist, attempting to install'); @@ -79,7 +79,7 @@ export async function installBins() { .map((bin) => bin.trim()) .filter(Boolean); - if (CACHE_ENABLED) { + if (isCacheEnabled()) { bins.push('cargo-cache'); } @@ -98,61 +98,6 @@ export async function installBins() { await exec.exec('cargo', ['binstall', '--no-confirm', '--log-level', 'info', ...bins]); } -export function getCacheTarget(): string { - return core.getInput('cache-target') || 'debug'; -} - -export function getCachePaths(): string[] { - return [ - // ~/.cargo/registry - path.join(CARGO_HOME, 'registry'), - // /workspace/target/debug - path.join(WORKSPACE_ROOT, 'target', getCacheTarget()), - ]; -} - -export function getCachePrefixes(): string[] { - return [`setup-rustcargo-v1-${process.platform}`, 'setup-rustcargo-v1']; -} - -export async function getPrimaryCacheKey() { - const hasher = crypto.createHash('sha1'); - - core.info('Generating cache key'); - - core.debug(`Hashing Rust version = ${RUST_VERSION}`); - hasher.update(RUST_VERSION); - - 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); - } - - const job = process.env.GITHUB_JOB; - - if (job) { - core.debug(`Hashing GITHUB_JOB = ${job}`); - hasher.update(job); - } - - return `${getCachePrefixes()[0]}-${hasher.digest('hex')}`; -} - export async function cleanCargoRegistry() { core.info('Cleaning ~/.cargo before saving'); @@ -208,7 +153,7 @@ export async function cleanTargetProfile() { } export async function saveCache() { - if (!CACHE_ENABLED) { + if (!isCacheEnabled()) { return; } @@ -229,7 +174,7 @@ export async function saveCache() { } export async function restoreCache() { - if (!CACHE_ENABLED) { + if (!isCacheEnabled()) { return; }