fix: build id mismatch between CI + deploy (#1681)
This commit is contained in:
@@ -21,3 +21,8 @@ export interface BuildSnapshot {
|
|||||||
* Returns an empty array if the entrypoint does not exist. */
|
* Returns an empty array if the entrypoint does not exist. */
|
||||||
dependencies(path: string): string[];
|
dependencies(path: string): string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface BuildSnapshotJson {
|
||||||
|
build_id: string;
|
||||||
|
files: Record<string, string[]>;
|
||||||
|
}
|
||||||
|
|||||||
+8
-3
@@ -2,6 +2,8 @@ import { ServerContext } from "../server/context.ts";
|
|||||||
import { FreshOptions, Manifest } from "../server/mod.ts";
|
import { FreshOptions, Manifest } from "../server/mod.ts";
|
||||||
import { dirname, fromFileUrl, join, toFileUrl } from "../server/deps.ts";
|
import { dirname, fromFileUrl, join, toFileUrl } from "../server/deps.ts";
|
||||||
import { fs } from "./deps.ts";
|
import { fs } from "./deps.ts";
|
||||||
|
import { BuildSnapshotJson } from "../build/mod.ts";
|
||||||
|
import { BUILD_ID } from "$fresh/src/server/build_id.ts";
|
||||||
|
|
||||||
export async function build(
|
export async function build(
|
||||||
manifestPath: string,
|
manifestPath: string,
|
||||||
@@ -32,12 +34,15 @@ export async function build(
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
// Write dependency snapshot file to disk
|
// Write dependency snapshot file to disk
|
||||||
const deps: Record<string, string[]> = {};
|
const jsonSnapshot: BuildSnapshotJson = {
|
||||||
|
build_id: BUILD_ID,
|
||||||
|
files: {},
|
||||||
|
};
|
||||||
for (const filePath of snapshot.paths) {
|
for (const filePath of snapshot.paths) {
|
||||||
const dependencies = snapshot.dependencies(filePath);
|
const dependencies = snapshot.dependencies(filePath);
|
||||||
deps[filePath] = dependencies;
|
jsonSnapshot.files[filePath] = dependencies;
|
||||||
}
|
}
|
||||||
|
|
||||||
const snapshotPath = join(outDir, "snapshot.json");
|
const snapshotPath = join(outDir, "snapshot.json");
|
||||||
await Deno.writeTextFile(snapshotPath, JSON.stringify(deps, null, 2));
|
await Deno.writeTextFile(snapshotPath, JSON.stringify(jsonSnapshot, null, 2));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
import { toHashString } from "./deps.ts";
|
import { toHashString } from "./deps.ts";
|
||||||
|
|
||||||
const deploymentId = Deno.env.get("DENO_DEPLOYMENT_ID") ||
|
const deploymentId = Deno.env.get("DENO_DEPLOYMENT_ID") ||
|
||||||
|
// For CI
|
||||||
|
Deno.env.get("GITHUB_SHA") ||
|
||||||
crypto.randomUUID();
|
crypto.randomUUID();
|
||||||
const buildIdHash = await crypto.subtle.digest(
|
const buildIdHash = await crypto.subtle.digest(
|
||||||
"SHA-1",
|
"SHA-1",
|
||||||
new TextEncoder().encode(deploymentId),
|
new TextEncoder().encode(deploymentId),
|
||||||
);
|
);
|
||||||
|
|
||||||
export const BUILD_ID = toHashString(buildIdHash, "hex");
|
export let BUILD_ID = toHashString(buildIdHash, "hex");
|
||||||
|
|
||||||
|
export function setBuildId(buildId: string) {
|
||||||
|
BUILD_ID = buildId;
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import { ComponentType, h } from "preact";
|
|||||||
import * as router from "./router.ts";
|
import * as router from "./router.ts";
|
||||||
import { DenoConfig, Manifest } from "./mod.ts";
|
import { DenoConfig, Manifest } from "./mod.ts";
|
||||||
import { ALIVE_URL, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts";
|
import { ALIVE_URL, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts";
|
||||||
import { BUILD_ID } from "./build_id.ts";
|
import { BUILD_ID, setBuildId } from "./build_id.ts";
|
||||||
import DefaultErrorHandler from "./default_error_page.ts";
|
import DefaultErrorHandler from "./default_error_page.ts";
|
||||||
import {
|
import {
|
||||||
AppModule,
|
AppModule,
|
||||||
@@ -51,6 +51,7 @@ import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts";
|
|||||||
import {
|
import {
|
||||||
Builder,
|
Builder,
|
||||||
BuildSnapshot,
|
BuildSnapshot,
|
||||||
|
BuildSnapshotJson,
|
||||||
EsbuildBuilder,
|
EsbuildBuilder,
|
||||||
EsbuildSnapshot,
|
EsbuildSnapshot,
|
||||||
JSXConfig,
|
JSXConfig,
|
||||||
@@ -174,9 +175,13 @@ export class ServerContext {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const snapshotPath = join(snapshotDirPath, "snapshot.json");
|
const snapshotPath = join(snapshotDirPath, "snapshot.json");
|
||||||
const json = JSON.parse(await Deno.readTextFile(snapshotPath));
|
const json = JSON.parse(
|
||||||
|
await Deno.readTextFile(snapshotPath),
|
||||||
|
) as BuildSnapshotJson;
|
||||||
|
setBuildId(json.build_id);
|
||||||
|
|
||||||
const dependencies = new Map<string, string[]>(
|
const dependencies = new Map<string, string[]>(
|
||||||
Object.entries(json),
|
Object.entries(json.files),
|
||||||
);
|
);
|
||||||
|
|
||||||
const files = new Map();
|
const files = new Map();
|
||||||
|
|||||||
+32
-8
@@ -2,6 +2,8 @@ import * as path from "$std/path/mod.ts";
|
|||||||
import { puppeteer } from "./deps.ts";
|
import { puppeteer } from "./deps.ts";
|
||||||
import { assert } from "$std/_util/asserts.ts";
|
import { assert } from "$std/_util/asserts.ts";
|
||||||
import { startFreshServer, waitForText } from "$fresh/tests/test_utils.ts";
|
import { startFreshServer, waitForText } from "$fresh/tests/test_utils.ts";
|
||||||
|
import { BuildSnapshotJson } from "$fresh/src/build/mod.ts";
|
||||||
|
import { assertStringIncludes } from "$std/testing/asserts.ts";
|
||||||
|
|
||||||
Deno.test("build snapshot and restore from it", async (t) => {
|
Deno.test("build snapshot and restore from it", async (t) => {
|
||||||
const fixture = path.join(Deno.cwd(), "tests", "fixture_build");
|
const fixture = path.join(Deno.cwd(), "tests", "fixture_build");
|
||||||
@@ -16,6 +18,10 @@ Deno.test("build snapshot and restore from it", async (t) => {
|
|||||||
path.join(fixture, "dev.ts"),
|
path.join(fixture, "dev.ts"),
|
||||||
"build",
|
"build",
|
||||||
],
|
],
|
||||||
|
env: {
|
||||||
|
GITHUB_SHA: "__BUILD_ID__",
|
||||||
|
DENO_DEPLOYMENT_ID: "__BUILD_ID__",
|
||||||
|
},
|
||||||
stdin: "null",
|
stdin: "null",
|
||||||
stdout: "piped",
|
stdout: "piped",
|
||||||
stderr: "inherit",
|
stderr: "inherit",
|
||||||
@@ -31,24 +37,25 @@ Deno.test("build snapshot and restore from it", async (t) => {
|
|||||||
assert((await Deno.stat(outDir)).isDirectory, "Missing output directory");
|
assert((await Deno.stat(outDir)).isDirectory, "Missing output directory");
|
||||||
});
|
});
|
||||||
|
|
||||||
await t.step("check snapshot file", async () => {
|
const snapshot = JSON.parse(
|
||||||
const snapshot = JSON.parse(
|
await Deno.readTextFile(path.join(outDir, "snapshot.json")),
|
||||||
await Deno.readTextFile(path.join(outDir, "snapshot.json")),
|
) as BuildSnapshotJson;
|
||||||
);
|
|
||||||
|
await t.step("check snapshot file", () => {
|
||||||
assert(
|
assert(
|
||||||
Array.isArray(snapshot["island-counter_default.js"]),
|
Array.isArray(snapshot.files["island-counter_default.js"]),
|
||||||
"Island output file not found in snapshot",
|
"Island output file not found in snapshot",
|
||||||
);
|
);
|
||||||
assert(
|
assert(
|
||||||
Array.isArray(snapshot["main.js"]),
|
Array.isArray(snapshot.files["main.js"]),
|
||||||
"main.js output file not found in snapshot",
|
"main.js output file not found in snapshot",
|
||||||
);
|
);
|
||||||
assert(
|
assert(
|
||||||
Array.isArray(snapshot["signals.js"]),
|
Array.isArray(snapshot.files["signals.js"]),
|
||||||
"signals.js output file not found in snapshot",
|
"signals.js output file not found in snapshot",
|
||||||
);
|
);
|
||||||
assert(
|
assert(
|
||||||
Array.isArray(snapshot["deserializer.js"]),
|
Array.isArray(snapshot.files["deserializer.js"]),
|
||||||
"deserializer.js output file not found in snapshot",
|
"deserializer.js output file not found in snapshot",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -79,6 +86,23 @@ Deno.test("build snapshot and restore from it", async (t) => {
|
|||||||
await page.click("button");
|
await page.click("button");
|
||||||
|
|
||||||
await waitForText(page, "p", "1");
|
await waitForText(page, "p", "1");
|
||||||
|
|
||||||
|
// Ensure that it uses the build id from the snapshot
|
||||||
|
const assetUrls = await page.evaluate(() => {
|
||||||
|
const links = Array.from(document.querySelectorAll("link")).map(
|
||||||
|
(link) => link.href,
|
||||||
|
);
|
||||||
|
const scripts = Array.from(document.querySelectorAll("script"))
|
||||||
|
.filter((script) =>
|
||||||
|
script.src && !script.src.endsWith("refresh.js")
|
||||||
|
).map((script) => script.src);
|
||||||
|
|
||||||
|
return [...links, ...scripts];
|
||||||
|
});
|
||||||
|
|
||||||
|
for (let i = 0; i < assetUrls.length; i++) {
|
||||||
|
assertStringIncludes(assetUrls[i], snapshot.build_id);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
await browser.close();
|
await browser.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
import { useSignal } from "@preact/signals";
|
import { useSignal } from "@preact/signals";
|
||||||
import Counter from "../islands/Counter.tsx";
|
import Counter from "../islands/Counter.tsx";
|
||||||
|
import { asset, Head } from "$fresh/runtime.ts";
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
const signal = useSignal(0);
|
const signal = useSignal(0);
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
<Head>
|
||||||
|
<link rel="stylesheet" href={asset("/style.css")} />
|
||||||
|
</Head>
|
||||||
<Counter id="counter" count={signal} />
|
<Counter id="counter" count={signal} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
body {
|
||||||
|
background: peachpuff;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user