From 0716d005db592a44c4842ca6868e367af8c3ce62 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Tue, 18 Apr 2023 10:52:30 -0700 Subject: [PATCH] new: Delete cache older than 14 days. --- README.md | 1 + package.json | 4 ++-- src/cargo.ts | 16 ++++++++++++++-- src/{fs.ts => helpers.ts} | 4 ++++ 4 files changed, 21 insertions(+), 4 deletions(-) rename src/{fs.ts => helpers.ts} (75%) diff --git a/README.md b/README.md index 86af5ee..3025b9c 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ The following optimizations and considerations are taken into account when cachi crate, a checkout will be performed on-demand. - The `/registry` directory is _cleaned_ before saving the cache. This includes removing `src`, `.cache`, and any other unnecessary files. + - Registry artifacts older than 14 days will be removed. - `/target/debug` - Only the `debug` profile is cached, as this profile is typically used for formatting, linting, and testing. diff --git a/package.json b/package.json index b8df3da..47fb993 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "@moonrepo/setup-rust", - "version": "0.4.1", + "version": "0.4.2", "description": "A GitHub action for setting up Rust and Cargo.", "main": "dist/index.js", "scripts": { "build": "ncc build ./index.ts && ncc build ./post.ts --out ./dist/post", - "check": "npm run lint && npm run typecheck", + "check": "pnpm run lint && pnpm run typecheck", "lint": "eslint --ext .ts,.js --fix .", "typecheck": "tsc --noEmit" }, diff --git a/src/cargo.ts b/src/cargo.ts index 89543eb..6e4c9aa 100644 --- a/src/cargo.ts +++ b/src/cargo.ts @@ -7,7 +7,7 @@ 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 { rmrf } from './fs'; +import { padDate, rmrf } from './helpers'; import { RUST_HASH, RUST_VERSION } from './rust'; export const CARGO_HOME = process.env.CARGO_HOME ?? path.join(os.homedir(), '.cargo'); @@ -129,7 +129,19 @@ export async function cleanCargoRegistry() { const registryDir = path.join(CARGO_HOME, 'registry'); // .cargo/registry/src - Delete entirely - await exec.exec('cargo', ['cache', '--autoclean']); + const staleDate = new Date(); + + // eslint-disable-next-line no-magic-numbers + staleDate.setDate(staleDate.getDate() - 14); + + await exec.exec('cargo', [ + 'cache', + '--autoclean', + '--remove-if-older-than', + `${staleDate.getFullYear()}.${padDate(staleDate.getMonth() + 1)}.${padDate( + staleDate.getDate(), + )}`, + ]); // .cargo/registry/index - Delete .cache directories const indexDir = path.join(registryDir, 'index'); diff --git a/src/fs.ts b/src/helpers.ts similarity index 75% rename from src/fs.ts rename to src/helpers.ts index 31454d2..0d5b27d 100644 --- a/src/fs.ts +++ b/src/helpers.ts @@ -10,3 +10,7 @@ export async function rmrf(dir: string) { core.warning(`Failed to delete ${dir}: ${error}`); } } + +export function padDate(value: number) { + return String(value).padStart(2, '0'); +}