Files
template-app/profiles/convex/deploy.mjs
T
2026-11-14 09:56:45 +00:00

85 lines
4.1 KiB
JavaScript

// SPDX-License-Identifier: LicenseRef-ConusAI-Template-1.0
// The linked service provisions the self-hosted backend; this runner installs only the app's
// already-scoped platform service credentials in that backend, then deploys the generated app.
// The environment sync is bounded HTTP; its errors never expose credential values.
// kit:guides:begin
//
// With the guides module this is also the Guides publish step (conusai.json declares
// `"guides": { "publish": true }` on convex-deploy): after a successful deploy it publishes the code
// guides and the anchor catalogue the image's build wrote. The platform hands this service alone the
// short-lived CONUSAI_GUIDES_PUBLISH_TOKEN, with CONUSAI_GUIDES_DEPLOYMENT, for a deployment a member
// started, and revokes it when the deploy job ends. Neither is written to the Convex backend.
// kit:guides:end
import { spawnSync } from "node:child_process"
import { join } from "node:path"
import { fileURLToPath } from "node:url"
import { syncPlatformEnvironment } from "./sync-env.mjs"
const required = (name) => {
const value = process.env[name]?.trim()
if (!value || /[\r\n]/.test(value)) throw new Error(`Convex deploy is missing a valid ${name}`)
return value
}
const root = fileURLToPath(new URL("../../", import.meta.url))
const cli = join(root, "node_modules/convex/bin/main.js")
const variables = ["CONUSAI_API_URL", "CONUSAI_API_TOKEN", "CONUSAI_PROJECT_ID", "CONUSAI_ORG_SLUG"]
const optional = ["AI_MODEL", "DOCUMENTS_EXCLUDED_PAGE_CLASSES"]
// The support channel's public key, for the support module's effect actions (convex/lib/effects.ts).
optional.push("CONUSAI_SUPPORT_CHANNEL") // kit:support
// kit:convex-better-auth:begin -- the account actions end a deleted person's Better Auth account
// (convex/lib/effects.ts): the Better Auth deployment's URL, its admin key and the identity namespace.
if (process.env.AUTH_PROVIDER?.trim() === "convex-better-auth") {
variables.push("AUTH_PROVIDER", "AUTH_IDENTITY_NAMESPACE", "BETTER_AUTH_CONVEX_URL", "BETTER_AUTH_CONVEX_ADMIN_KEY")
}
// kit:convex-better-auth:end
for (const name of optional) {
const value = process.env[name]?.trim()
if (value) {
if (/[\r\n]/.test(value)) throw new Error(`Convex deploy has an invalid ${name}`)
variables.push(name)
}
}
await syncPlatformEnvironment({
url: required("CONVEX_SELF_HOSTED_URL"),
adminKey: required("CONVEX_SELF_HOSTED_ADMIN_KEY"),
variables: Object.fromEntries(variables.map((name) => [name, required(name)])),
})
const deployed = spawnSync(process.execPath, [cli, "deploy", "--typecheck", "disable"], {
cwd: root,
env: process.env,
stdio: "inherit",
})
if (deployed.error) throw deployed.error
if (deployed.status !== 0) process.exitCode = deployed.status ?? 1
// kit:convex-better-auth:begin -- Better Auth's functions go to their own Convex deployment.
if (deployed.status === 0 && process.env.AUTH_PROVIDER?.trim() === "convex-better-auth") {
const betterAuth = spawnSync(process.execPath, [join(root, "better-auth/deploy.mjs")], {
env: process.env,
stdio: "inherit",
})
if (betterAuth.error || betterAuth.status !== 0) process.exitCode = 1
}
// kit:convex-better-auth:end
// kit:guides:begin -- after the functions are live, as the Postgres migrate job publishes after the
// schema; a refusal is logged and the deployment goes on, a broken catalogue or guide fails it.
if (!process.exitCode) {
const { existsSync } = await import("node:fs")
const { buildRegistry } = await import("@conusai-kit/core")
const { describeOutcome, publishGuidesAtDeploy } = await import("@conusai-kit/guides/deploy")
const { appModules } = await import("../../modules/dist/index.js")
const anchors = new URL("../../guides-anchors.json", import.meta.url)
const guides = await publishGuidesAtDeploy({
registry: buildRegistry([...appModules]),
env: {
CONUSAI_API_URL: process.env.CONUSAI_API_URL,
CONUSAI_GUIDES_PUBLISH_TOKEN: process.env.CONUSAI_GUIDES_PUBLISH_TOKEN,
CONUSAI_GUIDES_DEPLOYMENT: process.env.CONUSAI_GUIDES_DEPLOYMENT,
},
...(existsSync(anchors) ? { anchorsFile: fileURLToPath(anchors) } : {}),
})
console.info(`convex-deploy: ${describeOutcome(guides)}`)
}
// kit:guides:end