mirror of
https://github.com/Swatinem/rust-cache
synced 2025-04-29 14:45:53 +00:00
merge all the caches and simplify
This commit is contained in:
parent
f77cb1be47
commit
e8e3c57b3b
8 changed files with 197 additions and 259 deletions
|
@ -6,6 +6,9 @@ import fs from "fs";
|
|||
import os from "os";
|
||||
import path from "path";
|
||||
|
||||
export const stateKey = "RUST_CACHE_KEY";
|
||||
const stateHash = "RUST_CACHE_HASH";
|
||||
|
||||
const home = os.homedir();
|
||||
export const paths = {
|
||||
index: path.join(home, ".cargo/registry/index"),
|
||||
|
@ -15,15 +18,9 @@ export const paths = {
|
|||
};
|
||||
|
||||
interface CacheConfig {
|
||||
name: string;
|
||||
paths: Array<string>;
|
||||
key: string;
|
||||
restoreKeys?: Array<string>;
|
||||
}
|
||||
|
||||
interface Caches {
|
||||
registry: CacheConfig;
|
||||
target: CacheConfig;
|
||||
restoreKeys: Array<string>;
|
||||
}
|
||||
|
||||
const RefKey = "GITHUB_REF";
|
||||
|
@ -32,41 +29,36 @@ export function isValidEvent(): boolean {
|
|||
return RefKey in process.env && Boolean(process.env[RefKey]);
|
||||
}
|
||||
|
||||
export async function getCaches(): Promise<Caches> {
|
||||
const rustKey = await getRustKey();
|
||||
let lockHash = core.getState("lockHash");
|
||||
export async function getCacheConfig(): Promise<CacheConfig> {
|
||||
let lockHash = core.getState(stateHash);
|
||||
if (!lockHash) {
|
||||
lockHash = await getLockfileHash();
|
||||
core.saveState("lockHash", lockHash);
|
||||
}
|
||||
let targetKey = core.getInput("key");
|
||||
if (targetKey) {
|
||||
targetKey = `${targetKey}-`;
|
||||
}
|
||||
const job = process.env.GITHUB_JOB;
|
||||
if (job) {
|
||||
targetKey = `${job}-${targetKey}`;
|
||||
core.saveState(stateHash, lockHash);
|
||||
}
|
||||
|
||||
const registry = `v0-registry`;
|
||||
const target = `v0-target-${targetKey}${rustKey}`;
|
||||
let key = `v0-rust-`;
|
||||
|
||||
let inputKey = core.getInput("key");
|
||||
if (inputKey) {
|
||||
key += `${inputKey}-`;
|
||||
}
|
||||
|
||||
const job = process.env.GITHUB_JOB;
|
||||
if (job) {
|
||||
key += `${job}-`;
|
||||
}
|
||||
|
||||
key += await getRustKey();
|
||||
|
||||
return {
|
||||
registry: {
|
||||
name: "Registry",
|
||||
paths: [
|
||||
paths.index,
|
||||
paths.cache,
|
||||
// TODO: paths.git,
|
||||
],
|
||||
key: `${registry}-${lockHash}`,
|
||||
restoreKeys: [registry],
|
||||
},
|
||||
target: {
|
||||
name: "Target",
|
||||
paths: [paths.target],
|
||||
key: `${target}-${lockHash}`,
|
||||
restoreKeys: [target],
|
||||
},
|
||||
paths: [
|
||||
paths.index,
|
||||
paths.cache,
|
||||
// TODO: paths.git,
|
||||
paths.target,
|
||||
],
|
||||
key: `${key}-${lockHash}`,
|
||||
restoreKeys: [key],
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -109,17 +101,6 @@ export async function getCmdOutput(
|
|||
return stdout;
|
||||
}
|
||||
|
||||
export async function getRegistryName(): Promise<string> {
|
||||
const globber = await glob.create(`${paths.index}/**/.last-updated`, { followSymbolicLinks: false });
|
||||
const files = await globber.glob();
|
||||
if (files.length > 1) {
|
||||
core.warning(`got multiple registries: "${files.join('", "')}"`);
|
||||
}
|
||||
|
||||
const first = files.shift()!;
|
||||
return path.basename(path.dirname(first));
|
||||
}
|
||||
|
||||
async function getLockfileHash(): Promise<string> {
|
||||
const globber = await glob.create("**/Cargo.toml\n**/Cargo.lock", { followSymbolicLinks: false });
|
||||
const files = await globber.glob();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import * as cache from "@actions/cache";
|
||||
import * as core from "@actions/core";
|
||||
import { getCaches, isValidEvent } from "./common";
|
||||
import { getCacheConfig, isValidEvent, stateKey } from "./common";
|
||||
|
||||
async function run() {
|
||||
if (!isValidEvent()) {
|
||||
|
@ -10,28 +10,26 @@ async function run() {
|
|||
try {
|
||||
core.exportVariable("CARGO_INCREMENTAL", 0);
|
||||
|
||||
const caches = await getCaches();
|
||||
for (const [type, { name, paths, key, restoreKeys }] of Object.entries(caches)) {
|
||||
const start = Date.now();
|
||||
core.startGroup(`Restoring ${name}…`);
|
||||
core.info(`Restoring paths:\n ${paths.join("\n ")}.`);
|
||||
core.info(`Using keys:\n ${[key, ...restoreKeys].join("\n ")}`);
|
||||
try {
|
||||
const restoreKey = await cache.restoreCache(paths, key, restoreKeys);
|
||||
if (restoreKey) {
|
||||
core.info(`Restored from cache key "${restoreKey}".`);
|
||||
core.saveState(`CACHEKEY-${type}`, restoreKey);
|
||||
} else {
|
||||
core.info("No cache found.");
|
||||
}
|
||||
} catch (e) {
|
||||
core.info(`[warning] ${e.message}`);
|
||||
const start = Date.now();
|
||||
const { paths, key, restoreKeys } = await getCacheConfig();
|
||||
|
||||
core.info(`Restoring paths:\n ${paths.join("\n ")}.`);
|
||||
core.info(`Using keys:\n ${[key, ...restoreKeys].join("\n ")}`);
|
||||
try {
|
||||
const restoreKey = await cache.restoreCache(paths, key, restoreKeys);
|
||||
if (restoreKey) {
|
||||
core.info(`Restored from cache key "${restoreKey}".`);
|
||||
core.saveState(stateKey, restoreKey);
|
||||
} else {
|
||||
core.info("No cache found.");
|
||||
}
|
||||
const duration = Math.round((Date.now() - start) / 1000);
|
||||
if (duration) {
|
||||
core.info(`Took ${duration}s.`);
|
||||
}
|
||||
core.endGroup();
|
||||
} catch (e) {
|
||||
core.info(`[warning] ${e.message}`);
|
||||
}
|
||||
|
||||
const duration = Math.round((Date.now() - start) / 1000);
|
||||
if (duration) {
|
||||
core.info(`Took ${duration}s.`);
|
||||
}
|
||||
} catch (e) {
|
||||
core.info(`[warning] ${e.message}`);
|
||||
|
|
82
src/save.ts
82
src/save.ts
|
@ -1,10 +1,11 @@
|
|||
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";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { getCaches, getCmdOutput, getRegistryName, isValidEvent, paths } from "./common";
|
||||
import { getCacheConfig, getCmdOutput, isValidEvent, paths, stateKey } from "./common";
|
||||
|
||||
async function run() {
|
||||
if (!isValidEvent()) {
|
||||
|
@ -12,49 +13,35 @@ async function run() {
|
|||
}
|
||||
|
||||
try {
|
||||
const caches = await getCaches();
|
||||
let upToDate = true;
|
||||
for (const [type, { key }] of Object.entries(caches)) {
|
||||
if (core.getState(`CACHEKEY-${type}`) !== key) {
|
||||
upToDate = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (upToDate) {
|
||||
core.info(`All caches up-to-date`);
|
||||
const start = Date.now();
|
||||
const { paths: savePaths, key } = await getCacheConfig();
|
||||
|
||||
if (core.getState(stateKey) === key) {
|
||||
core.info(`Cache up-to-date.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const registryName = await getRegistryName();
|
||||
const packages = await getPackages();
|
||||
|
||||
// TODO: remove this once https://github.com/actions/toolkit/pull/553 lands
|
||||
await macOsWorkaround();
|
||||
|
||||
await io.rmRF(path.join(paths.index, registryName, ".cache"));
|
||||
await pruneRegistryCache(registryName, packages);
|
||||
const registryName = await getRegistryName();
|
||||
const packages = await getPackages();
|
||||
|
||||
await pruneTarget(packages);
|
||||
await cleanRegistry(registryName, packages);
|
||||
|
||||
for (const [type, { name, path: paths, key }] of Object.entries(caches)) {
|
||||
if (core.getState(`CACHEKEY-${type}`) === key) {
|
||||
core.info(`${name} up-to-date.`);
|
||||
continue;
|
||||
}
|
||||
const start = Date.now();
|
||||
core.startGroup(`Saving ${name}…`);
|
||||
core.info(`Saving paths:\n ${paths.join("\n ")}.`);
|
||||
core.info(`Using key "${key}".`);
|
||||
try {
|
||||
await cache.saveCache(paths, key);
|
||||
} catch (e) {
|
||||
core.info(`[warning] ${e.message}`);
|
||||
}
|
||||
const duration = Math.round((Date.now() - start) / 1000);
|
||||
if (duration) {
|
||||
core.info(`Took ${duration}s.`);
|
||||
}
|
||||
core.endGroup();
|
||||
await cleanTarget(packages);
|
||||
|
||||
core.info(`Saving paths:\n ${savePaths.join("\n ")}.`);
|
||||
core.info(`Using key "${key}".`);
|
||||
try {
|
||||
await cache.saveCache(savePaths, key);
|
||||
} catch (e) {
|
||||
core.info(`[warning] ${e.message}`);
|
||||
}
|
||||
|
||||
const duration = Math.round((Date.now() - start) / 1000);
|
||||
if (duration) {
|
||||
core.info(`Took ${duration}s.`);
|
||||
}
|
||||
} catch (e) {
|
||||
core.info(`[warning] ${e.message}`);
|
||||
|
@ -80,6 +67,17 @@ interface Meta {
|
|||
}>;
|
||||
}
|
||||
|
||||
async function getRegistryName(): Promise<string> {
|
||||
const globber = await glob.create(`${paths.index}/**/.last-updated`, { followSymbolicLinks: false });
|
||||
const files = await globber.glob();
|
||||
if (files.length > 1) {
|
||||
core.warning(`got multiple registries: "${files.join('", "')}"`);
|
||||
}
|
||||
|
||||
const first = files.shift()!;
|
||||
return path.basename(path.dirname(first));
|
||||
}
|
||||
|
||||
async function getPackages(): Promise<Packages> {
|
||||
const cwd = process.cwd();
|
||||
const meta: Meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1"]));
|
||||
|
@ -92,7 +90,9 @@ async function getPackages(): Promise<Packages> {
|
|||
});
|
||||
}
|
||||
|
||||
async function pruneRegistryCache(registryName: string, packages: Packages) {
|
||||
async function cleanRegistry(registryName: string, packages: Packages) {
|
||||
await io.rmRF(path.join(paths.index, registryName, ".cache"));
|
||||
|
||||
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));
|
||||
|
||||
const dir = await fs.promises.opendir(path.join(paths.cache, registryName));
|
||||
|
@ -105,12 +105,12 @@ async function pruneRegistryCache(registryName: string, packages: Packages) {
|
|||
}
|
||||
}
|
||||
|
||||
async function pruneTarget(packages: Packages) {
|
||||
async function cleanTarget(packages: Packages) {
|
||||
await fs.promises.unlink("./target/.rustc_info.json");
|
||||
await io.rmRF("./target/debug/examples");
|
||||
await io.rmRF("./target/debug/incremental");
|
||||
let dir: fs.Dir;
|
||||
|
||||
let dir: fs.Dir;
|
||||
// remove all *files* from debug
|
||||
dir = await fs.promises.opendir("./target/debug");
|
||||
for await (const dirent of dir) {
|
||||
|
@ -137,7 +137,7 @@ async function pruneTarget(packages: Packages) {
|
|||
await rmExcept("./target/debug/deps", keepDeps);
|
||||
}
|
||||
|
||||
const twoWeeks = 14 * 24 * 3600 * 1000;
|
||||
const oneWeek = 7 * 24 * 3600 * 1000;
|
||||
|
||||
async function rmExcept(dirName: string, keepPrefix: Set<string>) {
|
||||
const dir = await fs.promises.opendir(dirName);
|
||||
|
@ -149,7 +149,7 @@ async function rmExcept(dirName: string, keepPrefix: Set<string>) {
|
|||
}
|
||||
const fileName = path.join(dir.path, dirent.name);
|
||||
const { mtime } = await fs.promises.stat(fileName);
|
||||
if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > twoWeeks) {
|
||||
if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > oneWeek) {
|
||||
core.debug(`deleting "${fileName}"`);
|
||||
if (dirent.isFile()) {
|
||||
await fs.promises.unlink(fileName);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue