feat: Check for existing bun before downloading (#138)

* feat: Check for existing bun before downloading

Check for existing bun with same version number, if it exists we skip any futher actions. This avoids downloading cache or bun over the network.

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Erik Axel Nielsen
2025-12-23 19:56:47 +01:00
committed by GitHub
parent 635640504f
commit 1dbab0699e
2 changed files with 151 additions and 114 deletions

194
dist/setup/index.js generated vendored

File diff suppressed because one or more lines are too long

View File

@@ -7,6 +7,7 @@ import {
symlinkSync, symlinkSync,
renameSync, renameSync,
copyFileSync, copyFileSync,
existsSync,
} from "node:fs"; } from "node:fs";
import { addPath, info, warning } from "@actions/core"; import { addPath, info, warning } from "@actions/core";
import { isFeatureAvailable, restoreCache } from "@actions/cache"; import { isFeatureAvailable, restoreCache } from "@actions/cache";
@@ -73,27 +74,40 @@ 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); // Check if Bun executable already exists and matches requested version
if (cacheRestored) { if (!options.customUrl && existsSync(bunPath)) {
revision = await getRevision(bunPath); const existingRevision = await getRevision(bunPath);
if (revision) { if (existingRevision && isVersionMatch(existingRevision, options.version)) {
cacheHit = true; revision = existingRevision;
info(`Using a cached version of Bun: ${revision}`); cacheHit = true; // Treat as cache hit to avoid unnecessary network requests
} else { info(`Using existing Bun installation: ${revision}`);
warning(
`Found a cached version of Bun: ${revision} (but it appears to be corrupted?)`,
);
}
} }
} }
if (!cacheHit) { if (!revision) {
info(`Downloading a new version of Bun: ${url}`); if (cacheEnabled) {
// TODO: remove this, temporary fix for https://github.com/oven-sh/setup-bun/issues/73 const cacheKey = createHash("sha1").update(url).digest("base64");
revision = await retry(async () => await downloadBun(url, bunPath), 3);
const cacheRestored = await restoreCache([bunPath], cacheKey);
if (cacheRestored) {
revision = await getRevision(bunPath);
if (revision) {
cacheHit = true;
info(`Using a cached version of Bun: ${revision}`);
} else {
warning(
`Found a cached version of Bun: ${revision} (but it appears to be corrupted?)`,
);
}
}
}
if (!cacheHit) {
info(`Downloading a new version of Bun: ${url}`);
// TODO: remove this, temporary fix for https://github.com/oven-sh/setup-bun/issues/73
revision = await retry(async () => await downloadBun(url, bunPath), 3);
}
} }
if (!revision) { if (!revision) {
@@ -122,6 +136,29 @@ export default async (options: Input): Promise<Output> => {
}; };
}; };
function isVersionMatch(
existingRevision: string,
requestedVersion?: string,
): boolean {
// If no version specified, default is "latest" - don't match existing
if (!requestedVersion) {
return false;
}
// Non-pinned versions should never match existing installations
if (/^(latest|canary|action)$/i.test(requestedVersion)) {
return false;
}
const [existingVersion] = existingRevision.split("+");
const normalizeVersion = (v: string) => v.replace(/^v/i, "");
return (
normalizeVersion(existingVersion) === normalizeVersion(requestedVersion)
);
}
async function downloadBun( async function downloadBun(
url: string, url: string,
bunPath: string, bunPath: string,