Move cache save to runs.post

This commit is contained in:
andyexeter 2024-02-15 15:56:48 +00:00
parent 12944059f7
commit f32d88462b
7 changed files with 289 additions and 82 deletions

View file

@ -8,10 +8,11 @@ import {
copyFileSync,
} from "node:fs";
import { addPath, info, warning } from "@actions/core";
import { isFeatureAvailable, restoreCache, saveCache } from "@actions/cache";
import { isFeatureAvailable, restoreCache } from "@actions/cache";
import { downloadTool, extractZip } from "@actions/tool-cache";
import { getExecOutput } from "@actions/exec";
import { writeBunfig } from "./bunfig";
import { saveState } from "@actions/core";
export type Input = {
customUrl?: string;
@ -30,6 +31,13 @@ export type Output = {
cacheHit: boolean;
};
export type CacheState = {
cacheEnabled: boolean;
cacheHit: boolean;
bunPath: string;
url: string;
};
export default async (options: Input): Promise<Output> => {
const bunfigPath = join(process.cwd(), "bunfig.toml");
writeBunfig(bunfigPath, options);
@ -96,15 +104,17 @@ export default async (options: Input): Promise<Output> => {
);
}
if (cacheEnabled && !cacheHit) {
try {
await saveCache([bunPath], url);
} catch (error) {
warning("Failed to save Bun to cache.");
}
}
const [version] = revision.split("+");
const cacheState: CacheState = {
cacheEnabled,
cacheHit,
bunPath,
url,
};
saveState("cache", JSON.stringify(cacheState));
return {
version,
revision,

15
src/cache-save.ts Normal file
View file

@ -0,0 +1,15 @@
import { saveCache } from "@actions/cache";
import { getState, warning } from "@actions/core";
import { CacheState } from "./action";
(async () => {
const state: CacheState = JSON.parse(getState("cache"));
if (state.cacheEnabled && !state.cacheHit) {
try {
await saveCache([state.bunPath], state.url);
process.exit(0);
} catch (error) {
warning("Failed to save Bun to cache.");
}
}
})();