Skip to content

Commit ee9ef4a

Browse files
committed
feat: read buildId from GIT_REVISION env if present
1 parent 647d6f9 commit ee9ef4a

4 files changed

Lines changed: 150 additions & 17 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { BUILD_ID } from "@fresh/build-id";
2+
3+
export default function BuildIdPage() {
4+
return (
5+
<div>
6+
<div class="ready">Ready</div>
7+
<div id="build-id">{BUILD_ID}</div>
8+
</div>
9+
);
10+
}

packages/plugin-vite/src/plugins/build_id.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export function setBuildId(id) {
3939
}
4040

4141
export async function getBuildId(dev: boolean): Promise<string> {
42+
// Check for GIT_REVISION environment variable first
43+
const gitRevision = Deno.env.get("GIT_REVISION");
44+
if (gitRevision !== undefined) {
45+
return gitRevision;
46+
}
47+
4248
if (!dev) {
4349
const bin = Deno.build.os === "windows" ? "git.exe" : "git";
4450
const res = await new Deno.Command(bin, { args: ["rev-parse", "HEAD"] })

packages/plugin-vite/tests/build_test.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,99 @@ Deno.test({
296296
sanitizeOps: false,
297297
sanitizeResources: false,
298298
});
299+
300+
Deno.test({
301+
name: "vite build - build ID uses GIT_REVISION when set",
302+
fn: async () => {
303+
const testRevision = "test-commit-hash-123";
304+
await using res = await buildVite(DEMO_DIR, { GIT_REVISION: testRevision });
305+
306+
await withChildProcessServer(
307+
{
308+
cwd: res.tmp,
309+
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
310+
},
311+
async (address) => {
312+
await withBrowser(async (page) => {
313+
await page.goto(`${address}/tests/build_id`, {
314+
waitUntil: "networkidle2",
315+
});
316+
317+
await page.locator(".ready").wait();
318+
const buildId = await page.locator("#build-id").evaluate((el) =>
319+
// deno-lint-ignore no-explicit-any
320+
(el as any).textContent?.trim()
321+
);
322+
expect(buildId).toEqual(testRevision);
323+
});
324+
},
325+
);
326+
},
327+
sanitizeOps: false,
328+
sanitizeResources: false,
329+
});
330+
331+
Deno.test({
332+
name:
333+
"vite build - build ID generates unique value when GIT_REVISION not set",
334+
fn: async () => {
335+
await using res = await buildVite(DEMO_DIR);
336+
337+
await withChildProcessServer(
338+
{
339+
cwd: res.tmp,
340+
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
341+
},
342+
async (address) => {
343+
await withBrowser(async (page) => {
344+
await page.goto(`${address}/tests/build_id`, {
345+
waitUntil: "networkidle2",
346+
});
347+
348+
await page.locator(".ready").wait();
349+
const buildId = await page.locator("#build-id").evaluate((el) =>
350+
// deno-lint-ignore no-explicit-any
351+
(el as any).textContent?.trim()
352+
);
353+
354+
// Should be a non-empty string
355+
expect(buildId).toBeTruthy();
356+
expect(typeof buildId).toEqual("string");
357+
expect(buildId!.length).toBeGreaterThan(0);
358+
});
359+
},
360+
);
361+
},
362+
sanitizeOps: false,
363+
sanitizeResources: false,
364+
});
365+
366+
Deno.test({
367+
name: "vite build - build ID handles empty GIT_REVISION",
368+
fn: async () => {
369+
await using res = await buildVite(DEMO_DIR, { GIT_REVISION: "" });
370+
371+
await withChildProcessServer(
372+
{
373+
cwd: res.tmp,
374+
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
375+
},
376+
async (address) => {
377+
await withBrowser(async (page) => {
378+
await page.goto(`${address}/tests/build_id`, {
379+
waitUntil: "networkidle2",
380+
});
381+
382+
await page.locator(".ready").wait();
383+
const buildId = await page.locator("#build-id").evaluate((el) =>
384+
// deno-lint-ignore no-explicit-any
385+
(el as any).textContent?.trim()
386+
);
387+
expect(buildId).toEqual("");
388+
});
389+
},
390+
);
391+
},
392+
sanitizeOps: false,
393+
sanitizeResources: false,
394+
});

packages/plugin-vite/tests/test_utils.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,52 @@ export default defineConfig({
7878
);
7979
}
8080

81-
export async function buildVite(fixtureDir: string) {
81+
export async function buildVite(
82+
fixtureDir: string,
83+
env: Record<string, string> = {},
84+
) {
8285
const tmp = await withTmpDir({
8386
dir: path.join(import.meta.dirname!, ".."),
8487
prefix: "tmp_vite_",
8588
});
8689

87-
const builder = await createBuilder({
88-
root: fixtureDir,
89-
build: {
90-
emptyOutDir: true,
91-
},
92-
environments: {
93-
ssr: {
94-
build: {
95-
outDir: path.join(tmp.dir, "_fresh", "server"),
96-
},
90+
// Set environment variables for the build process
91+
const originalEnv: Record<string, string | undefined> = {};
92+
for (const [key, value] of Object.entries(env)) {
93+
originalEnv[key] = Deno.env.get(key);
94+
Deno.env.set(key, value);
95+
}
96+
97+
try {
98+
const builder = await createBuilder({
99+
root: fixtureDir,
100+
build: {
101+
emptyOutDir: true,
97102
},
98-
client: {
99-
build: {
100-
outDir: path.join(tmp.dir, "_fresh", "client"),
103+
environments: {
104+
ssr: {
105+
build: {
106+
outDir: path.join(tmp.dir, "_fresh", "server"),
107+
},
108+
},
109+
client: {
110+
build: {
111+
outDir: path.join(tmp.dir, "_fresh", "client"),
112+
},
101113
},
102114
},
103-
},
104-
});
105-
await builder.buildApp();
115+
});
116+
await builder.buildApp();
117+
} finally {
118+
// Restore original environment variables
119+
for (const [key, originalValue] of Object.entries(originalEnv)) {
120+
if (originalValue === undefined) {
121+
Deno.env.delete(key);
122+
} else {
123+
Deno.env.set(key, originalValue);
124+
}
125+
}
126+
}
106127

107128
return {
108129
tmp: tmp.dir,

0 commit comments

Comments
 (0)