Diffinery

homeflare/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

diffinery/scripts/render-config.mjs
78 lines · 4.7 KB · executableRaw History
1#!/usr/bin/env node
2// Render apps/diffinery/wrangler.deploy.jsonc from wrangler.jsonc plus values
3// discovered by scripts/bootstrap.sh (real D1 id, Access team domain and AUD).
4//
5// node scripts/render-config.mjs --d1 <uuid> [--team <x.cloudflareaccess.com>] [--aud <tag>]
6// [--public-url https://git.example.com] [--zone example.com] [--mail-from [email protected]]
7// [--admins [email protected],[email protected]]
8//
9// --public-url also points the Worker's route at that host (in --zone, by default
10// the host's last two labels) and sends email from notifications@<host>.
11// --admins sets BOOTSTRAP_ADMINS (comma-separated emails; bootstrap.sh passes what
12// scripts/bootstrap-admins.sh found). Without it, wrangler.jsonc's value stays.
13// From the environment it takes RUNNER_IMAGE (scripts/runner-image.sh),
14// TURNSTILE_SITE_KEY (scripts/turnstile.sh, through bootstrap) and the GitHub App of GitHub
15// runners: GITHUB_APP_ID, GITHUB_APP_SLUG, GITHUB_APP_CLIENT_ID and GITHUB_APP_URL.
16import { readFileSync, writeFileSync } from "node:fs";
17import { dirname, join } from "node:path";
18import { fileURLToPath } from "node:url";
19import { parseArgs } from "node:util";
20import { parseJsonc } from "./jsonc.mjs";
21
22const { values } = parseArgs({ options: { d1: { type: "string" }, team: { type: "string" }, aud: { type: "string" }, "public-url": { type: "string" }, zone: { type: "string" }, "mail-from": { type: "string" }, admins: { type: "string" } } });
23if (!values.d1) throw new Error("--d1 <database id> is required");
24
25const appDir = join(dirname(fileURLToPath(import.meta.url)), "..", "apps", "diffinery");
26const cfg = parseJsonc(readFileSync(join(appDir, "wrangler.jsonc"), "utf8"));
27delete cfg.env;
28delete cfg.$schema;
29cfg.d1_databases[0].database_id = values.d1;
30if (values.team) cfg.vars.ACCESS_TEAM_DOMAIN = values.team;
31if (values.aud) cfg.vars.ACCESS_AUD = values.aud;
32if (values["public-url"]) {
33 const url = new URL(values["public-url"]);
34 if (url.protocol !== "https:" || url.pathname !== "/") throw new Error("--public-url must be an https origin, like https://git.example.com");
35 cfg.vars.PUBLIC_URL = url.origin;
36 const zone = values.zone ?? url.hostname.split(".").slice(-2).join(".");
37 cfg.routes = [{ pattern: `${url.hostname}/*`, zone_name: zone }];
38 cfg.vars.MAIL_FROM = `notifications@${url.hostname}`;
39}
40if (values["mail-from"]) cfg.vars.MAIL_FROM = values["mail-from"];
41if (values.admins !== undefined) {
42 const admins = values.admins.split(",").map((s) => s.trim()).filter(Boolean);
43 // Only verified email addresses make site administrators (src/lib/directory.ts); a login would be ignored.
44 const bad = admins.filter((a) => !/^[^\s@,]+@[^\s@,]+$/.test(a));
45 if (bad.length) throw new Error(`--admins takes comma-separated email addresses; not an email address: ${bad.map((b) => JSON.stringify(b)).join(", ")}`);
46 cfg.vars.BOOTSTRAP_ADMINS = admins.join(",");
47}
48// A runner image already in the registry (scripts/runner-image.sh): containers use it
49// instead of building the Dockerfile on every deploy.
50if (process.env.RUNNER_IMAGE) {
51 // Named as the runner reports it (`runner.image` on a job), for /meta and the Live test.
52 cfg.vars.RUNNER_IMAGE = process.env.RUNNER_IMAGE.split("/").pop();
53 for (const c of cfg.containers) {
54 c.image = process.env.RUNNER_IMAGE;
55 delete c.image_build_context;
56 }
57}
58// The site key of the Turnstile widget scripts/turnstile.sh set up for this hostname (not a
59// secret). One set in wrangler.jsonc is the operator's own and wins; turnstile.sh then leaves it alone.
60if (process.env.TURNSTILE_SITE_KEY && !cfg.vars.TURNSTILE_SITE_KEY) cfg.vars.TURNSTILE_SITE_KEY = process.env.TURNSTILE_SITE_KEY;
61// The GitHub App's IDs (docs/self-hosting/configuration.md#github-runners) aren't secrets, but they are
62// the instance's own, so they come from the environment like the hostname rather than from
63// wrangler.jsonc; one that is empty there leaves wrangler.jsonc's value. Its secrets are Worker secrets.
64const githubApp = [
65 ["GITHUB_APP_ID", /^\d+$/, "the App ID, a number"],
66 ["GITHUB_APP_SLUG", /^[a-z0-9][a-z0-9-]*$/, "the slug, like diffinery-runners"],
67 ["GITHUB_APP_CLIENT_ID", /^\S+$/, "the Client ID, like Iv23li..."],
68 ["GITHUB_APP_URL", /^https:\/\/[^/\s]+$/, "an https origin, like https://ghe.example.com"],
69];
70for (const [key, shape, want] of githubApp) {
71 const value = process.env[key]?.trim();
72 if (!value) continue;
73 // Not the value itself: a secret set in the wrong variable would end up in the deploy log.
74 if (!shape.test(value)) throw new Error(`${key} must be ${want}`);
75 cfg.vars[key] = value;
76}
77writeFileSync(join(appDir, "wrangler.deploy.jsonc"), `// Generated by scripts/render-config.mjs; do not edit.\n${JSON.stringify(cfg, null, 2)}\n`);
78console.log(`wrote ${join(appDir, "wrangler.deploy.jsonc")}`);