mirror of
https://github.com/moonrepo/setup-rust.git
synced 2025-04-29 21:40:01 +00:00
new: Clean target directory before saving. (#3)
This commit is contained in:
parent
8e43d3720d
commit
c33bc5a232
4 changed files with 69 additions and 28 deletions
24
README.md
24
README.md
|
@ -86,16 +86,22 @@ CI times. To disable caching, set the `cache` input to `false`.
|
||||||
|
|
||||||
The following optimizations and considerations are taken into account when caching:
|
The following optimizations and considerations are taken into account when caching:
|
||||||
|
|
||||||
- The `~/.cargo/bin` directory is not cached as we manage binary installation in this action via the
|
- `~/.cargo`
|
||||||
|
- The `/bin` directory is not cached as we manage binary installation in this action via the
|
||||||
`bins` input.
|
`bins` input.
|
||||||
- The `~/.cargo/git` directory is not cached as it's not necessary for CI. When required by Cargo or
|
- The `/git` directory is not cached as it's not necessary for CI. When required by Cargo or a
|
||||||
a crate, a checkout will be performed on-demand.
|
crate, a checkout will be performed on-demand.
|
||||||
- The `~/.cargo/registry` directory is _cleaned_ before saving the cache. This includes removing
|
- The `/registry` directory is _cleaned_ before saving the cache. This includes removing `src`,
|
||||||
`src`, `.cache`, and any other unnecessary files.
|
`.cache`, and any other unnecessary files.
|
||||||
- Only the `/target/debug` profile is cached, as this profile is typically used for formatting,
|
- `/target/debug`
|
||||||
linting, and testing.
|
- Only the `debug` profile is cached, as this profile is typically used for formatting, linting,
|
||||||
- The following sources are hashed for the generated cache key: `$GITHUB_JOB`, `Cargo.lock`, Rust
|
and testing.
|
||||||
version, Rust commit hash, and operating system.
|
- The `/examples` and `/incremental` 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
|
||||||
|
re-executions.
|
||||||
|
|
||||||
|
The following sources are hashed for the generated cache key: `$GITHUB_JOB`, `Cargo.lock`, Rust
|
||||||
|
version, Rust commit hash, and OS.
|
||||||
|
|
||||||
## Compared to
|
## Compared to
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@moonrepo/setup-rust",
|
"name": "@moonrepo/setup-rust",
|
||||||
"version": "0.3.6",
|
"version": "0.4.0",
|
||||||
"description": "A GitHub action for setting up Rust and Cargo.",
|
"description": "A GitHub action for setting up Rust and Cargo.",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
55
src/cargo.ts
55
src/cargo.ts
|
@ -6,12 +6,14 @@ import * as cache from '@actions/cache';
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import * as exec from '@actions/exec';
|
import * as exec from '@actions/exec';
|
||||||
import * as glob from '@actions/glob';
|
import * as glob from '@actions/glob';
|
||||||
import * as io from '@actions/io';
|
|
||||||
import * as tc from '@actions/tool-cache';
|
import * as tc from '@actions/tool-cache';
|
||||||
|
import { rmrf } from './fs';
|
||||||
import { RUST_HASH, RUST_VERSION } from './rust';
|
import { RUST_HASH, RUST_VERSION } from './rust';
|
||||||
|
|
||||||
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 WORKSPACE_ROOT = process.env.GITHUB_WORKSPACE ?? process.cwd();
|
||||||
|
|
||||||
export const CACHE_ENABLED = core.getBooleanInput('cache') || cache.isFeatureAvailable();
|
export const CACHE_ENABLED = core.getBooleanInput('cache') || cache.isFeatureAvailable();
|
||||||
|
|
||||||
export async function downloadAndInstallBinstall(binDir: string) {
|
export async function downloadAndInstallBinstall(binDir: string) {
|
||||||
|
@ -87,12 +89,12 @@ export function getCachePaths(): string[] {
|
||||||
// ~/.cargo/registry
|
// ~/.cargo/registry
|
||||||
path.join(CARGO_HOME, 'registry'),
|
path.join(CARGO_HOME, 'registry'),
|
||||||
// /workspace/target/debug
|
// /workspace/target/debug
|
||||||
path.join(process.env.GITHUB_WORKSPACE ?? process.cwd(), 'target/debug'),
|
path.join(WORKSPACE_ROOT, 'target/debug'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCachePrefixes(): string[] {
|
export function getCachePrefixes(): string[] {
|
||||||
return [`setup-rustcargo-v0-${process.platform}`, 'setup-rustcargo-v0'];
|
return [`setup-rustcargo-v1-${process.platform}`, 'setup-rustcargo-v1'];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getPrimaryCacheKey() {
|
export async function getPrimaryCacheKey() {
|
||||||
|
@ -122,7 +124,7 @@ export async function getPrimaryCacheKey() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function cleanCargoRegistry() {
|
export async function cleanCargoRegistry() {
|
||||||
core.info('Cleaning cache before saving');
|
core.info('Cleaning ~/.cargo before saving');
|
||||||
|
|
||||||
const registryDir = path.join(CARGO_HOME, 'registry');
|
const registryDir = path.join(CARGO_HOME, 'registry');
|
||||||
|
|
||||||
|
@ -133,25 +135,46 @@ export async function cleanCargoRegistry() {
|
||||||
const indexDir = path.join(registryDir, 'index');
|
const indexDir = path.join(registryDir, 'index');
|
||||||
|
|
||||||
if (fs.existsSync(indexDir)) {
|
if (fs.existsSync(indexDir)) {
|
||||||
for (const index of fs.readdirSync(indexDir)) {
|
await Promise.all(
|
||||||
|
fs.readdirSync(indexDir).map(async (index) => {
|
||||||
if (fs.existsSync(path.join(indexDir, index, '.git'))) {
|
if (fs.existsSync(path.join(indexDir, index, '.git'))) {
|
||||||
const dir = path.join(indexDir, index, '.cache');
|
await rmrf(path.join(indexDir, index, '.cache'));
|
||||||
|
|
||||||
core.debug(`Deleting ${dir}`);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
await io.rmRF(dir);
|
|
||||||
} catch (error: unknown) {
|
|
||||||
core.warning(`Failed to delete ${dir}: ${error}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// .cargo/registry/cache - Do nothing?
|
// .cargo/registry/cache - Do nothing?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://doc.rust-lang.org/cargo/guide/build-cache.html
|
||||||
|
export async function cleanTargetProfile() {
|
||||||
|
core.info('Cleaning target/debug before saving');
|
||||||
|
|
||||||
|
const targetDir = path.join(WORKSPACE_ROOT, 'target/debug');
|
||||||
|
|
||||||
|
// target/debug/{examples,incremental} - Not required in CI
|
||||||
|
core.info('Removing examples and incremental directories');
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
['examples', 'incremental'].map(async (dirName) => {
|
||||||
|
const dir = path.join(targetDir, dirName);
|
||||||
|
|
||||||
|
if (fs.existsSync(dir)) {
|
||||||
|
await rmrf(dir);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
// target/debug/**/*.d - Not required in CI?
|
||||||
|
core.info('Removing dep-info files (*.d)');
|
||||||
|
|
||||||
|
const globber = await glob.create(path.join(targetDir, '**/*.d'));
|
||||||
|
const files = await globber.glob();
|
||||||
|
|
||||||
|
await Promise.all(files.map(rmrf));
|
||||||
|
}
|
||||||
|
|
||||||
export async function saveCache() {
|
export async function saveCache() {
|
||||||
if (!CACHE_ENABLED) {
|
if (!CACHE_ENABLED) {
|
||||||
return;
|
return;
|
||||||
|
|
12
src/fs.ts
Normal file
12
src/fs.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import * as core from '@actions/core';
|
||||||
|
import * as io from '@actions/io';
|
||||||
|
|
||||||
|
export async function rmrf(dir: string) {
|
||||||
|
core.debug(`Deleting ${dir}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await io.rmRF(dir);
|
||||||
|
} catch (error: unknown) {
|
||||||
|
core.warning(`Failed to delete ${dir}: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue