diff --git a/CHANGELOG.md b/CHANGELOG.md index 371436c..ddd6030 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Breaking: Cargo `bins` must provide the `cargo-` crate prefix manually. This change allows non-crate globals to be installed. +- Added a `cache-target` input, to customize which target profile is cached. Defaults to `debug`. # 0.5.0 diff --git a/README.md b/README.md index e5fa85a..5f27074 100644 --- a/README.md +++ b/README.md @@ -79,12 +79,14 @@ names. ## Caching in CI By default this action will cache the `~/.cargo/registry` and `/target/debug` directories to improve -CI times. To disable caching, set the `cache` input to `false`. +CI times. To disable caching, set the `cache` input to `false`. Furthermore, the target profile can +be changed with the `cache-target` input, which defaults to `debug`. ```yaml - uses: moonrepo/setup-rust@v0 with: cache: false + cache-target: release ``` The following optimizations and considerations are taken into account when caching: @@ -98,7 +100,7 @@ The following optimizations and considerations are taken into account when cachi `.cache`, and any other unnecessary files. - `/target` - Only the `/debug` profile is cached, as this profile is typically used for formatting, linting, - and testing. + and testing. This can be changed with the `cache-target` input. - The `/examples` and `/incremental` sub-directories are not cached as they are not necessary for CI. - All dep-info (`*.d`) files are removed, as they're meant for build systems and signaling diff --git a/action.yml b/action.yml index 9efcb4a..7f9d2ee 100644 --- a/action.yml +++ b/action.yml @@ -7,8 +7,11 @@ inputs: bins: description: 'Comma-separated list of global binaries to install into Cargo.' cache: - description: 'Toggle caching of ~/.cargo/registry and /target/debug directories.' + description: 'Toggle caching of ~/.cargo/registry and /target/ directories.' default: true + cache-target: + description: 'Name of the target profile to cache.' + default: 'debug' channel: description: 'Toolchain specification/channel to install.' components: diff --git a/package.json b/package.json index dc4e6eb..c1c803d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moonrepo/setup-rust", - "version": "0.5.0", + "version": "0.6.0", "description": "A GitHub action for setting up Rust and Cargo.", "main": "dist/index.js", "scripts": { diff --git a/src/cargo.ts b/src/cargo.ts index f425fa1..55bf0c8 100644 --- a/src/cargo.ts +++ b/src/cargo.ts @@ -83,12 +83,16 @@ 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/debug'), + path.join(WORKSPACE_ROOT, 'target', getCacheTarget()), ]; } @@ -112,6 +116,11 @@ export async function getPrimaryCacheKey() { core.debug(`Hashing Cargo.lock = ${lockHash}`); hasher.update(lockHash); + const cacheTarget = getCacheTarget(); + + core.debug(`Hashing target profile = ${cacheTarget}`); + hasher.update(cacheTarget); + const job = process.env.GITHUB_JOB; if (job) { @@ -148,11 +157,13 @@ export async function cleanCargoRegistry() { // https://doc.rust-lang.org/cargo/guide/build-cache.html export async function cleanTargetProfile() { - core.info('Cleaning target/debug before saving'); + const targetProfile = getCacheTarget(); - const targetDir = path.join(WORKSPACE_ROOT, 'target/debug'); + core.info(`Cleaning target/${targetProfile} before saving`); - // target/debug/{examples,incremental} - Not required in CI + const targetDir = path.join(WORKSPACE_ROOT, 'target', targetProfile); + + // target/*/{examples,incremental} - Not required in CI core.info('Removing examples and incremental directories'); await Promise.all( @@ -165,7 +176,7 @@ export async function cleanTargetProfile() { }), ); - // target/debug/**/*.d - Not required in CI? + // target/**/*.d - Not required in CI? core.info('Removing dep-info files (*.d)'); const globber = await glob.create(path.join(targetDir, '**/*.d'));