Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move cache save to runs.post and exit early #60

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ outputs:
cache-hit:
description: If the version of Bun was cached.
runs:
using: node20
main: dist/index.js
using: "node20"
main: "dist/setup/index.js"
post: "dist/cache-save/index.js"
post-if: success()
68 changes: 68 additions & 0 deletions dist/cache-save/index.js

Large diffs are not rendered by default.

62 changes: 31 additions & 31 deletions dist/index.js → dist/setup/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"author": "xHyroM",
"scripts": {
"format": "prettier --write src *.yml *.json *.md",
"build": "esbuild --target=node20 --outdir=dist --bundle --minify --platform=node --format=cjs src/index.ts",
"start": "npm run build && node dist/index.js"
"build": "esbuild --target=node20 --outfile=dist/setup/index.js --bundle --minify --platform=node --format=cjs src/index.ts && esbuild --target=node20 --outfile=dist/cache-save/index.js --bundle --minify --platform=node --format=cjs src/cache-save.ts",
"start": "npm run build && node dist/setup/index.js"
},
"dependencies": {
"@actions/cache": "^3.1.4",
Expand Down
28 changes: 19 additions & 9 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions src/cache-save.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
}
})();
Loading