Adding support for package caching

This commit is contained in:
Nathan Ahn 2025-07-12 13:18:34 -04:00
parent 22457c87c1
commit 9b0a10bfca
6 changed files with 124 additions and 419 deletions

View file

@ -38,7 +38,10 @@ inputs:
required: false
type: boolean
default: false
description: Disable caching of bun executable.
description: Disable caching of bun executable and packages.
extra-key:
required: false
description: Extra key to use for caching.
outputs:
bun-version:

216
dist/cache-save/index.js generated vendored

File diff suppressed because one or more lines are too long

304
dist/setup/index.js generated vendored

File diff suppressed because one or more lines are too long

View file

@ -26,6 +26,7 @@ export type Input = {
profile?: boolean;
registries?: Registry[];
noCache?: boolean;
extraKey?: string;
};
export type Output = {
@ -41,6 +42,7 @@ export type CacheState = {
cacheHit: boolean;
bunPath: string;
url: string;
cacheKey: string;
};
export default async (options: Input): Promise<Output> => {
@ -51,6 +53,7 @@ export default async (options: Input): Promise<Output> => {
const cacheEnabled = isCacheEnabled(options);
const binPath = join(homedir(), ".bun", "bin");
const cachePath = join(homedir(), ".bun", "cache");
try {
mkdirSync(binPath, { recursive: true });
} catch (error) {
@ -73,10 +76,11 @@ export default async (options: Input): Promise<Output> => {
let revision: string | undefined;
let cacheHit = false;
if (cacheEnabled) {
const cacheKey = createHash("sha1").update(url).digest("base64");
const cacheRestored = await restoreCache([bunPath], cacheKey);
const cacheKeyBase = createHash("sha1").update(url).digest("base64")
const cacheKey = `${cacheKeyBase}-${options.extraKey ?? "default"}`
if (cacheEnabled) {
const cacheRestored = await restoreCache([bunPath], cacheKey, [cacheKeyBase]);
if (cacheRestored) {
revision = await getRevision(bunPath);
if (revision) {
@ -109,6 +113,7 @@ export default async (options: Input): Promise<Output> => {
cacheHit,
bunPath,
url,
cacheKey,
};
saveState("cache", JSON.stringify(cacheState));

View file

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

View file

@ -30,6 +30,7 @@ runAction({
customUrl: getInput("bun-download-url") || undefined,
registries: registries,
noCache: getBooleanInput("no-cache") || false,
extraKey: getInput("extra-key") || undefined,
})
.then(({ version, revision, bunPath, url, cacheHit }) => {
setOutput("bun-version", version);