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
80 lines · 5.0 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 // On another Cloudflare-compatible API (Homeflare), a custom domain: the zone isn't Cloudflare's.
38 const onCloudflare = (process.env.CLOUDFLARE_API_BASE_URL ?? "https://api.cloudflare.com/").startsWith("https://api.cloudflare.com/");
39 cfg.routes = onCloudflare ? [{ pattern: `${url.hostname}/*`, zone_name: zone }] : [{ pattern: url.hostname, custom_domain: true }];
40 cfg.vars.MAIL_FROM = `notifications@${url.hostname}`;
41}
42if (values["mail-from"]) cfg.vars.MAIL_FROM = values["mail-from"];
43if (values.admins !== undefined) {
44 const admins = values.admins.split(",").map((s) => s.trim()).filter(Boolean);
45 // Only verified email addresses make site administrators (src/lib/directory.ts); a login would be ignored.
46 const bad = admins.filter((a) => !/^[^\s@,]+@[^\s@,]+$/.test(a));
47 if (bad.length) throw new Error(`--admins takes comma-separated email addresses; not an email address: ${bad.map((b) => JSON.stringify(b)).join(", ")}`);
48 cfg.vars.BOOTSTRAP_ADMINS = admins.join(",");
49}
50// A runner image already in the registry (scripts/runner-image.sh): containers use it
51// instead of building the Dockerfile on every deploy.
52if (process.env.RUNNER_IMAGE) {
53 // Named as the runner reports it (`runner.image` on a job), for /meta and the Live test.
54 cfg.vars.RUNNER_IMAGE = process.env.RUNNER_IMAGE.split("/").pop();
55 for (const c of cfg.containers) {
56 c.image = process.env.RUNNER_IMAGE;
57 delete c.image_build_context;
58 }
59}
60// The site key of the Turnstile widget scripts/turnstile.sh set up for this hostname (not a
61// secret). One set in wrangler.jsonc is the operator's own and wins; turnstile.sh then leaves it alone.
62if (process.env.TURNSTILE_SITE_KEY && !cfg.vars.TURNSTILE_SITE_KEY) cfg.vars.TURNSTILE_SITE_KEY = process.env.TURNSTILE_SITE_KEY;
63// The GitHub App's IDs (docs/self-hosting/configuration.md#github-runners) aren't secrets, but they are
64// the instance's own, so they come from the environment like the hostname rather than from
65// wrangler.jsonc; one that is empty there leaves wrangler.jsonc's value. Its secrets are Worker secrets.
66const githubApp = [
67 ["GITHUB_APP_ID", /^\d+$/, "the App ID, a number"],
68 ["GITHUB_APP_SLUG", /^[a-z0-9][a-z0-9-]*$/, "the slug, like diffinery-runners"],
69 ["GITHUB_APP_CLIENT_ID", /^\S+$/, "the Client ID, like Iv23li..."],
70 ["GITHUB_APP_URL", /^https:\/\/[^/\s]+$/, "an https origin, like https://ghe.example.com"],
71];
72for (const [key, shape, want] of githubApp) {
73 const value = process.env[key]?.trim();
74 if (!value) continue;
75 // Not the value itself: a secret set in the wrong variable would end up in the deploy log.
76 if (!shape.test(value)) throw new Error(`${key} must be ${want}`);
77 cfg.vars[key] = value;
78}
79writeFileSync(join(appDir, "wrangler.deploy.jsonc"), `// Generated by scripts/render-config.mjs; do not edit.\n${JSON.stringify(cfg, null, 2)}\n`);
80console.log(`wrote ${join(appDir, "wrangler.deploy.jsonc")}`);