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

View file

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

View file

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