fix: build id mismatch between CI + deploy (#1681)

This commit is contained in:
Marvin Hagemeister
2023-08-28 12:49:49 +02:00
committed by GitHub
parent fb6d11747b
commit efcaa2dee2
7 changed files with 67 additions and 15 deletions
+5
View File
@@ -21,3 +21,8 @@ export interface BuildSnapshot {
* Returns an empty array if the entrypoint does not exist. */
dependencies(path: string): string[];
}
export interface BuildSnapshotJson {
build_id: string;
files: Record<string, string[]>;
}
+8 -3
View File
@@ -2,6 +2,8 @@ import { ServerContext } from "../server/context.ts";
import { FreshOptions, Manifest } from "../server/mod.ts";
import { dirname, fromFileUrl, join, toFileUrl } from "../server/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(
manifestPath: string,
@@ -32,12 +34,15 @@ export async function build(
}));
// 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) {
const dependencies = snapshot.dependencies(filePath);
deps[filePath] = dependencies;
jsonSnapshot.files[filePath] = dependencies;
}
const snapshotPath = join(outDir, "snapshot.json");
await Deno.writeTextFile(snapshotPath, JSON.stringify(deps, null, 2));
await Deno.writeTextFile(snapshotPath, JSON.stringify(jsonSnapshot, null, 2));
}
+7 -1
View File
@@ -1,10 +1,16 @@
import { toHashString } from "./deps.ts";
const deploymentId = Deno.env.get("DENO_DEPLOYMENT_ID") ||
// For CI
Deno.env.get("GITHUB_SHA") ||
crypto.randomUUID();
const buildIdHash = await crypto.subtle.digest(
"SHA-1",
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;
}
+8 -3
View File
@@ -14,7 +14,7 @@ import { ComponentType, h } from "preact";
import * as router from "./router.ts";
import { DenoConfig, Manifest } from "./mod.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 {
AppModule,
@@ -51,6 +51,7 @@ import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts";
import {
Builder,
BuildSnapshot,
BuildSnapshotJson,
EsbuildBuilder,
EsbuildSnapshot,
JSXConfig,
@@ -174,9 +175,13 @@ export class ServerContext {
);
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[]>(
Object.entries(json),
Object.entries(json.files),
);
const files = new Map();