feat(vite): read build id from env if present (#3233)
Fix #3232 Disclaimer: This code was mostly generated by GitHub Copilot agent using Sonnet-4, but tested and verified by me. --------- Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
co-authored by
Marvin Hagemeister
parent
647d6f9adb
commit
9f09786b06
@@ -6,6 +6,7 @@ export const DENO_DEPLOYMENT_ID: string | undefined = Deno.env.get(
|
||||
const deploymentId = DENO_DEPLOYMENT_ID ||
|
||||
// For CI
|
||||
Deno.env.get("GITHUB_SHA") ||
|
||||
Deno.env.get("CI_COMMIT_SHA") ||
|
||||
crypto.randomUUID();
|
||||
const buildIdHash = await crypto.subtle.digest(
|
||||
"SHA-1",
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
import { BUILD_ID } from "@fresh/build-id";
|
||||
|
||||
export const handler = () => new Response(BUILD_ID);
|
||||
@@ -39,12 +39,22 @@ export function setBuildId(id) {
|
||||
}
|
||||
|
||||
export async function getBuildId(dev: boolean): Promise<string> {
|
||||
if (!dev) {
|
||||
const bin = Deno.build.os === "windows" ? "git.exe" : "git";
|
||||
const res = await new Deno.Command(bin, { args: ["rev-parse", "HEAD"] })
|
||||
.output();
|
||||
const gitRevision = Deno.env.get("DENO_DEPLOYMENT_ID") ??
|
||||
Deno.env.get("GITHUB_SHA") ?? Deno.env.get("CI_COMMIT_SHA");
|
||||
if (gitRevision !== undefined) {
|
||||
return gitRevision;
|
||||
}
|
||||
|
||||
return new TextDecoder().decode(res.stdout);
|
||||
if (!dev) {
|
||||
try {
|
||||
const bin = Deno.build.os === "windows" ? "git.exe" : "git";
|
||||
const res = await new Deno.Command(bin, { args: ["rev-parse", "HEAD"] })
|
||||
.output();
|
||||
|
||||
return new TextDecoder().decode(res.stdout);
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
const arr = new Uint32Array(1);
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
withBrowser,
|
||||
withChildProcessServer,
|
||||
} from "../../fresh/tests/test_utils.tsx";
|
||||
import { buildVite, DEMO_DIR, FIXTURE_DIR } from "./test_utils.ts";
|
||||
import { buildVite, DEMO_DIR, FIXTURE_DIR, usingEnv } from "./test_utils.ts";
|
||||
import * as path from "@std/path";
|
||||
|
||||
Deno.test({
|
||||
@@ -296,3 +296,47 @@ Deno.test({
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite build - build ID uses env variables when set",
|
||||
fn: async () => {
|
||||
const revision = "test-commit-hash-123";
|
||||
|
||||
// We're running on GitHub Actions, so GITHUB_SHA will always
|
||||
// be set
|
||||
Deno.env.delete("GITHUB_SHA");
|
||||
|
||||
for (
|
||||
const key of [
|
||||
"DENO_DEPLOYMENT_ID",
|
||||
"GITHUB_SHA",
|
||||
"CI_COMMIT_SHA",
|
||||
"OTHER",
|
||||
]
|
||||
) {
|
||||
using _ = usingEnv(key, revision);
|
||||
// deno-lint-ignore no-console
|
||||
console.log("Checking...", key, Deno.env.get(key));
|
||||
await using res = await buildVite(DEMO_DIR);
|
||||
|
||||
await withChildProcessServer(
|
||||
{
|
||||
cwd: res.tmp,
|
||||
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
|
||||
},
|
||||
async (address) => {
|
||||
const res = await fetch(`${address}/tests/build_id`);
|
||||
const text = await res.text();
|
||||
|
||||
if (key === "OTHER") {
|
||||
expect(text).not.toEqual(revision);
|
||||
} else {
|
||||
expect(text).toEqual(revision);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
@@ -113,10 +113,15 @@ export async function buildVite(fixtureDir: string) {
|
||||
}
|
||||
|
||||
export function usingEnv(name: string, value: string) {
|
||||
const prev = Deno.env.get(name);
|
||||
Deno.env.set(name, value);
|
||||
return {
|
||||
[Symbol.dispose]: () => {
|
||||
Deno.env.delete(name);
|
||||
if (prev === undefined) {
|
||||
Deno.env.delete(name);
|
||||
} else {
|
||||
Deno.env.set(name, prev);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user