mirror of
https://github.com/Swatinem/rust-cache
synced 2025-04-29 14:45:53 +00:00
support working-directory input, cleanup
This commit is contained in:
parent
fb2efae33d
commit
9e10a44ea3
6 changed files with 53 additions and 49 deletions
|
@ -11,6 +11,11 @@ process.on("uncaughtException", (e) => {
|
|||
core.info(`[warning] ${e.message}`);
|
||||
});
|
||||
|
||||
const cwd = core.getInput("working-directory");
|
||||
if (cwd) {
|
||||
process.chdir(cwd);
|
||||
}
|
||||
|
||||
export const stateKey = "RUST_CACHE_KEY";
|
||||
const stateHash = "RUST_CACHE_HASH";
|
||||
|
||||
|
@ -196,11 +201,13 @@ export async function rmExcept(dirName: string, keepPrefix: Set<string>) {
|
|||
}
|
||||
|
||||
export async function rm(parent: string, dirent: fs.Dirent) {
|
||||
const fileName = path.join(parent, dirent.name);
|
||||
core.debug(`deleting "${fileName}"`);
|
||||
if (dirent.isFile()) {
|
||||
await fs.promises.unlink(fileName);
|
||||
} else if (dirent.isDirectory()) {
|
||||
await io.rmRF(fileName);
|
||||
}
|
||||
try {
|
||||
const fileName = path.join(parent, dirent.name);
|
||||
core.debug(`deleting "${fileName}"`);
|
||||
if (dirent.isFile()) {
|
||||
await fs.promises.unlink(fileName);
|
||||
} else if (dirent.isDirectory()) {
|
||||
await io.rmRF(fileName);
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
|
|
@ -1,42 +1,28 @@
|
|||
import * as cache from "@actions/cache";
|
||||
import * as core from "@actions/core";
|
||||
import { cleanTarget, getCacheConfig, getPackages, isValidEvent, stateKey } from "./common";
|
||||
import { cleanTarget, getCacheConfig, getPackages, stateKey } from "./common";
|
||||
|
||||
async function run() {
|
||||
if (!isValidEvent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
core.exportVariable("CARGO_INCREMENTAL", 0);
|
||||
|
||||
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);
|
||||
const restoreKey = await cache.restoreCache(paths, key, restoreKeys);
|
||||
if (restoreKey) {
|
||||
core.info(`Restored from cache key "${restoreKey}".`);
|
||||
core.saveState(stateKey, restoreKey);
|
||||
|
||||
if (restoreKey !== key) {
|
||||
// pre-clean the target directory on cache mismatch
|
||||
const packages = await getPackages();
|
||||
if (restoreKey !== key) {
|
||||
// pre-clean the target directory on cache mismatch
|
||||
const packages = await getPackages();
|
||||
|
||||
await cleanTarget(packages);
|
||||
}
|
||||
} else {
|
||||
core.info("No cache found.");
|
||||
await cleanTarget(packages);
|
||||
}
|
||||
} catch (e) {
|
||||
core.info(`[warning] ${e.message}`);
|
||||
}
|
||||
|
||||
const duration = Math.round((Date.now() - start) / 1000);
|
||||
if (duration) {
|
||||
core.info(`Took ${duration}s.`);
|
||||
} else {
|
||||
core.info("No cache found.");
|
||||
}
|
||||
} catch (e) {
|
||||
core.info(`[warning] ${e.message}`);
|
||||
|
|
18
src/save.ts
18
src/save.ts
|
@ -5,15 +5,10 @@ import * as glob from "@actions/glob";
|
|||
import * as io from "@actions/io";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { cleanTarget, getCacheConfig, getPackages, isValidEvent, Packages, paths, rm, stateKey } from "./common";
|
||||
import { cleanTarget, getCacheConfig, getPackages, Packages, paths, rm, stateKey } from "./common";
|
||||
|
||||
async function run() {
|
||||
if (!isValidEvent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const start = Date.now();
|
||||
const { paths: savePaths, key } = await getCacheConfig();
|
||||
|
||||
if (core.getState(stateKey) === key) {
|
||||
|
@ -41,16 +36,7 @@ async function run() {
|
|||
|
||||
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.`);
|
||||
}
|
||||
await cache.saveCache(savePaths, key);
|
||||
} catch (e) {
|
||||
core.info(`[warning] ${e.message}`);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue