From 6baedb48896c9b71b6a51bbc307e40d314fa3be6 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Oct 2026 19:39:01 -0700 Subject: [PATCH 1/9] build(deploy): build every cloud service through one turbo graph apps/cloud becomes the private workspace package @zitadel/cloud with three turbo tasks: `build` (the launcher, apps/cloud/dist/nextgen-launcher, via build-launcher.sh with build.sh's ldflags and -tags noconsole), `schemas` (apps/cloud/dist/cloud-schemas) and `dev` (pnpm run cloud's script, replacing the root //#cloud). Moon gets no cloud project. - build.sh builds the launcher with `turbo run build --filter=@zitadel/cloud`, copies it to $VERCEL_OUTPUT_FILE and migrates; the migration is never cached. The launcher's key holds VERCEL_GIT_COMMIT_REF, the branch it stamps in to pick a preview's database, and leaves out .vercel/** and node_modules: turbo hashes the ignored files a glob matches, and on Vercel the Go toolchain and module cache lie in .vercel/cache/golang. The Go cache prune moved into the build task, so a cache hit does not empty Go's cache. - The docs and website services run `turbo run build --filter=...`. Their builds now see the Speed Insights sample rates (strict mode hid them), and are keyed by VERCEL_DEPLOYMENT_ID: Skew Protection bakes the deployment's ID into a Next.js build, so they never hit on Vercel. The docs' generate and typecheck drop the inferred NEXT_DEPLOYMENT_ID and still hit. The docs' build script is plain `next build`, as moon's. - The schemas service runs `turbo run schemas --filter=@zitadel/cloud` with the shared install and stays rooted at apps/cloud, whose package.json has no dependencies, so Vercel still detects no framework. - Runbook, README, turbo design docs and AGENTS.md files follow. Co-Authored-By: Claude Opus 5.5 --- .moon/AGENTS.md | 3 + apps/AGENTS.md | 10 +-- apps/cloud/README.md | 34 ++++++--- apps/cloud/build-launcher.sh | 43 ++++++++++++ apps/cloud/build.sh | 61 +++++++--------- apps/cloud/launcher/database.go | 6 +- apps/cloud/launcher/main_test.go | 4 +- apps/cloud/package.json | 12 ++++ apps/cloud/turbo.json | 73 ++++++++++++++++++++ apps/docs/AGENTS.md | 6 +- apps/docs/package.json | 2 +- apps/docs/turbo.json | 19 ++++- apps/server/moon.yml | 2 +- apps/server/turbo.json | 2 +- apps/website/AGENTS.md | 5 +- apps/website/turbo.json | 6 ++ docs/design/build/turborepo-migration.md | 11 +-- docs/design/build/turborepo.md | 31 ++++++--- docs/runbooks/cloud.md | 35 ++++++---- internal/staticui/console/embed_noconsole.go | 4 +- pnpm-lock.yaml | 2 + scripts/render-builtin-schemas.mjs | 14 ++-- scripts/run-cloud.mjs | 6 +- turbo.json | 10 +-- vercel.json | 10 +-- 25 files changed, 293 insertions(+), 118 deletions(-) create mode 100644 apps/cloud/build-launcher.sh create mode 100644 apps/cloud/package.json create mode 100644 apps/cloud/turbo.json diff --git a/.moon/AGENTS.md b/.moon/AGENTS.md index 594ca711ce..adfbe734ae 100644 --- a/.moon/AGENTS.md +++ b/.moon/AGENTS.md @@ -14,6 +14,9 @@ non-required `ci-turbo` job. Where the two differ on purpose: +- The hosted cloud is a turbo package (`@zitadel/cloud`: the launcher, the + schemas, `dev`) and no moon project. Moon's `workspace:cloud` runs the same + script as its `dev`, and `server:test` runs the launcher's tests. - Moon's `test` runs each package's `test:all` script, turbo runs `test`. In the browser-test packages, `test` wraps `scripts/task-guard.mjs` for turbo's retries and `test:all` calls Vitest directly, because moon has its diff --git a/apps/AGENTS.md b/apps/AGENTS.md index 94ff388f87..e66cf0c4c9 100644 --- a/apps/AGENTS.md +++ b/apps/AGENTS.md @@ -11,12 +11,14 @@ Conventions shared by the deployable apps. - `console` builds into `internal/staticui/console/dist`, the tree the Go server embeds ([`embed.go`](../internal/staticui/console/embed.go)). The cloud's `server` service compiles with `-tags noconsole` - ([`cloud/build.sh`](cloud/build.sh)) so it does not embed it as well. + ([`cloud/build-launcher.sh`](cloud/build-launcher.sh)) so it does not + embed it as well. - `docs` and `website` are Next.js: Vercel's Next.js preset reads `.next/`, so there is no output directory to set. - - The cloud's `schemas` service (`root: apps/cloud` in the repo-root - [`vercel.json`](../vercel.json)) renders the built-in schemas into the - repo-root `dist/cloud-schemas`. + - `cloud` holds two services' builds: the launcher binary in + `dist/nextgen-launcher`, and the `schemas` service's built-in schemas in + `dist/cloud-schemas` ([`vercel.json`](../vercel.json)), so neither + serves the other. ## What `next dev` writes diff --git a/apps/cloud/README.md b/apps/cloud/README.md index dc7a563605..69ccf1c016 100644 --- a/apps/cloud/README.md +++ b/apps/cloud/README.md @@ -38,24 +38,36 @@ the document whose `$id` is that URL, while the API keeps `/schemas` and The services install and build from `vercel.json` itself. The install leaves out the other platforms' native packages, so Vercel's build cache stays under its 1.5 GB limit, and pnpm skips it once a service built earlier -in the deployment has installed (`--optimistic-repeat-install`). The console -builds with `turbo run build`, which reads and writes the team's remote cache -as Vercel sets it up; the docs and the website run their own -`pnpm run build` after turbo has built the packages they render: the -website's showcase the atoms of `@zitadel/components`, the docs' live +in the deployment has installed (`--optimistic-repeat-install`). Every +service builds through turbo, so CI and the deployments share one task graph +and the team's remote cache, which each build reads and writes as Vercel +sets it up. The console runs `turbo run build`; the docs and the website run +`turbo run build --filter=…`, which builds the packages they render first: +the website's showcase the atoms of `@zitadel/components`, the docs' live examples the same package, `@zitadel/api-mock` and the `@zitadel/api` client -the mock calls. The schemas -need no install: their build is one Node script, and the service is rooted -here, where no `package.json` makes Vercel detect a framework. +the mock calls. Their own builds never come from the cache: Skew Protection +bakes the deployment's ID into a Next.js build, and their keys hold it. + +This folder is the package `@zitadel/cloud`, whose tasks +([`turbo.json`](turbo.json)) build the `server` and `schemas` services. +`build.sh`, the `server` service's build, builds the launcher through turbo +(`build`) and then migrates, which is never cached. The `schemas` service +runs `schemas` and is rooted here, where `package.json` has no +dependencies, so Vercel detects no framework (a service takes no +`framework: null`); a framework dependency in it would change how that +service builds. Moon has no project for the package: the launcher's Go +tests run in the server's `test`. | File | Role | |---|---| | [`launcher/`](launcher/) | the function binary: maps `CLOUD_DATABASE_URL` / `CLOUD_MIGRATOR_DATABASE_URL`, the master key, Vercel's `PORT` and its `x-vercel-id` request ID onto the server's configuration; at build time it creates a preview's database, checks the function's configuration and migrates | -| [`build.sh`](build.sh) | the `server` service's build: launcher, migrations | +| [`build.sh`](build.sh) | the `server` service's build: the launcher through turbo, then migrations | +| [`build-launcher.sh`](build-launcher.sh) | the `build` task: compiles the launcher, without the console, into `dist/nextgen-launcher`; turbo keys it by the branch it stamps in, which picks a preview's database | | [`setup-secrets.sh`](setup-secrets.sh) | mints an environment's master key and root admin (with [`admin-document.mjs`](admin-document.mjs)) and sets them on the Vercel project; the originals go to 1Password | -`pnpm run cloud` runs the same services on http://localhost:3000 with -`vercel dev`, the server on SQLite and a throwaway master key +`pnpm run cloud` (turbo: this package's `dev`) runs the same services on +http://localhost:3000 with `vercel dev`, the server on SQLite and a +throwaway master key ([`scripts/run-cloud.mjs`](../../scripts/run-cloud.mjs); the runbook's [Running the cloud locally](../../docs/runbooks/cloud.md#running-the-cloud-locally)). diff --git a/apps/cloud/build-launcher.sh b/apps/cloud/build-launcher.sh new file mode 100644 index 0000000000..a54970dce3 --- /dev/null +++ b/apps/cloud/build-launcher.sh @@ -0,0 +1,43 @@ +#!/bin/sh +# The `build` task of @zitadel/cloud (apps/cloud/turbo.json): compile the +# launcher into the `server` service's function binary, +# apps/cloud/dist/nextgen-launcher. build.sh, that service's build, runs it +# through turbo. +# +# The console is not embedded: it is a service of its own, and the launcher +# turns the server's console off. The services build one after another in +# one checkout, and the console service's build lands in the server's embed +# directory (internal/staticui/console/dist), so the launcher is compiled +# with -tags noconsole; without it, the function would carry the console +# whenever that service built first. +set -eu +cd "$(dirname "$0")/../.." + +version="$(node -p 'require("./apps/server/package.json").version')" +commit="${VERCEL_GIT_COMMIT_SHA:-$(git rev-parse HEAD 2>/dev/null || echo unknown)}" +# The branch picks a preview's database; stamped in so the serving function +# knows it even where Vercel exposes no Git metadata at runtime. turbo.json +# hashes it, so a cached binary never serves another branch. +ref="${VERCEL_GIT_COMMIT_REF:-}" +date="$(date -u +%Y-%m-%dT%H:%M:%SZ)" +out=apps/cloud/dist/nextgen-launcher +mkdir -p "$(dirname "$out")" + +CGO_ENABLED=0 go build -trimpath -tags noconsole \ + -ldflags "-s -w \ + -X github.com/zitadel/nextgen/internal/build.version=${version}+cloud \ + -X github.com/zitadel/nextgen/internal/build.commit=${commit} \ + -X github.com/zitadel/nextgen/internal/build.date=${date} \ + -X main.buildRef=${ref}" \ + -o "$out" ./apps/cloud/launcher +echo "built $out (version ${version}+cloud, commit ${commit})" + +# Go drops a build cache entry only after five days unused, and Vercel +# carries the cache from build to build, so every rebuild's packages would +# pile up towards the build cache's limit. On Vercel, drop what no build +# used in the last two hours (Go refreshes an entry's time at most hourly). +# Here rather than in build.sh: when turbo restores the binary, Go has +# refreshed nothing, and the prune would empty the cache. +if [ -n "${VERCEL:-}" ]; then + find -H "$(go env GOCACHE)" -mindepth 2 -type f -mmin +120 -delete +fi diff --git a/apps/cloud/build.sh b/apps/cloud/build.sh index 91b827c556..4137e60c3a 100644 --- a/apps/cloud/build.sh +++ b/apps/cloud/build.sh @@ -1,49 +1,36 @@ #!/bin/sh # Build step of the `server` service (repo-root vercel.json, Go preset): -# compile the launcher into the function binary, then migrate the -# deployment's database (a preview's is created first; see -# apps/cloud/launcher/database.go). -# -# The console is not embedded: it is a service of its own, and the launcher -# turns the server's console off. The services build one after another in -# one checkout, and the console service's build lands in the server's embed -# directory (internal/staticui/console/dist), so the launcher is compiled -# with -tags noconsole; without it, the function would carry the console -# whenever that service built first. +# build the launcher, the function binary, through turbo (@zitadel/cloud's +# `build` task, build-launcher.sh), so it comes from the team's remote cache +# when nothing it compiles changed, then migrate the deployment's database +# (a preview's is created first; see apps/cloud/launcher/database.go). # # Migrations run here, before the deployment goes live, never when a -# function starts. A failed migration fails the build and the previous -# deployment keeps serving, so every migration must leave the schema -# readable by the deployment still serving (expand/contract). +# function starts, and never from turbo's cache. A failed migration fails +# the build and the previous deployment keeps serving, so every migration +# must leave the schema readable by the deployment still serving +# (expand/contract). # -# Output: $VERCEL_OUTPUT_FILE on Vercel, dist/nextgen-launcher locally. +# Output: $VERCEL_OUTPUT_FILE on Vercel, apps/cloud/dist/nextgen-launcher +# locally. set -eu cd "$(dirname "$0")/../.." -version="$(node -p 'require("./apps/server/package.json").version')" -commit="${VERCEL_GIT_COMMIT_SHA:-$(git rev-parse HEAD 2>/dev/null || echo unknown)}" -# The branch picks a preview's database; stamped in so the serving function -# knows it even where Vercel exposes no Git metadata at runtime. -ref="${VERCEL_GIT_COMMIT_REF:-}" -date="$(date -u +%Y-%m-%dT%H:%M:%SZ)" -out="${VERCEL_OUTPUT_FILE:-dist/nextgen-launcher}" -mkdir -p "$(dirname "$out")" - -CGO_ENABLED=0 go build -trimpath -tags noconsole \ - -ldflags "-s -w \ - -X github.com/zitadel/nextgen/internal/build.version=${version}+cloud \ - -X github.com/zitadel/nextgen/internal/build.commit=${commit} \ - -X github.com/zitadel/nextgen/internal/build.date=${date} \ - -X main.buildRef=${ref}" \ - -o "$out" ./apps/cloud/launcher -echo "built $out (version ${version}+cloud, commit ${commit})" - -# Go drops a build cache entry only after five days unused, and Vercel -# carries the cache from build to build, so every rebuild's packages would -# pile up towards the build cache's limit. On Vercel, drop what no build -# used in the last two hours (Go refreshes an entry's time at most hourly). if [ -n "${VERCEL_OUTPUT_FILE:-}" ]; then - find -H "$(go env GOCACHE)" -mindepth 2 -type f -mmin +120 -delete + # The Go preset runs no installCommand, so the install the other services + # declare in vercel.json runs here; pnpm skips it once a service built + # earlier in the deployment has installed. + pnpm install --os=current --cpu=current --optimistic-repeat-install +fi +# turbo's own binary: through `pnpm exec`, every task's `pnpm run` would +# re-check the workspace and warn about each other platform's package. +node_modules/.bin/turbo run build --filter=@zitadel/cloud + +launcher=apps/cloud/dist/nextgen-launcher +out="${VERCEL_OUTPUT_FILE:-$launcher}" +if [ "$out" != "$launcher" ]; then + mkdir -p "$(dirname "$out")" + cp "$launcher" "$out" fi "$out" migrate diff --git a/apps/cloud/launcher/database.go b/apps/cloud/launcher/database.go index 5fe9b49f50..aac572a402 100644 --- a/apps/cloud/launcher/database.go +++ b/apps/cloud/launcher/database.go @@ -35,9 +35,9 @@ const ( ) // buildRef is the Git branch the binary was built from, stamped in by -// build.sh. A CLI deployment carries no Git metadata at runtime, so the -// serving function would otherwise not know its preview database; with the -// stamp, the build and the function always agree on it. +// build-launcher.sh. A CLI deployment carries no Git metadata at runtime, +// so the serving function would otherwise not know its preview database; +// with the stamp, the build and the function always agree on it. var buildRef string // gitRef is the branch this deployment was built from. diff --git a/apps/cloud/launcher/main_test.go b/apps/cloud/launcher/main_test.go index 693e9b7cd0..79cece1695 100644 --- a/apps/cloud/launcher/main_test.go +++ b/apps/cloud/launcher/main_test.go @@ -338,7 +338,7 @@ func TestVercelJSONKeepsTheServerInFrankfurt(t *testing.T) { // checkout, so the function is compiled without it; a plain build would // carry the console whenever that service built first. func TestBuildScriptLeavesTheConsoleOut(t *testing.T) { - raw, err := os.ReadFile(filepath.Join("..", "build.sh")) + raw, err := os.ReadFile(filepath.Join("..", "build-launcher.sh")) require.NoError(t, err) var builds []string for _, line := range strings.Split(string(raw), "\n") { @@ -346,7 +346,7 @@ func TestBuildScriptLeavesTheConsoleOut(t *testing.T) { builds = append(builds, line) } } - require.Len(t, builds, 1, "build.sh compiles the function once") + require.Len(t, builds, 1, "build-launcher.sh compiles the function once") assert.Contains(t, builds[0], "-tags noconsole") } diff --git a/apps/cloud/package.json b/apps/cloud/package.json new file mode 100644 index 0000000000..47db26c584 --- /dev/null +++ b/apps/cloud/package.json @@ -0,0 +1,12 @@ +{ + "name": "@zitadel/cloud", + "version": "0.0.0", + "private": true, + "license": "AGPL-3.0-only", + "type": "module", + "scripts": { + "build": "sh build-launcher.sh", + "schemas": "node ../../scripts/render-builtin-schemas.mjs", + "dev": "node ../../scripts/run-cloud.mjs" + } +} diff --git a/apps/cloud/turbo.json b/apps/cloud/turbo.json new file mode 100644 index 0000000000..5648f9f70d --- /dev/null +++ b/apps/cloud/turbo.json @@ -0,0 +1,73 @@ +{ + "$schema": "https://turborepo.dev/schema.json", + "extends": ["//"], + // The hosted cloud on Vercel: what the repo-root vercel.json's `server` and + // `schemas` services build. Moon has no project for it (moon's + // workspace:cloud is this package's `dev`), and the launcher's Go tests run + // in @zitadel/server#test (`go test ./...`). + "tasks": { + // The `server` service's function binary (build-launcher.sh), which + // build.sh builds through turbo and then migrates with; the migration is + // never cached. Its inputs are the Go files and what they embed + // (//go:embed), as in apps/server/moon.yml's serverInputs, and the + // server's version, which it stamps in. The OpenAPI YAML reaches the + // binary only through the generated Go code. Turbo hashes the ignored + // files a glob matches, so the trees that hold other Go code are + // excluded: node_modules, agents' worktrees and, on Vercel, the Go + // toolchain and module cache inside the checkout (.vercel/cache/golang). + // + // It also stamps in the branch, which picks a preview's database + // (launcher/database.go), so VERCEL_GIT_COMMIT_REF is hashed: a binary is + // never restored into another branch's deployment. The commit and date it + // stamps in are those of the build that made it (turbo passes VERCEL_* + // through unhashed). CGO_ENABLED is pinned in the script. + "build": { + "env": ["GOFLAGS", "GOOS", "GOARCH", "VERCEL_GIT_COMMIT_REF"], + "inputs": [ + "$TURBO_DEFAULT$", + "$TURBO_ROOT$/**/*.go", + "$TURBO_ROOT$/**/*.sql", + "$TURBO_ROOT$/go.mod", + "$TURBO_ROOT$/go.sum", + "$TURBO_ROOT$/api/**/*.json", + "$TURBO_ROOT$/packages/config/defaults/**", + "$TURBO_ROOT$/internal/errorpage/*", + "$TURBO_ROOT$/packages/design-tokens/src/generated/tokens.css", + "$TURBO_ROOT$/internal/**/*.json", + "$TURBO_ROOT$/apps/server/package.json", + "!$TURBO_ROOT$/**/node_modules/**", + "!$TURBO_ROOT$/.claude/**", + "!$TURBO_ROOT$/.codex/**", + "!$TURBO_ROOT$/.cursor/**", + "!$TURBO_ROOT$/.agents/**", + "!$TURBO_ROOT$/.vercel/**" + ], + "outputs": ["dist/nextgen-launcher"] + }, + // The `schemas` service's static files: the built-in schemas the server + // embeds, rendered as the documents their identifiers name + // (scripts/render-builtin-schemas.mjs). Node only: on Vercel the + // service has no Go toolchain, so it renders the committed copies and + // waits for no generation. + "schemas": { + "inputs": [ + "$TURBO_DEFAULT$", + "$TURBO_ROOT$/scripts/render-builtin-schemas.mjs", + "$TURBO_ROOT$/api/openapi/endpoints/schemas/*.json" + ], + "outputs": ["dist/cloud-schemas/**"] + }, + // The cloud on localhost: `vercel dev` over vercel.json's services + // (scripts/run-cloud.mjs), which builds what the services' dev servers + // read itself. `pnpm run cloud` runs the same script without a runner. + "dev": { + "inputs": [ + "$TURBO_DEFAULT$", + "$TURBO_ROOT$/scripts/run-cloud.mjs", + "$TURBO_ROOT$/scripts/dev-process.mjs", + "$TURBO_ROOT$/scripts/task-runner.mjs" + ], + "passThroughEnv": ["CLOUD_DEV_PORT", "CLOUD_DEV_DEBUG", "NEXTGEN_*", "GOFLAGS"] + } + } +} diff --git a/apps/docs/AGENTS.md b/apps/docs/AGENTS.md index d7cccea28f..a3a5122a3c 100644 --- a/apps/docs/AGENTS.md +++ b/apps/docs/AGENTS.md @@ -115,8 +115,10 @@ moon run docs:e2e # tests/ against the local build CLOUD_URL=https://cloud-….vercel.app moon run docs:e2e-deployment ``` -- Vercel builds with `pnpm run build`, never moon: moon cannot archive the - symlinks Vercel's Next.js adapter writes into `.next/output`. +- Vercel builds with `turbo run build --filter=@zitadel/docs`, never moon: + moon cannot archive the symlinks Vercel's Next.js adapter writes into + `.next/output`. The build's key holds the deployment's ID, so Vercel never + restores it ([turborepo.md](../../docs/design/build/turborepo.md)). - `tests/` is a contract suite written against a URL, so the same specs check the local build and any deployment. A deployment run needs `VERCEL_AUTOMATION_BYPASS_SECRET`. diff --git a/apps/docs/package.json b/apps/docs/package.json index 3262d5f58f..f1e38d9fe0 100644 --- a/apps/docs/package.json +++ b/apps/docs/package.json @@ -10,7 +10,7 @@ "scripts": { "generate": "node scripts/bundle-openapi.mjs && node scripts/bundle-cli.mjs", "dev": "node scripts/bundle-openapi.mjs && node scripts/bundle-cli.mjs && next dev --port 3003", - "build": "node scripts/bundle-openapi.mjs && node scripts/bundle-cli.mjs && next build", + "build": "next build", "start": "next start --port 3003", "typecheck": "next typegen && tsc --noEmit -p tsconfig.app.json && tsc --noEmit -p tsconfig.spec.json && node scripts/sdk-samples.ts && tsc -p tsconfig.samples.json", "lint": "biome check .", diff --git a/apps/docs/turbo.json b/apps/docs/turbo.json index cc2c27fae2..31cc0b4733 100644 --- a/apps/docs/turbo.json +++ b/apps/docs/turbo.json @@ -7,7 +7,13 @@ // lib/error-reference.ts imports, and the CLI's command reference, which // its reference test writes and lib/cli-reference.ts imports, with the // CLI's release notes, which Changesets writes on every release. + // + // Turbo hashes every NEXT_* variable into the tasks of a Next.js package, + // so on Vercel each deployment's NEXT_DEPLOYMENT_ID (see build) would key + // these too; nothing here depends on it, so generate and typecheck drop + // it and still hit the cache there. "generate": { + "env": ["!NEXT_DEPLOYMENT_ID"], "inputs": [ "$TURBO_DEFAULT$", "$TURBO_ROOT$/api/openapi/**/*.yaml", @@ -30,6 +36,7 @@ // The live examples (components/examples) import the components, the // mock and the client the mock calls, each from its dist/. "typecheck": { + "env": ["!NEXT_DEPLOYMENT_ID"], "dependsOn": [ "generate", "@zitadel/api#generate", @@ -39,8 +46,6 @@ ], "inputs": ["$TURBO_DEFAULT$", "$TURBO_ROOT$/packages/api/src/**"] }, - // The build script bundles again before `next build`, because the - // hosted cloud's docs service runs the script without a task runner. // app/global.css imports the design tokens' CSS variables, which the // repository commits, so that build needs no generation of them. // @@ -48,7 +53,17 @@ // .next/types, where tsc reads the route types, so side by side // typecheck failed with TS2304. A task-guard mutex would leave a gap: a // cache hit restores .next without running the script. + // + // The hosted cloud's docs service builds through this task. A Next.js + // build on Vercel bakes its deployment in: Skew Protection's ID into + // every asset URL (`?dpl=`, from NEXT_DEPLOYMENT_ID) and Vercel's URLs + // into the metadata, which turbo passes through unhashed. Keyed by the + // deployment's ID, a build is never restored into another deployment: on + // Vercel it never hits, elsewhere the variable is unset. The Speed + // Insights sample rate is read at build time (next.config.mjs, + // app/layout.tsx), and strict mode would hide it. "build": { + "env": ["VERCEL_DEPLOYMENT_ID", "DOCS_SPEED_INSIGHTS_SAMPLE_RATE"], "dependsOn": [ "typecheck", "generate", diff --git a/apps/server/moon.yml b/apps/server/moon.yml index 1c66aa7f06..5a6eecbc2d 100644 --- a/apps/server/moon.yml +++ b/apps/server/moon.yml @@ -199,7 +199,7 @@ tasks: # the deployment's configuration together. Scoped to test: nothing the # server compiles reads them. - "/vercel.json" - - "/apps/cloud/build.sh" + - "/apps/cloud/build-launcher.sh" options: runFromWorkspaceRoot: true diff --git a/apps/server/turbo.json b/apps/server/turbo.json index 8fa7b39eb7..79dd88c987 100644 --- a/apps/server/turbo.json +++ b/apps/server/turbo.json @@ -215,7 +215,7 @@ "$TURBO_ROOT$/scripts/generate-meta-schemas.ts", // Read by the cloud launcher's tests, as in moon.yml. "$TURBO_ROOT$/vercel.json", - "$TURBO_ROOT$/apps/cloud/build.sh" + "$TURBO_ROOT$/apps/cloud/build-launcher.sh" ] }, // The database lanes talk to a database whose state no hash sees, so diff --git a/apps/website/AGENTS.md b/apps/website/AGENTS.md index 898e6267fa..088d9af3ed 100644 --- a/apps/website/AGENTS.md +++ b/apps/website/AGENTS.md @@ -116,5 +116,6 @@ moon run website:typecheck website:lint website:test website:build moon run website:start # the build on port 3004 ``` -Vercel runs `turbo run build --filter=@zitadel/components`, then -`pnpm run build`; the Next build itself never goes through a task runner. +Vercel runs `turbo run build --filter=@zitadel/website`; the build's key +holds the deployment's ID, so Vercel never restores it +([turborepo.md](../../docs/design/build/turborepo.md)). diff --git a/apps/website/turbo.json b/apps/website/turbo.json index 33804c4a0e..6e525517cd 100644 --- a/apps/website/turbo.json +++ b/apps/website/turbo.json @@ -17,7 +17,13 @@ // dist/. The changelog finds the release that ships each entry in the // published packages' release notes, which Changesets writes on every // release (app/changelog/entries.ts). + // + // The hosted cloud's website service builds through this task, keyed by + // the deployment's ID as the docs' build is (apps/docs/turbo.json): on + // Vercel it never hits. The Speed Insights sample rate is read at build + // time (next.config.ts, app/layout.tsx), and strict mode would hide it. "build": { + "env": ["VERCEL_DEPLOYMENT_ID", "WEBSITE_SPEED_INSIGHTS_SAMPLE_RATE"], "dependsOn": ["@zitadel/components#build"], "inputs": [ "$TURBO_DEFAULT$", diff --git a/docs/design/build/turborepo-migration.md b/docs/design/build/turborepo-migration.md index bdd1ead5af..2696daaee1 100644 --- a/docs/design/build/turborepo-migration.md +++ b/docs/design/build/turborepo-migration.md @@ -184,7 +184,7 @@ as they were. | demo-nuxt | `build` (outputs .output, mutex sdk-nuxt-dist) | `#build` | outputs `.output/**` | | e2e | `test` (journey harness unit tests); lanes `console-smoke`, `console-real`, `console-embedded`, `console-platform`, `console-deployment`, `demo-next-mock`, `demo-next-real`, `demo-nuxt-mock`, `journey`, `journey-local` (env; the demo-next pair's mutex `demo-next-app`); `all` (noop over every lane; ci.yml's e2e step runs it) | `@zitadel/e2e#…`, same lane names, plus `e2e-local`; no `all` | scripts carry the `NEXTGEN_*`, `ZITADEL_TELEMETRY` and `JOURNEY_TARBALLS_REQUIRED` defaults; the demo-next pair holds the guard's `demo-next-app` lock; ci-turbo.yml runs the lanes as gated steps | | docs | `generate` (inputs openapi + redocly) | `#generate` | same inputs and outputs | -| docs | `build` (`next build`, mergeArgs replace) | `#build` | existing script (`pnpm run generate && next build`): bundles once more than moon; the Vercel docs service runs the script without a runner | +| docs | `build` (`next build`, mergeArgs replace) | `#build` | the script is `next build`, as moon's command; the Vercel docs service runs the task, keyed by the deployment's ID (`VERCEL_DEPLOYMENT_ID`) | | docs | `dev`, `start` | same | existing | | docs | `e2e` (dep build), `e2e-deployment` (its own Playwright config, which requires `CLOUD_URL`) | same | **new** scripts; the root's `check:cloud` runs `e2e-deployment` with the e2e package's `console-deployment` | | console | `build` env inputs `$CONSOLE_VERCEL_INSIGHTS`, `$CONSOLE_SPEED_INSIGHTS_SAMPLE_RATE` | `#build` `env` | both listed: strict mode would otherwise hide them from vite.config.ts. Neither is `VITE_`-prefixed: Vite inference would hash a prefixed one into every Vite package of the cloud's console build (as `VITE_CONSOLE_VERCEL_INSIGHTS`, it had `components` built twice per deployment) | @@ -196,6 +196,7 @@ as they were. | release | `changesets`, `check-graph`, `build-public-packages`, `version`, `pack`, `snapshot`, `publish` | `@zitadel/release#…` | **new** `tools/release/package.json` (`cd ../.. && node scripts/release.mjs …`) | | workspace | `test`, `check-typecheck`, `check-openapi-rules`, `check-moon-graph`, `check-adrs` | `//#…` | root scripts (**new** for the four checks), explicit inputs as in moon.yml | | workspace | `doctor`, `server`, `server-debug`, `build-local-image`, `smoke-container`, `check` | `//#…` | root scripts (**new** `server-debug`, `build-local-image`, `smoke-container`); `journey` moved to `@zitadel/e2e#journey`, and moon keeps `workspace:journey` as a deprecated alias | +| workspace | `cloud` (persistent) | `@zitadel/cloud#dev` | **new** `apps/cloud/package.json`, no moon project; its `build` (the cloud's launcher) and `schemas` are what the cloud's `server` and `schemas` services build | | workspace | `cli` (interactive) | — | not a turbo task: interactive tasks need turbo's terminal UI. `pnpm run cli -- …` (the root script that exists today) | | cli-skill-e2e (no moon project) | — | — | its `test` script is renamed `eval` (`eval:fresh` for `FRESH=1`), otherwise `turbo run test` would run a Claude-driven eval | @@ -379,8 +380,10 @@ token. console-deployment --filter=@zitadel/e2e`), the tasks the root's `pnpm check:cloud` runs by hand, and the step summary's "Reproduce" line names that command. -- **Vercel:** the repo-root `vercel.json` and `apps/cloud/build.sh` already - build with turbo (pilot). `apps/console/vercel.json` belongs to the paused +- **Vercel:** every service of the repo-root `vercel.json` builds through + turbo: the console, the docs and the website with `turbo run build`, the + server (`apps/cloud/build.sh`) and the schemas through `@zitadel/cloud`'s + `build` and `schemas`. `apps/console/vercel.json` belongs to the paused legacy `nextgen` project and still says `moon run console:build`; it is deleted or updated with that project. - **Dockerfile:** the comment "Consumed by Moon release tasks" is text only. @@ -420,7 +423,7 @@ Two workable setups; the prototype's draft uses the first. | | Vercel Remote Cache (OIDC) | Depot's turbo cache | |---|---|---| | Setup | a repository variable `TURBO_TEAM=zitadel`, `id-token: write` on the job, `vercel/setup-turborepo-remote-cache-action@v1.1.0` before the first `turbo` (with `policy:` set to the policy's ID only if more than one of the team's Turborepo policies matches the token: Vercel then refuses the exchange), `TURBO_API=https://vercel.com` on Depot runners (they inject Depot's `TURBO_API`), and a **Turborepo CLI OIDC policy** in the Vercel team settings (Build and Deployment → OIDC Policies for CLI Access → Turborepo CLI Policies, not a VCR policy; select GitHub, repository `zitadel/nextgen`; optionally restrict to a workflow or branch) | nothing on Depot runners: `TURBO_API`, `TURBO_TOKEN`, `TURBO_TEAM` are injected | -| Shared with | the cloud deployments (same team cache): a build CI did is a hit on Vercel and the reverse | CI only | +| Shared with | the cloud deployments (same team cache, apart by key: turbo hashes `VERCEL_TARGET_ENV` into every task on Vercel, so a build CI did is no hit there) | CI only | | Cost | free, within a fair-use limit of 1 TB/month of uploads on Pro; artifacts expire after 7 days | part of the Depot plan (as moon's cache today) | | Forks | no token, local cache only | same | | Write policy | `TURBO_CACHE=local:rw,remote:rw` on every run: pushes to main (which run the gate alone), runs by hand, and pull requests and the merge queue while they run it (decision 9), as moon's runs write Depot's cache; on Vercel, every build reads and writes, Vercel's default, production and previews in separate entries (see "Who may write to the cache") | same flags | diff --git a/docs/design/build/turborepo.md b/docs/design/build/turborepo.md index 1620112c76..d39b4a7d05 100644 --- a/docs/design/build/turborepo.md +++ b/docs/design/build/turborepo.md @@ -24,7 +24,9 @@ builds, and locally with `NEXTGEN_TASK_RUNNER=turbo`. add an empty script to fill a gap. Moon's `server`, `bench` and `release` projects are the packages `@zitadel/server`, `@zitadel/bench` (`tools/bench`) and `@zitadel/release` (`tools/release`); their scripts - `cd` to the directory the command needs. + `cd` to the directory the command needs. `@zitadel/cloud` (`apps/cloud`, + the hosted cloud's launcher and schemas) has no moon project: its `dev` is + moon's `workspace:cloud`. - Task names are script names: `dev:real`, `test:integration` and `sync:export` keep their colons. The e2e lanes are `@zitadel/e2e`'s `console-smoke`, `console-real`, `console-embedded`, `console-platform`, @@ -68,6 +70,10 @@ builds, and locally with `NEXTGEN_TASK_RUNNER=turbo`. and lists `["$TURBO_DEFAULT$", ".env*"]`; without them, a changed `.env.local` would restore a build made without it. Moon hashes only what git sees, listed or not ([.moon/AGENTS.md](../../../.moon/AGENTS.md)). +- A listed glob matches ignored files too: `$TURBO_ROOT$/**/*.go` hashes + `node_modules`, agents' worktrees (`.claude/`, …) and, on Vercel, the Go + toolchain and module cache in `.vercel/cache/golang`. Exclude them + (`@zitadel/cloud#build`). - Outputs may lie outside the package (`$TURBO_ROOT$/internal/staticui/ console/dist/**`). Framework caches are excluded (`!.next/cache/**`). - `cache: false` for anything that is not a pure function of its inputs: the @@ -104,7 +110,9 @@ builds, and locally with `NEXTGEN_TASK_RUNNER=turbo`. leaves them out only in the Vite service's build, and the same packages would get other keys in the storybook's and the server's builds. `VERCEL_*` still reaches every task unhashed (turbo's built-in list): a - build whose output depends on one lists it in its `env`. + build whose output depends on one lists it in its `env` (the cloud's + launcher `VERCEL_GIT_COMMIT_REF`, the Next.js builds + `VERCEL_DEPLOYMENT_ID`). - A task cannot set a variable's value. Values a lane needs (moon's task `env:` blocks) are set in the `package.json` script as defaults that a caller's own value overrides, as it overrides moon's task `env`: @@ -167,15 +175,20 @@ git worktree turbo's cache defaults to the main checkout's `.turbo/cache`; set `TURBO_CACHE_DIR` to keep worktrees apart. Telemetry is off in CI and on Vercel (`TURBO_TELEMETRY_DISABLED=1`); set it locally too. -On Vercel, the hosted cloud's services build with the plain `turbo run build` -Vercel itself uses (repo-root `vercel.json`; the server's login UI in -`apps/cloud/build.sh`). Two project variables, `TURBO_TELEMETRY_DISABLED` +On Vercel, every service of the hosted cloud builds through turbo (repo-root +`vercel.json`): the console, the docs and the website with +`turbo run build`, the server with `@zitadel/cloud`'s `build` +(`apps/cloud/build.sh`, which then migrates outside turbo) and the schemas +with its `schemas`. Two project variables, `TURBO_TELEMETRY_DISABLED` and `TURBO_PLATFORM_ENV_DISABLED`, turn off telemetry and turbo's warning about each project variable `turbo.json` does not pass ([runbook](../../runbooks/cloud.md)). The cache works as Vercel sets it up: every build reads and writes the team's remote cache, production and previews in separate entries, and keeps no local cache. Writing takes the -team's token, which a fork's pull request never gets unapproved. The docs -and the website do not build through turbo: with Skew Protection on, a -Next.js build's cache key includes the deployment's ID, so no build could -reuse another's. +team's token, which a fork's pull request never gets unapproved. The docs' +and the website's builds never hit there: a Next.js build bakes in its +deployment (Skew Protection's ID in its asset URLs, Vercel's URLs in its +metadata), so their keys hold `VERCEL_DEPLOYMENT_ID`. Turbo infers +`NEXT_DEPLOYMENT_ID` into every task of a Next.js package, so the docs' +`generate` and `typecheck`, which do not depend on it, drop it +(`!NEXT_DEPLOYMENT_ID`) and still hit. diff --git a/docs/runbooks/cloud.md b/docs/runbooks/cloud.md index 631fbae27b..7610e5373f 100644 --- a/docs/runbooks/cloud.md +++ b/docs/runbooks/cloud.md @@ -26,11 +26,11 @@ One Vercel deployment, five [services](https://vercel.com/docs/services): | Service | Built by | Serves | |---|---|---| -| `server` | Go preset; `apps/cloud/build.sh` compiles `apps/cloud/launcher` without the console (`-tags noconsole`), then migrates | every path except the console's, the docs', the website's and the Mixpanel proxy's, including `/console/runtime.json` | +| `server` | Go preset; `apps/cloud/build.sh` builds `apps/cloud/launcher` without the console (`-tags noconsole`) through turbo, `@zitadel/cloud`'s `build` task, then migrates | every path except the console's, the docs', the website's and the Mixpanel proxy's, including `/console/runtime.json` | | `console` | Vite; `turbo run build` with `CONSOLE_BASE_PATH=/console/` and `CONSOLE_VERCEL_INSIGHTS=1`, so it reports Vercel Web Analytics and Speed Insights; the service serves the build where it lands, `internal/staticui/console/dist` | `/console/*` (bare `/console` redirects to the slash form; `/ui/console` and `/ui/console/*` redirect here with a 307) | -| `docs` | Next.js; `turbo run build` for `@zitadel/components`, `@zitadel/api-mock` and the `@zitadel/api` client the mock calls, which the live examples render, then `apps/docs`' own `pnpm run build`, without a task runner: moon cannot archive the symlinks Vercel's Next.js adapter writes, and turbo could never reuse a build, whose cache key holds the deployment's ID under Skew Protection | `/docs` and `/docs/*`: pages, assets (`/docs/_next/`), search, `llms.txt`, each page's `.md` and the MCP server | -| `website` | Next.js; `turbo run build --filter=@zitadel/components`, for the atoms its showcase renders, then `apps/website`'s own `pnpm run build`, without a task runner, as the docs' | `/`, the front page, `/changelog` (`/changelog/` redirects there) with its entries' pages and its feed under it, `/changelog/*`, and their assets at `/_next/*`; the host's `/llms.txt`; the host's favicons at `/favicon.ico` and `/favicons/*`, which the console, the login UI and the docs link | -| `schemas` | Static files: Node runs [`scripts/render-builtin-schemas.mjs`](../../scripts/render-builtin-schemas.mjs), which needs no install, into `dist/cloud-schemas`. The service is rooted at `apps/cloud`, which holds no `package.json`, so Vercel detects no framework | `/schemas/v1/*`: the [built-in schemas](#built-in-schemas), as `application/schema+json`, fresh for an hour and readable from any origin | +| `docs` | Next.js; `turbo run build --filter=@zitadel/docs`, after `@zitadel/components`, `@zitadel/api-mock` and the `@zitadel/api` client the mock calls, which the live examples render, and the docs' typecheck. The build's cache key holds the deployment's ID, which Skew Protection bakes into it, so it never comes from the cache; never moon, which cannot archive the symlinks Vercel's Next.js adapter writes | `/docs` and `/docs/*`: pages, assets (`/docs/_next/`), search, `llms.txt`, each page's `.md` and the MCP server | +| `website` | Next.js; `turbo run build --filter=@zitadel/website`, after the atoms of `@zitadel/components` its showcase renders; keyed by the deployment's ID, as the docs' | `/`, the front page, `/changelog` (`/changelog/` redirects there) with its entries' pages and its feed under it, `/changelog/*`, and their assets at `/_next/*`; the host's `/llms.txt`; the host's favicons at `/favicon.ico` and `/favicons/*`, which the console, the login UI and the docs link | +| `schemas` | Static files: `turbo run schemas --filter=@zitadel/cloud` renders them with [`scripts/render-builtin-schemas.mjs`](../../scripts/render-builtin-schemas.mjs) into `apps/cloud/dist/cloud-schemas`. The service is rooted at `apps/cloud`, whose `package.json` has no dependencies, so Vercel detects no framework: a service takes no `framework: null`, and a framework dependency there would change how it builds | `/schemas/v1/*`: the [built-in schemas](#built-in-schemas), as `application/schema+json`, fresh for an hour and readable from any origin | The console lives on the same origin, at `/console/` rather than a self-hosted server's `/ui/console/`. The server's own console is switched @@ -38,7 +38,7 @@ off and its console path set to `/console`, so the console links it derives (claim, dashboard) point there; the session cookie and CSRF protection work as in a self-hosted install. The function does not carry the console either: the console service's build lands in the server's embed directory of the -same checkout, so `build.sh` compiles the launcher with `-tags noconsole`. +same checkout, so `build-launcher.sh` compiles the launcher with `-tags noconsole`. The server's `/console/runtime.json` lies under the console's path, so its rewrite comes first in `vercel.json`, and a launcher test holds the mount, the build's base and the console path together. @@ -395,7 +395,8 @@ the deployment as the CLI's does, a status in `production-checks.yml`'s ## Running the cloud locally -`pnpm run cloud` (`moon run workspace:cloud`) runs this deployment at +`pnpm run cloud` (`moon run workspace:cloud`; turbo's is +`turbo run dev --filter=@zitadel/cloud`) runs this deployment at http://localhost:3000: `vercel dev -L` over `vercel.json`, every service behind the deployment's own routes on one origin, the server on SQLite. No Vercel account, link or secret is involved, and Go need not be installed: Vercel's Go @@ -417,8 +418,8 @@ not use: - The `server` service's `entrypoint` names `apps/cloud/launcher`. In dev, Vercel's Go builder builds that package and starts it on `$PORT`; a - deployment runs `build.sh`, which compiles the same package, and the - builder takes the build command's binary instead. Without + deployment runs `build.sh`, which builds the same package through turbo, + and the builder takes the build command's binary instead. Without `CLOUD_DATABASE_URL` and outside a deployment, the launcher serves from SQLite and migrates it at startup. - The `console` service's `devCommand` builds it and serves the build behind @@ -562,11 +563,14 @@ project and imports the root admin. - **Signing in:** `/console/` with the environment's root admin from 1Password (production and preview have different passwords). Anyone else registers through the console's sign-in page. -- **Turbo's remote cache:** the console's build, and the package builds the - docs and the website start with, read and write the team's remote cache, - production and previews in separate entries; each build's log shows - `cache hit` or `cache miss` per task, and - the deployment's Run Summary shows why a task missed. A build with a +- **Turbo's remote cache:** every service builds through turbo and reads and + writes the team's remote cache, production and previews in separate + entries: the console, the launcher (a preview's also keyed by its branch, + which picks its database), the schemas and the packages the docs and the + website render. The docs' and the website's own builds never come from it: + their keys hold the deployment's ID. Each build's log shows `cache hit` or + `cache miss` per task, and the deployment's Run Summary shows why a task + missed. A build with a result that is wrong although its inputs are right (a broken or planted entry) is fixed by a team owner clearing the cache (team Settings → Build and Deployment → Remote Caching); a redeploy without the build cache @@ -594,8 +598,9 @@ project and imports the root admin. blindly: a fresh checkout's manifests are newer than its last install, so pnpm compares the lockfile with the one that `node_modules` was installed from and installs when they differ. Go would keep each rebuild's compiled - packages for five days, so `build.sh` drops those no build used in the - last two hours. A new Go version starts the Go caches over: when the + packages for five days, so `build-launcher.sh` drops those no build used + in the last two hours, after it compiled: a binary turbo restores + refreshes none. A new Go version starts the Go caches over: when the toolchain Vercel picks from `go.mod`'s `toolchain` line differs from the restored one, its Go builder downloads it and deletes `.vercel/cache/golang`, module cache included. The next build of each diff --git a/internal/staticui/console/embed_noconsole.go b/internal/staticui/console/embed_noconsole.go index 945650a03e..8b5dc84606 100644 --- a/internal/staticui/console/embed_noconsole.go +++ b/internal/staticui/console/embed_noconsole.go @@ -6,6 +6,6 @@ import "io/fs" // Built with -tags noconsole, the binary carries no console: dist stays nil, // so Handler and ValidateDist refuse. The hosted cloud's function builds this -// way (apps/cloud/build.sh): its console is a service of its own, whose build -// lands in dist/ of the same checkout. +// way (apps/cloud/build-launcher.sh): its console is a service of its own, +// whose build lands in dist/ of the same checkout. var dist fs.FS diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5a8cd4b86d..1def94158f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -733,6 +733,8 @@ importers: specifier: 'catalog:' version: 4.1.11(@edge-runtime/vm@5.0.0)(@opentelemetry/api@1.9.1)(@types/node@24.19.1)(@vitest/browser-playwright@4.1.11)(@vitest/coverage-v8@4.1.11)(happy-dom@20.14.5)(jsdom@29.0.2(@noble/hashes@1.8.0))(msw@2.14.6(@types/node@24.19.1)(typescript@6.0.3))(vite@8.3.1(@types/node@24.19.1)(esbuild@0.28.2)(jiti@2.7.0)(less@4.6.4)(sass-embedded@1.104.1)(sass@1.104.1)(terser@5.49.0)(tsx@4.21.0)(yaml@2.9.1)) + apps/cloud: {} + apps/console: dependencies: '@fontsource/arimo': diff --git a/scripts/render-builtin-schemas.mjs b/scripts/render-builtin-schemas.mjs index 263f4347df..f2f119b44a 100644 --- a/scripts/render-builtin-schemas.mjs +++ b/scripts/render-builtin-schemas.mjs @@ -4,9 +4,10 @@ * (`api/openapi/endpoints/schemas/*.json`) as the documents their identifiers * name, so that a URL such as https://zitadel.com/schemas/v1/user-schema.json * can answer with the document whose `$id` it is. The hosted cloud's `schemas` - * service (repo-root vercel.json) runs it as its build and serves the files - * under the same path; zitadel.com answers for the namespace by forwarding that - * path to the cloud (docs/runbooks/cloud.md#built-in-schemas): + * service (repo-root vercel.json) builds it as @zitadel/cloud's `schemas` task + * and serves the files under the same path; zitadel.com answers for the + * namespace by forwarding that path to the cloud + * (docs/runbooks/cloud.md#built-in-schemas): * * node scripts/render-builtin-schemas.mjs [--base URL] [--out DIR] [--serve] * @@ -26,7 +27,8 @@ * so the server's build cannot hand the files to a static service. * * Output: `//.json`, by default - * `dist/cloud-schemas/schemas/v1/`, the layout the service serves them under. + * `apps/cloud/dist/cloud-schemas/schemas/v1/`, the layout the service serves + * them under. * The build fails when a document refers to one under the base that is not a * built-in, so an editor following a `$ref` never meets a 404. */ @@ -57,8 +59,8 @@ export const HOSTED_BASE = "https://zitadel.com/schemas/v1"; /** The built-in schemas the server embeds, generated from the OpenAPI YAML. */ export const SOURCE_DIR = join(ROOT, "api/openapi/endpoints/schemas"); -/** The `schemas` service's output directory (vercel.json). */ -export const DEFAULT_OUT_DIR = join(ROOT, "dist/cloud-schemas"); +/** The `schemas` service's output directory (vercel.json, apps/cloud/turbo.json). */ +export const DEFAULT_OUT_DIR = join(ROOT, "apps/cloud/dist/cloud-schemas"); // What the server's isRelativeRef treats as absolute (absoluteScheme). const ABSOLUTE_HTTP = /^https?:\/\//; diff --git a/scripts/run-cloud.mjs b/scripts/run-cloud.mjs index 86fd5529db..20e7ff2327 100644 --- a/scripts/run-cloud.mjs +++ b/scripts/run-cloud.mjs @@ -10,7 +10,7 @@ * * - server: `entrypoint` names apps/cloud/launcher. Vercel's Go builder builds * it in dev and starts it on $PORT; a deployment runs build.sh instead, - * which compiles the same package. The launcher, without + * which builds the same package through turbo. The launcher, without * CLOUD_DATABASE_URL, serves from SQLite and migrates it at startup. * - console: its `devCommand` builds it and serves the build, as the * deployment serves it, behind the same service rewrites. Its dev server @@ -20,8 +20,8 @@ * What only a local run needs comes from here: the builds the dev servers * read from disk, a throwaway master key and root admin minted on the first * run under .zitadel/local/cloud/, where the SQLite database lives too, and - * the function compiled without the console (-tags noconsole) as build.sh - * compiles it. No Vercel account, link or secret is involved (-L), and Go + * the function compiled without the console (-tags noconsole) as + * build-launcher.sh compiles it. No Vercel account, link or secret is involved (-L), and Go * need not be installed: Vercel's Go builder downloads the version go.mod * names. */ diff --git a/turbo.json b/turbo.json index b420047489..58de701ece 100644 --- a/turbo.json +++ b/turbo.json @@ -5,8 +5,8 @@ // here, the rest in each package's turbo.json ("extends": ["//"]), which // mirrors that package's moon.yml edge for edge. Both runners run the same // package.json scripts. Moon stays the runner of record (CI, local docs) - // until the cutover; the hosted cloud (vercel.json, apps/cloud/build.sh) - // builds with turbo since the pilot. + // until the cutover; the hosted cloud's services build through turbo + // (vercel.json, apps/cloud), which moon has no project for. // // Without this, turbo writes its own block into the root AGENTS.md whenever // it detects an AI agent running it. @@ -214,12 +214,6 @@ "inputs": ["scripts/run-server.mjs", "scripts/dev-process.mjs", "scripts/task-runner.mjs"], "passThroughEnv": ["ZITADEL_*", "NEXTGEN_*", "GOFLAGS"] }, - "//#cloud": { - "cache": false, - "persistent": true, - "inputs": ["scripts/run-cloud.mjs", "scripts/dev-process.mjs", "scripts/task-runner.mjs"], - "passThroughEnv": ["CLOUD_DEV_PORT", "CLOUD_DEV_DEBUG", "NEXTGEN_*", "GOFLAGS"] - }, "//#server-debug": { "cache": false, "persistent": true, diff --git a/vercel.json b/vercel.json index 1e4010ddd6..34bbbad87a 100644 --- a/vercel.json +++ b/vercel.json @@ -34,20 +34,20 @@ "root": "apps/docs", "framework": "nextjs", "installCommand": "pnpm install --os=current --cpu=current --optimistic-repeat-install", - "buildCommand": "turbo run build --filter=@zitadel/api --filter=@zitadel/api-mock --filter=@zitadel/components && pnpm run build" + "buildCommand": "turbo run build --filter=@zitadel/docs" }, "website": { "root": "apps/website", "framework": "nextjs", "installCommand": "pnpm install --os=current --cpu=current --optimistic-repeat-install", - "buildCommand": "turbo run build --filter=@zitadel/components && pnpm run build" + "buildCommand": "turbo run build --filter=@zitadel/website" }, "schemas": { "root": "apps/cloud", - "installCommand": "true", - "buildCommand": "node ../../scripts/render-builtin-schemas.mjs", + "installCommand": "pnpm install --os=current --cpu=current --optimistic-repeat-install", + "buildCommand": "turbo run schemas --filter=@zitadel/cloud", "devCommand": "node ../../scripts/render-builtin-schemas.mjs --serve", - "outputDirectory": "../../dist/cloud-schemas", + "outputDirectory": "dist/cloud-schemas", "headers": [ { "source": "/schemas/v1/(.*)", From fb1f5279269ddb9fd3dca5cf62909c67fc85ef29 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Oct 2026 19:49:12 -0700 Subject: [PATCH 2/9] build(deploy): keep the cloud doc edits to what the package needs Revert wording the package does not need, so the change stays clear of the open PRs that edit the same files: the README's build.sh row (beside the row #2397 changes), two runbook phrasings, the root turbo.json header and the turborepo.md bullet on ignored files in input globs, which the rework of #2420 covers. The cloud task's own comment keeps why it excludes .vercel/ and the other ignored trees. Co-Authored-By: Claude Opus 5.5 --- apps/cloud/README.md | 2 +- docs/design/build/turborepo.md | 4 ---- docs/runbooks/cloud.md | 7 +++---- turbo.json | 4 ++-- 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/apps/cloud/README.md b/apps/cloud/README.md index 69ccf1c016..2219e86a1f 100644 --- a/apps/cloud/README.md +++ b/apps/cloud/README.md @@ -61,7 +61,7 @@ tests run in the server's `test`. | File | Role | |---|---| | [`launcher/`](launcher/) | the function binary: maps `CLOUD_DATABASE_URL` / `CLOUD_MIGRATOR_DATABASE_URL`, the master key, Vercel's `PORT` and its `x-vercel-id` request ID onto the server's configuration; at build time it creates a preview's database, checks the function's configuration and migrates | -| [`build.sh`](build.sh) | the `server` service's build: the launcher through turbo, then migrations | +| [`build.sh`](build.sh) | the `server` service's build: launcher, migrations | | [`build-launcher.sh`](build-launcher.sh) | the `build` task: compiles the launcher, without the console, into `dist/nextgen-launcher`; turbo keys it by the branch it stamps in, which picks a preview's database | | [`setup-secrets.sh`](setup-secrets.sh) | mints an environment's master key and root admin (with [`admin-document.mjs`](admin-document.mjs)) and sets them on the Vercel project; the originals go to 1Password | diff --git a/docs/design/build/turborepo.md b/docs/design/build/turborepo.md index d39b4a7d05..419e11a441 100644 --- a/docs/design/build/turborepo.md +++ b/docs/design/build/turborepo.md @@ -70,10 +70,6 @@ builds, and locally with `NEXTGEN_TASK_RUNNER=turbo`. and lists `["$TURBO_DEFAULT$", ".env*"]`; without them, a changed `.env.local` would restore a build made without it. Moon hashes only what git sees, listed or not ([.moon/AGENTS.md](../../../.moon/AGENTS.md)). -- A listed glob matches ignored files too: `$TURBO_ROOT$/**/*.go` hashes - `node_modules`, agents' worktrees (`.claude/`, …) and, on Vercel, the Go - toolchain and module cache in `.vercel/cache/golang`. Exclude them - (`@zitadel/cloud#build`). - Outputs may lie outside the package (`$TURBO_ROOT$/internal/staticui/ console/dist/**`). Framework caches are excluded (`!.next/cache/**`). - `cache: false` for anything that is not a pure function of its inputs: the diff --git a/docs/runbooks/cloud.md b/docs/runbooks/cloud.md index 7610e5373f..187692e14b 100644 --- a/docs/runbooks/cloud.md +++ b/docs/runbooks/cloud.md @@ -395,8 +395,7 @@ the deployment as the CLI's does, a status in `production-checks.yml`'s ## Running the cloud locally -`pnpm run cloud` (`moon run workspace:cloud`; turbo's is -`turbo run dev --filter=@zitadel/cloud`) runs this deployment at +`pnpm run cloud` (`moon run workspace:cloud`) runs this deployment at http://localhost:3000: `vercel dev -L` over `vercel.json`, every service behind the deployment's own routes on one origin, the server on SQLite. No Vercel account, link or secret is involved, and Go need not be installed: Vercel's Go @@ -418,8 +417,8 @@ not use: - The `server` service's `entrypoint` names `apps/cloud/launcher`. In dev, Vercel's Go builder builds that package and starts it on `$PORT`; a - deployment runs `build.sh`, which builds the same package through turbo, - and the builder takes the build command's binary instead. Without + deployment runs `build.sh`, which compiles the same package, and the + builder takes the build command's binary instead. Without `CLOUD_DATABASE_URL` and outside a deployment, the launcher serves from SQLite and migrates it at startup. - The `console` service's `devCommand` builds it and serves the build behind diff --git a/turbo.json b/turbo.json index 58de701ece..67e139580a 100644 --- a/turbo.json +++ b/turbo.json @@ -5,8 +5,8 @@ // here, the rest in each package's turbo.json ("extends": ["//"]), which // mirrors that package's moon.yml edge for edge. Both runners run the same // package.json scripts. Moon stays the runner of record (CI, local docs) - // until the cutover; the hosted cloud's services build through turbo - // (vercel.json, apps/cloud), which moon has no project for. + // until the cutover; the hosted cloud (vercel.json, apps/cloud/build.sh) + // builds with turbo since the pilot. // // Without this, turbo writes its own block into the root AGENTS.md whenever // it detects an AI agent running it. From 299048754d92d2170595d1db7cd4c29d60773d09 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Oct 2026 19:52:26 -0700 Subject: [PATCH 3/9] build(deploy): add an empty changeset for the server's task inputs apps/server/turbo.json and moon.yml lie under @zitadel/server's publishable root, so the changeset check wants a changeset; they only rename the file the launcher's tests read (build-launcher.sh), and nothing ships. Co-Authored-By: Claude Opus 5.5 --- .changeset/cloud-turbo-package.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cloud-turbo-package.md diff --git a/.changeset/cloud-turbo-package.md b/.changeset/cloud-turbo-package.md new file mode 100644 index 0000000000..064435dc68 --- /dev/null +++ b/.changeset/cloud-turbo-package.md @@ -0,0 +1,5 @@ +--- + +--- + + From 840685d1e45e57bdb40e8c20699026eec37b1fce Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Oct 2026 20:04:58 -0700 Subject: [PATCH 4/9] build(deploy): run the cloud's copy and migration as an uncached turbo task The server service's build command now installs (the Go preset runs no installCommand) and runs `turbo run deploy --filter=@zitadel/cloud`. @zitadel/cloud's new `deploy` task depends on `build` (the launcher, from the cache when nothing it compiles changed) and runs deploy.sh, the former build.sh without its install and turbo call: copy the binary to $VERCEL_OUTPUT_FILE, then migrate. It is never cached, and it passes the whole environment through (`passThroughEnv: ["*"]`): the launcher checks every variable the function needs at build time, so a variable strict mode hid (CRON_SECRET, which #2397 adds, or any later one) would fail every deployment. Co-Authored-By: Claude Opus 5.5 --- .moon/AGENTS.md | 4 +-- apps/cloud/README.md | 8 ++++-- apps/cloud/build-launcher.sh | 6 ++-- apps/cloud/build.sh | 36 ------------------------ apps/cloud/deploy.sh | 26 +++++++++++++++++ apps/cloud/launcher/main.go | 2 +- apps/cloud/package.json | 1 + apps/cloud/turbo.json | 17 +++++++++-- docs/design/build/turborepo-migration.md | 6 ++-- docs/design/build/turborepo.md | 8 ++++-- docs/runbooks/cloud.md | 6 ++-- scripts/run-cloud.mjs | 5 ++-- turbo.json | 2 +- vercel.json | 2 +- 14 files changed, 69 insertions(+), 60 deletions(-) delete mode 100644 apps/cloud/build.sh create mode 100644 apps/cloud/deploy.sh diff --git a/.moon/AGENTS.md b/.moon/AGENTS.md index adfbe734ae..adc256e99e 100644 --- a/.moon/AGENTS.md +++ b/.moon/AGENTS.md @@ -14,8 +14,8 @@ non-required `ci-turbo` job. Where the two differ on purpose: -- The hosted cloud is a turbo package (`@zitadel/cloud`: the launcher, the - schemas, `dev`) and no moon project. Moon's `workspace:cloud` runs the same +- The hosted cloud is a turbo package (`@zitadel/cloud`: the launcher and + its migration, the schemas, `dev`) and no moon project. Moon's `workspace:cloud` runs the same script as its `dev`, and `server:test` runs the launcher's tests. - Moon's `test` runs each package's `test:all` script, turbo runs `test`. In the browser-test packages, `test` wraps `scripts/task-guard.mjs` for diff --git a/apps/cloud/README.md b/apps/cloud/README.md index 2219e86a1f..360d3b227a 100644 --- a/apps/cloud/README.md +++ b/apps/cloud/README.md @@ -50,8 +50,10 @@ bakes the deployment's ID into a Next.js build, and their keys hold it. This folder is the package `@zitadel/cloud`, whose tasks ([`turbo.json`](turbo.json)) build the `server` and `schemas` services. -`build.sh`, the `server` service's build, builds the launcher through turbo -(`build`) and then migrates, which is never cached. The `schemas` service +The `server` service's build command installs, since the Go preset runs no +`installCommand`, and runs `deploy`: turbo builds the launcher (`build`), +then `deploy` hands it to Vercel and migrates, never from the cache and +with the deployment's whole environment. The `schemas` service runs `schemas` and is rooted here, where `package.json` has no dependencies, so Vercel detects no framework (a service takes no `framework: null`); a framework dependency in it would change how that @@ -61,8 +63,8 @@ tests run in the server's `test`. | File | Role | |---|---| | [`launcher/`](launcher/) | the function binary: maps `CLOUD_DATABASE_URL` / `CLOUD_MIGRATOR_DATABASE_URL`, the master key, Vercel's `PORT` and its `x-vercel-id` request ID onto the server's configuration; at build time it creates a preview's database, checks the function's configuration and migrates | -| [`build.sh`](build.sh) | the `server` service's build: launcher, migrations | | [`build-launcher.sh`](build-launcher.sh) | the `build` task: compiles the launcher, without the console, into `dist/nextgen-launcher`; turbo keys it by the branch it stamps in, which picks a preview's database | +| [`deploy.sh`](deploy.sh) | the `deploy` task, the `server` service's build after `build`: copies the launcher to `$VERCEL_OUTPUT_FILE`, then migrates | | [`setup-secrets.sh`](setup-secrets.sh) | mints an environment's master key and root admin (with [`admin-document.mjs`](admin-document.mjs)) and sets them on the Vercel project; the originals go to 1Password | `pnpm run cloud` (turbo: this package's `dev`) runs the same services on diff --git a/apps/cloud/build-launcher.sh b/apps/cloud/build-launcher.sh index a54970dce3..18396084a6 100644 --- a/apps/cloud/build-launcher.sh +++ b/apps/cloud/build-launcher.sh @@ -1,8 +1,8 @@ #!/bin/sh # The `build` task of @zitadel/cloud (apps/cloud/turbo.json): compile the # launcher into the `server` service's function binary, -# apps/cloud/dist/nextgen-launcher. build.sh, that service's build, runs it -# through turbo. +# apps/cloud/dist/nextgen-launcher. The service's build command runs it +# through turbo as a dependency of `deploy` (deploy.sh), which migrates. # # The console is not embedded: it is a service of its own, and the launcher # turns the server's console off. The services build one after another in @@ -36,7 +36,7 @@ echo "built $out (version ${version}+cloud, commit ${commit})" # carries the cache from build to build, so every rebuild's packages would # pile up towards the build cache's limit. On Vercel, drop what no build # used in the last two hours (Go refreshes an entry's time at most hourly). -# Here rather than in build.sh: when turbo restores the binary, Go has +# Here rather than in deploy.sh: when turbo restores the binary, Go has # refreshed nothing, and the prune would empty the cache. if [ -n "${VERCEL:-}" ]; then find -H "$(go env GOCACHE)" -mindepth 2 -type f -mmin +120 -delete diff --git a/apps/cloud/build.sh b/apps/cloud/build.sh deleted file mode 100644 index 4137e60c3a..0000000000 --- a/apps/cloud/build.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/sh -# Build step of the `server` service (repo-root vercel.json, Go preset): -# build the launcher, the function binary, through turbo (@zitadel/cloud's -# `build` task, build-launcher.sh), so it comes from the team's remote cache -# when nothing it compiles changed, then migrate the deployment's database -# (a preview's is created first; see apps/cloud/launcher/database.go). -# -# Migrations run here, before the deployment goes live, never when a -# function starts, and never from turbo's cache. A failed migration fails -# the build and the previous deployment keeps serving, so every migration -# must leave the schema readable by the deployment still serving -# (expand/contract). -# -# Output: $VERCEL_OUTPUT_FILE on Vercel, apps/cloud/dist/nextgen-launcher -# locally. -set -eu -cd "$(dirname "$0")/../.." - -if [ -n "${VERCEL_OUTPUT_FILE:-}" ]; then - # The Go preset runs no installCommand, so the install the other services - # declare in vercel.json runs here; pnpm skips it once a service built - # earlier in the deployment has installed. - pnpm install --os=current --cpu=current --optimistic-repeat-install -fi -# turbo's own binary: through `pnpm exec`, every task's `pnpm run` would -# re-check the workspace and warn about each other platform's package. -node_modules/.bin/turbo run build --filter=@zitadel/cloud - -launcher=apps/cloud/dist/nextgen-launcher -out="${VERCEL_OUTPUT_FILE:-$launcher}" -if [ "$out" != "$launcher" ]; then - mkdir -p "$(dirname "$out")" - cp "$launcher" "$out" -fi - -"$out" migrate diff --git a/apps/cloud/deploy.sh b/apps/cloud/deploy.sh new file mode 100644 index 0000000000..b9dcad619a --- /dev/null +++ b/apps/cloud/deploy.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# The `deploy` task of @zitadel/cloud (apps/cloud/turbo.json), which the +# `server` service's build command runs (repo-root vercel.json, Go preset) +# once turbo has the launcher (`build`, build-launcher.sh): hand the binary +# to Vercel's Go builder, then migrate the deployment's database (a +# preview's is created first; see apps/cloud/launcher/database.go). +# +# Migrations run here, before the deployment goes live, never when a +# function starts, and never from turbo's cache: the task is uncached. A +# failed migration fails the build and the previous deployment keeps +# serving, so every migration must leave the schema readable by the +# deployment still serving (expand/contract). +# +# Output: $VERCEL_OUTPUT_FILE on Vercel; locally the binary stays in +# apps/cloud/dist/nextgen-launcher. +set -eu +cd "$(dirname "$0")/../.." + +launcher=apps/cloud/dist/nextgen-launcher +out="${VERCEL_OUTPUT_FILE:-$launcher}" +if [ "$out" != "$launcher" ]; then + mkdir -p "$(dirname "$out")" + cp "$launcher" "$out" +fi + +"$out" migrate diff --git a/apps/cloud/launcher/main.go b/apps/cloud/launcher/main.go index 4dcaac26cc..cb8f71f63c 100644 --- a/apps/cloud/launcher/main.go +++ b/apps/cloud/launcher/main.go @@ -24,7 +24,7 @@ // JSON and the public base is the deployment's URL unless a NEXTGEN_* // variable says otherwise. // -// A deployment migrates at deploy time, never at startup: build.sh calls +// A deployment migrates at deploy time, never at startup: deploy.sh calls // `launcher migrate` after compiling, which creates a preview's database when // it is missing and runs the server's migrate command against the direct // URL. The serving invocation refuses --migrate, so a function cannot change diff --git a/apps/cloud/package.json b/apps/cloud/package.json index 47db26c584..b9c4f6c633 100644 --- a/apps/cloud/package.json +++ b/apps/cloud/package.json @@ -6,6 +6,7 @@ "type": "module", "scripts": { "build": "sh build-launcher.sh", + "deploy": "sh deploy.sh", "schemas": "node ../../scripts/render-builtin-schemas.mjs", "dev": "node ../../scripts/run-cloud.mjs" } diff --git a/apps/cloud/turbo.json b/apps/cloud/turbo.json index 5648f9f70d..cfe4dcf22c 100644 --- a/apps/cloud/turbo.json +++ b/apps/cloud/turbo.json @@ -7,8 +7,8 @@ // in @zitadel/server#test (`go test ./...`). "tasks": { // The `server` service's function binary (build-launcher.sh), which - // build.sh builds through turbo and then migrates with; the migration is - // never cached. Its inputs are the Go files and what they embed + // `deploy` hands to Vercel and migrates with. Its inputs are the Go files + // and what they embed // (//go:embed), as in apps/server/moon.yml's serverInputs, and the // server's version, which it stamps in. The OpenAPI YAML reaches the // binary only through the generated Go code. Turbo hashes the ignored @@ -44,6 +44,19 @@ ], "outputs": ["dist/nextgen-launcher"] }, + // The `server` service's build command (repo-root vercel.json) installs, + // as the Go preset runs no installCommand, and runs this: deploy.sh + // copies the binary to $VERCEL_OUTPUT_FILE, where the preset takes the + // function from, then migrates the deployment's database. Never cached: + // it has to migrate on every deployment. It sees the whole environment: + // the deployment's configuration is the environment, and the launcher + // checks every variable the function needs to start (a missing one fails + // the build), which strict mode would hide unless listed here. + "deploy": { + "cache": false, + "dependsOn": ["build"], + "passThroughEnv": ["*"] + }, // The `schemas` service's static files: the built-in schemas the server // embeds, rendered as the documents their identifiers name // (scripts/render-builtin-schemas.mjs). Node only: on Vercel the diff --git a/docs/design/build/turborepo-migration.md b/docs/design/build/turborepo-migration.md index 2696daaee1..7195c1fde2 100644 --- a/docs/design/build/turborepo-migration.md +++ b/docs/design/build/turborepo-migration.md @@ -196,7 +196,7 @@ as they were. | release | `changesets`, `check-graph`, `build-public-packages`, `version`, `pack`, `snapshot`, `publish` | `@zitadel/release#…` | **new** `tools/release/package.json` (`cd ../.. && node scripts/release.mjs …`) | | workspace | `test`, `check-typecheck`, `check-openapi-rules`, `check-moon-graph`, `check-adrs` | `//#…` | root scripts (**new** for the four checks), explicit inputs as in moon.yml | | workspace | `doctor`, `server`, `server-debug`, `build-local-image`, `smoke-container`, `check` | `//#…` | root scripts (**new** `server-debug`, `build-local-image`, `smoke-container`); `journey` moved to `@zitadel/e2e#journey`, and moon keeps `workspace:journey` as a deprecated alias | -| workspace | `cloud` (persistent) | `@zitadel/cloud#dev` | **new** `apps/cloud/package.json`, no moon project; its `build` (the cloud's launcher) and `schemas` are what the cloud's `server` and `schemas` services build | +| workspace | `cloud` (persistent) | `@zitadel/cloud#dev` | **new** `apps/cloud/package.json`, no moon project; its `deploy` (the cloud's launcher, then the migration) and `schemas` are what the cloud's `server` and `schemas` services run | | workspace | `cli` (interactive) | — | not a turbo task: interactive tasks need turbo's terminal UI. `pnpm run cli -- …` (the root script that exists today) | | cli-skill-e2e (no moon project) | — | — | its `test` script is renamed `eval` (`eval:fresh` for `FRESH=1`), otherwise `turbo run test` would run a Claude-driven eval | @@ -382,8 +382,8 @@ token. names that command. - **Vercel:** every service of the repo-root `vercel.json` builds through turbo: the console, the docs and the website with `turbo run build`, the - server (`apps/cloud/build.sh`) and the schemas through `@zitadel/cloud`'s - `build` and `schemas`. `apps/console/vercel.json` belongs to the paused + server and the schemas through `@zitadel/cloud`'s `deploy` (its `build`, + then the migration) and `schemas`. `apps/console/vercel.json` belongs to the paused legacy `nextgen` project and still says `moon run console:build`; it is deleted or updated with that project. - **Dockerfile:** the comment "Consumed by Moon release tasks" is text only. diff --git a/docs/design/build/turborepo.md b/docs/design/build/turborepo.md index 419e11a441..90cbb645ff 100644 --- a/docs/design/build/turborepo.md +++ b/docs/design/build/turborepo.md @@ -173,9 +173,11 @@ Vercel (`TURBO_TELEMETRY_DISABLED=1`); set it locally too. On Vercel, every service of the hosted cloud builds through turbo (repo-root `vercel.json`): the console, the docs and the website with -`turbo run build`, the server with `@zitadel/cloud`'s `build` -(`apps/cloud/build.sh`, which then migrates outside turbo) and the schemas -with its `schemas`. Two project variables, `TURBO_TELEMETRY_DISABLED` +`turbo run build`, the server with `@zitadel/cloud`'s `deploy`, which +migrates after its `build` and is never cached, and the schemas with its +`schemas`. `deploy` passes the whole environment through +(`passThroughEnv: ["*"]`): the launcher checks every variable the function +needs, and strict mode would hide one nobody listed. Two project variables, `TURBO_TELEMETRY_DISABLED` and `TURBO_PLATFORM_ENV_DISABLED`, turn off telemetry and turbo's warning about each project variable `turbo.json` does not pass ([runbook](../../runbooks/cloud.md)). The cache works as Vercel sets it up: diff --git a/docs/runbooks/cloud.md b/docs/runbooks/cloud.md index 187692e14b..78eb3cbed3 100644 --- a/docs/runbooks/cloud.md +++ b/docs/runbooks/cloud.md @@ -26,7 +26,7 @@ One Vercel deployment, five [services](https://vercel.com/docs/services): | Service | Built by | Serves | |---|---|---| -| `server` | Go preset; `apps/cloud/build.sh` builds `apps/cloud/launcher` without the console (`-tags noconsole`) through turbo, `@zitadel/cloud`'s `build` task, then migrates | every path except the console's, the docs', the website's and the Mixpanel proxy's, including `/console/runtime.json` | +| `server` | Go preset; the build command installs (the preset runs no `installCommand`) and runs `turbo run deploy --filter=@zitadel/cloud`: `@zitadel/cloud`'s `build` compiles `apps/cloud/launcher` without the console (`-tags noconsole`), and the uncached `deploy` hands it to the preset and migrates | every path except the console's, the docs', the website's and the Mixpanel proxy's, including `/console/runtime.json` | | `console` | Vite; `turbo run build` with `CONSOLE_BASE_PATH=/console/` and `CONSOLE_VERCEL_INSIGHTS=1`, so it reports Vercel Web Analytics and Speed Insights; the service serves the build where it lands, `internal/staticui/console/dist` | `/console/*` (bare `/console` redirects to the slash form; `/ui/console` and `/ui/console/*` redirect here with a 307) | | `docs` | Next.js; `turbo run build --filter=@zitadel/docs`, after `@zitadel/components`, `@zitadel/api-mock` and the `@zitadel/api` client the mock calls, which the live examples render, and the docs' typecheck. The build's cache key holds the deployment's ID, which Skew Protection bakes into it, so it never comes from the cache; never moon, which cannot archive the symlinks Vercel's Next.js adapter writes | `/docs` and `/docs/*`: pages, assets (`/docs/_next/`), search, `llms.txt`, each page's `.md` and the MCP server | | `website` | Next.js; `turbo run build --filter=@zitadel/website`, after the atoms of `@zitadel/components` its showcase renders; keyed by the deployment's ID, as the docs' | `/`, the front page, `/changelog` (`/changelog/` redirects there) with its entries' pages and its feed under it, `/changelog/*`, and their assets at `/_next/*`; the host's `/llms.txt`; the host's favicons at `/favicon.ico` and `/favicons/*`, which the console, the login UI and the docs link | @@ -417,8 +417,8 @@ not use: - The `server` service's `entrypoint` names `apps/cloud/launcher`. In dev, Vercel's Go builder builds that package and starts it on `$PORT`; a - deployment runs `build.sh`, which compiles the same package, and the - builder takes the build command's binary instead. Without + deployment's build command compiles the same package through turbo, and + the builder takes that binary instead. Without `CLOUD_DATABASE_URL` and outside a deployment, the launcher serves from SQLite and migrates it at startup. - The `console` service's `devCommand` builds it and serves the build behind diff --git a/scripts/run-cloud.mjs b/scripts/run-cloud.mjs index 20e7ff2327..da603a9e31 100644 --- a/scripts/run-cloud.mjs +++ b/scripts/run-cloud.mjs @@ -9,8 +9,9 @@ * not use: * * - server: `entrypoint` names apps/cloud/launcher. Vercel's Go builder builds - * it in dev and starts it on $PORT; a deployment runs build.sh instead, - * which builds the same package through turbo. The launcher, without + * it in dev and starts it on $PORT; a deployment's build runs + * @zitadel/cloud's `deploy` instead, which compiles the same package + * through turbo. The launcher, without * CLOUD_DATABASE_URL, serves from SQLite and migrates it at startup. * - console: its `devCommand` builds it and serves the build, as the * deployment serves it, behind the same service rewrites. Its dev server diff --git a/turbo.json b/turbo.json index 67e139580a..ddcaa38313 100644 --- a/turbo.json +++ b/turbo.json @@ -5,7 +5,7 @@ // here, the rest in each package's turbo.json ("extends": ["//"]), which // mirrors that package's moon.yml edge for edge. Both runners run the same // package.json scripts. Moon stays the runner of record (CI, local docs) - // until the cutover; the hosted cloud (vercel.json, apps/cloud/build.sh) + // until the cutover; the hosted cloud (vercel.json, apps/cloud) // builds with turbo since the pilot. // // Without this, turbo writes its own block into the root AGENTS.md whenever diff --git a/vercel.json b/vercel.json index 34bbbad87a..fe7b288337 100644 --- a/vercel.json +++ b/vercel.json @@ -5,7 +5,7 @@ "root": ".", "framework": "go", "entrypoint": "apps/cloud/launcher/main.go", - "buildCommand": "sh apps/cloud/build.sh", + "buildCommand": "pnpm install --os=current --cpu=current --optimistic-repeat-install && node_modules/.bin/turbo run deploy --filter=@zitadel/cloud", "functions": { "apps/cloud/launcher/main.go": { "regions": ["fra1"] From 6b9005d7d8e19bb4d7c82c2999879a845ddd3049 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Oct 2026 20:12:28 -0700 Subject: [PATCH 5/9] build(deploy): order the cloud's build, migrate and deploy as turbo tasks Split the uncached `deploy` task in two, ordered with dependsOn: `migrate` (after `build`; the launcher's migration, with the whole environment passed through) and `deploy` (after `migrate`; deploy.sh now only copies the binary to $VERCEL_OUTPUT_FILE, and only once the migration applied). The server's build command still installs first: Vercel's Go builder runs no installCommand, and turbo comes with the install. The docs' generate and typecheck drop every NEXT_* variable from their keys, not only NEXT_DEPLOYMENT_ID: the docs service's builds also hash Vercel's NEXT_PUBLIC_VERCEL_* variables, and the second preview missed both tasks with their inputs unchanged. Co-Authored-By: Claude Opus 5.5 --- apps/cloud/README.md | 8 +++--- apps/cloud/build-launcher.sh | 7 ++--- apps/cloud/deploy.sh | 34 +++++++++--------------- apps/cloud/launcher/main.go | 2 +- apps/cloud/package.json | 1 + apps/cloud/turbo.json | 33 +++++++++++++++-------- apps/docs/turbo.json | 12 +++++---- docs/design/build/turborepo-migration.md | 6 ++--- docs/design/build/turborepo.md | 17 +++++++----- docs/runbooks/cloud.md | 2 +- 10 files changed, 65 insertions(+), 57 deletions(-) diff --git a/apps/cloud/README.md b/apps/cloud/README.md index 360d3b227a..04a912a96d 100644 --- a/apps/cloud/README.md +++ b/apps/cloud/README.md @@ -51,9 +51,9 @@ bakes the deployment's ID into a Next.js build, and their keys hold it. This folder is the package `@zitadel/cloud`, whose tasks ([`turbo.json`](turbo.json)) build the `server` and `schemas` services. The `server` service's build command installs, since the Go preset runs no -`installCommand`, and runs `deploy`: turbo builds the launcher (`build`), -then `deploy` hands it to Vercel and migrates, never from the cache and -with the deployment's whole environment. The `schemas` service +`installCommand`, and runs `deploy`, which turbo orders after `build` (the +launcher) and `migrate`. The last two are never cached, and `migrate` sees +the deployment's whole environment. The `schemas` service runs `schemas` and is rooted here, where `package.json` has no dependencies, so Vercel detects no framework (a service takes no `framework: null`); a framework dependency in it would change how that @@ -64,7 +64,7 @@ tests run in the server's `test`. |---|---| | [`launcher/`](launcher/) | the function binary: maps `CLOUD_DATABASE_URL` / `CLOUD_MIGRATOR_DATABASE_URL`, the master key, Vercel's `PORT` and its `x-vercel-id` request ID onto the server's configuration; at build time it creates a preview's database, checks the function's configuration and migrates | | [`build-launcher.sh`](build-launcher.sh) | the `build` task: compiles the launcher, without the console, into `dist/nextgen-launcher`; turbo keys it by the branch it stamps in, which picks a preview's database | -| [`deploy.sh`](deploy.sh) | the `deploy` task, the `server` service's build after `build`: copies the launcher to `$VERCEL_OUTPUT_FILE`, then migrates | +| [`deploy.sh`](deploy.sh) | the `deploy` task, last in the `server` service's build: copies the migrated launcher to `$VERCEL_OUTPUT_FILE` | | [`setup-secrets.sh`](setup-secrets.sh) | mints an environment's master key and root admin (with [`admin-document.mjs`](admin-document.mjs)) and sets them on the Vercel project; the originals go to 1Password | `pnpm run cloud` (turbo: this package's `dev`) runs the same services on diff --git a/apps/cloud/build-launcher.sh b/apps/cloud/build-launcher.sh index 18396084a6..44f2dc3f13 100644 --- a/apps/cloud/build-launcher.sh +++ b/apps/cloud/build-launcher.sh @@ -2,7 +2,7 @@ # The `build` task of @zitadel/cloud (apps/cloud/turbo.json): compile the # launcher into the `server` service's function binary, # apps/cloud/dist/nextgen-launcher. The service's build command runs it -# through turbo as a dependency of `deploy` (deploy.sh), which migrates. +# through turbo, before `migrate` and `deploy`. # # The console is not embedded: it is a service of its own, and the launcher # turns the server's console off. The services build one after another in @@ -36,8 +36,9 @@ echo "built $out (version ${version}+cloud, commit ${commit})" # carries the cache from build to build, so every rebuild's packages would # pile up towards the build cache's limit. On Vercel, drop what no build # used in the last two hours (Go refreshes an entry's time at most hourly). -# Here rather than in deploy.sh: when turbo restores the binary, Go has -# refreshed nothing, and the prune would empty the cache. +# Here rather than in a task that runs on every deployment: when turbo +# restores the binary, Go has refreshed nothing, and the prune would empty +# the cache. if [ -n "${VERCEL:-}" ]; then find -H "$(go env GOCACHE)" -mindepth 2 -type f -mmin +120 -delete fi diff --git a/apps/cloud/deploy.sh b/apps/cloud/deploy.sh index b9dcad619a..e16b413042 100644 --- a/apps/cloud/deploy.sh +++ b/apps/cloud/deploy.sh @@ -1,26 +1,16 @@ #!/bin/sh -# The `deploy` task of @zitadel/cloud (apps/cloud/turbo.json), which the -# `server` service's build command runs (repo-root vercel.json, Go preset) -# once turbo has the launcher (`build`, build-launcher.sh): hand the binary -# to Vercel's Go builder, then migrate the deployment's database (a -# preview's is created first; see apps/cloud/launcher/database.go). -# -# Migrations run here, before the deployment goes live, never when a -# function starts, and never from turbo's cache: the task is uncached. A -# failed migration fails the build and the previous deployment keeps -# serving, so every migration must leave the schema readable by the -# deployment still serving (expand/contract). -# -# Output: $VERCEL_OUTPUT_FILE on Vercel; locally the binary stays in -# apps/cloud/dist/nextgen-launcher. +# The `deploy` task of @zitadel/cloud (apps/cloud/turbo.json), the last of +# the `server` service's build (repo-root vercel.json, Go preset), after +# `migrate`: hand the launcher to Vercel's Go builder, which takes the +# function from $VERCEL_OUTPUT_FILE, a path outside the repository. Outside +# a Vercel build there is nothing to hand over. set -eu -cd "$(dirname "$0")/../.." +cd "$(dirname "$0")" -launcher=apps/cloud/dist/nextgen-launcher -out="${VERCEL_OUTPUT_FILE:-$launcher}" -if [ "$out" != "$launcher" ]; then - mkdir -p "$(dirname "$out")" - cp "$launcher" "$out" +if [ -z "${VERCEL_OUTPUT_FILE:-}" ]; then + echo "no VERCEL_OUTPUT_FILE: the launcher stays in apps/cloud/dist/nextgen-launcher" + exit 0 fi - -"$out" migrate +mkdir -p "$(dirname "$VERCEL_OUTPUT_FILE")" +cp dist/nextgen-launcher "$VERCEL_OUTPUT_FILE" +echo "handed dist/nextgen-launcher to the Go builder" diff --git a/apps/cloud/launcher/main.go b/apps/cloud/launcher/main.go index cb8f71f63c..8c23551883 100644 --- a/apps/cloud/launcher/main.go +++ b/apps/cloud/launcher/main.go @@ -24,7 +24,7 @@ // JSON and the public base is the deployment's URL unless a NEXTGEN_* // variable says otherwise. // -// A deployment migrates at deploy time, never at startup: deploy.sh calls +// A deployment migrates at deploy time, never at startup: a turbo task runs // `launcher migrate` after compiling, which creates a preview's database when // it is missing and runs the server's migrate command against the direct // URL. The serving invocation refuses --migrate, so a function cannot change diff --git a/apps/cloud/package.json b/apps/cloud/package.json index b9c4f6c633..11300583b6 100644 --- a/apps/cloud/package.json +++ b/apps/cloud/package.json @@ -6,6 +6,7 @@ "type": "module", "scripts": { "build": "sh build-launcher.sh", + "migrate": "cd ../.. && apps/cloud/dist/nextgen-launcher migrate", "deploy": "sh deploy.sh", "schemas": "node ../../scripts/render-builtin-schemas.mjs", "dev": "node ../../scripts/run-cloud.mjs" diff --git a/apps/cloud/turbo.json b/apps/cloud/turbo.json index cfe4dcf22c..d5cdad62fa 100644 --- a/apps/cloud/turbo.json +++ b/apps/cloud/turbo.json @@ -7,8 +7,8 @@ // in @zitadel/server#test (`go test ./...`). "tasks": { // The `server` service's function binary (build-launcher.sh), which - // `deploy` hands to Vercel and migrates with. Its inputs are the Go files - // and what they embed + // `migrate` runs and `deploy` hands to Vercel. Its inputs are the Go + // files and what they embed // (//go:embed), as in apps/server/moon.yml's serverInputs, and the // server's version, which it stamps in. The OpenAPI YAML reaches the // binary only through the generated Go code. Turbo hashes the ignored @@ -44,19 +44,30 @@ ], "outputs": ["dist/nextgen-launcher"] }, - // The `server` service's build command (repo-root vercel.json) installs, - // as the Go preset runs no installCommand, and runs this: deploy.sh - // copies the binary to $VERCEL_OUTPUT_FILE, where the preset takes the - // function from, then migrates the deployment's database. Never cached: - // it has to migrate on every deployment. It sees the whole environment: - // the deployment's configuration is the environment, and the launcher - // checks every variable the function needs to start (a missing one fails - // the build), which strict mode would hide unless listed here. - "deploy": { + // The deployment's database: `launcher migrate` creates a preview's when + // it is missing (launcher/database.go) and migrates it. Migrations run + // here, before the deployment goes live, never when a function starts, + // and never from the cache. A failed migration fails the build and the + // previous deployment keeps serving, so every migration must leave the + // schema readable by the deployment still serving (expand/contract). + // + // It sees the whole environment: the deployment's configuration is the + // environment, and the launcher also checks every variable the function + // needs to start (a missing one fails the build), which strict mode would + // hide unless listed here. + "migrate": { "cache": false, "dependsOn": ["build"], "passThroughEnv": ["*"] }, + // The last step of the `server` service's build, whose command installs + // first, as Vercel's Go builder runs no installCommand, and then runs + // this task: deploy.sh copies the migrated launcher to + // $VERCEL_OUTPUT_FILE, where the builder takes the function from. + "deploy": { + "cache": false, + "dependsOn": ["migrate"] + }, // The `schemas` service's static files: the built-in schemas the server // embeds, rendered as the documents their identifiers name // (scripts/render-builtin-schemas.mjs). Node only: on Vercel the diff --git a/apps/docs/turbo.json b/apps/docs/turbo.json index 31cc0b4733..71589b077b 100644 --- a/apps/docs/turbo.json +++ b/apps/docs/turbo.json @@ -9,11 +9,13 @@ // CLI's release notes, which Changesets writes on every release. // // Turbo hashes every NEXT_* variable into the tasks of a Next.js package, - // so on Vercel each deployment's NEXT_DEPLOYMENT_ID (see build) would key - // these too; nothing here depends on it, so generate and typecheck drop - // it and still hit the cache there. + // and on Vercel each deployment brings its own: NEXT_DEPLOYMENT_ID (see + // build), and Vercel's own variables under NEXT_PUBLIC_VERCEL_ (the + // deployment's URL, its commit), which the docs service's builds hash + // too: a preview missed both tasks with their inputs unchanged. Neither + // reads one, so both drop them all and hit the cache there. "generate": { - "env": ["!NEXT_DEPLOYMENT_ID"], + "env": ["!NEXT_*"], "inputs": [ "$TURBO_DEFAULT$", "$TURBO_ROOT$/api/openapi/**/*.yaml", @@ -36,7 +38,7 @@ // The live examples (components/examples) import the components, the // mock and the client the mock calls, each from its dist/. "typecheck": { - "env": ["!NEXT_DEPLOYMENT_ID"], + "env": ["!NEXT_*"], "dependsOn": [ "generate", "@zitadel/api#generate", diff --git a/docs/design/build/turborepo-migration.md b/docs/design/build/turborepo-migration.md index 7195c1fde2..794632d2ce 100644 --- a/docs/design/build/turborepo-migration.md +++ b/docs/design/build/turborepo-migration.md @@ -196,7 +196,7 @@ as they were. | release | `changesets`, `check-graph`, `build-public-packages`, `version`, `pack`, `snapshot`, `publish` | `@zitadel/release#…` | **new** `tools/release/package.json` (`cd ../.. && node scripts/release.mjs …`) | | workspace | `test`, `check-typecheck`, `check-openapi-rules`, `check-moon-graph`, `check-adrs` | `//#…` | root scripts (**new** for the four checks), explicit inputs as in moon.yml | | workspace | `doctor`, `server`, `server-debug`, `build-local-image`, `smoke-container`, `check` | `//#…` | root scripts (**new** `server-debug`, `build-local-image`, `smoke-container`); `journey` moved to `@zitadel/e2e#journey`, and moon keeps `workspace:journey` as a deprecated alias | -| workspace | `cloud` (persistent) | `@zitadel/cloud#dev` | **new** `apps/cloud/package.json`, no moon project; its `deploy` (the cloud's launcher, then the migration) and `schemas` are what the cloud's `server` and `schemas` services run | +| workspace | `cloud` (persistent) | `@zitadel/cloud#dev` | **new** `apps/cloud/package.json`, no moon project; its `build`, `migrate` and `deploy` (in that order) and `schemas` are what the cloud's `server` and `schemas` services run | | workspace | `cli` (interactive) | — | not a turbo task: interactive tasks need turbo's terminal UI. `pnpm run cli -- …` (the root script that exists today) | | cli-skill-e2e (no moon project) | — | — | its `test` script is renamed `eval` (`eval:fresh` for `FRESH=1`), otherwise `turbo run test` would run a Claude-driven eval | @@ -382,8 +382,8 @@ token. names that command. - **Vercel:** every service of the repo-root `vercel.json` builds through turbo: the console, the docs and the website with `turbo run build`, the - server and the schemas through `@zitadel/cloud`'s `deploy` (its `build`, - then the migration) and `schemas`. `apps/console/vercel.json` belongs to the paused + server and the schemas through `@zitadel/cloud`'s `deploy` (after its + `build` and `migrate`) and `schemas`. `apps/console/vercel.json` belongs to the paused legacy `nextgen` project and still says `moon run console:build`; it is deleted or updated with that project. - **Dockerfile:** the comment "Consumed by Moon release tasks" is text only. diff --git a/docs/design/build/turborepo.md b/docs/design/build/turborepo.md index 90cbb645ff..c1bcea26c9 100644 --- a/docs/design/build/turborepo.md +++ b/docs/design/build/turborepo.md @@ -173,9 +173,11 @@ Vercel (`TURBO_TELEMETRY_DISABLED=1`); set it locally too. On Vercel, every service of the hosted cloud builds through turbo (repo-root `vercel.json`): the console, the docs and the website with -`turbo run build`, the server with `@zitadel/cloud`'s `deploy`, which -migrates after its `build` and is never cached, and the schemas with its -`schemas`. `deploy` passes the whole environment through +`turbo run build`, the server with `@zitadel/cloud`'s `build`, `migrate` +and `deploy`, in that order (`dependsOn`), the last two never cached, and +the schemas with its `schemas`. The server's build command installs before +it runs turbo: Vercel's Go builder runs no `installCommand`, and turbo comes +with the install. `migrate` passes the whole environment through (`passThroughEnv: ["*"]`): the launcher checks every variable the function needs, and strict mode would hide one nobody listed. Two project variables, `TURBO_TELEMETRY_DISABLED` and `TURBO_PLATFORM_ENV_DISABLED`, turn off telemetry and turbo's warning @@ -186,7 +188,8 @@ previews in separate entries, and keeps no local cache. Writing takes the team's token, which a fork's pull request never gets unapproved. The docs' and the website's builds never hit there: a Next.js build bakes in its deployment (Skew Protection's ID in its asset URLs, Vercel's URLs in its -metadata), so their keys hold `VERCEL_DEPLOYMENT_ID`. Turbo infers -`NEXT_DEPLOYMENT_ID` into every task of a Next.js package, so the docs' -`generate` and `typecheck`, which do not depend on it, drop it -(`!NEXT_DEPLOYMENT_ID`) and still hit. +metadata), so their keys hold `VERCEL_DEPLOYMENT_ID`. Turbo infers every +`NEXT_*` variable into every task of a Next.js package, and each deployment +brings its own (`NEXT_DEPLOYMENT_ID`, and Vercel's `NEXT_PUBLIC_VERCEL_*`, +which these services' builds hash too), so the docs' `generate` and +`typecheck`, which read none, drop them (`!NEXT_*`) and still hit. diff --git a/docs/runbooks/cloud.md b/docs/runbooks/cloud.md index 78eb3cbed3..a486bf9063 100644 --- a/docs/runbooks/cloud.md +++ b/docs/runbooks/cloud.md @@ -26,7 +26,7 @@ One Vercel deployment, five [services](https://vercel.com/docs/services): | Service | Built by | Serves | |---|---|---| -| `server` | Go preset; the build command installs (the preset runs no `installCommand`) and runs `turbo run deploy --filter=@zitadel/cloud`: `@zitadel/cloud`'s `build` compiles `apps/cloud/launcher` without the console (`-tags noconsole`), and the uncached `deploy` hands it to the preset and migrates | every path except the console's, the docs', the website's and the Mixpanel proxy's, including `/console/runtime.json` | +| `server` | Go preset; the build command installs (the preset runs no `installCommand`) and runs `turbo run deploy --filter=@zitadel/cloud`: `@zitadel/cloud`'s `build` compiles `apps/cloud/launcher` without the console (`-tags noconsole`), then the uncached `migrate` migrates and `deploy` hands the binary to the preset | every path except the console's, the docs', the website's and the Mixpanel proxy's, including `/console/runtime.json` | | `console` | Vite; `turbo run build` with `CONSOLE_BASE_PATH=/console/` and `CONSOLE_VERCEL_INSIGHTS=1`, so it reports Vercel Web Analytics and Speed Insights; the service serves the build where it lands, `internal/staticui/console/dist` | `/console/*` (bare `/console` redirects to the slash form; `/ui/console` and `/ui/console/*` redirect here with a 307) | | `docs` | Next.js; `turbo run build --filter=@zitadel/docs`, after `@zitadel/components`, `@zitadel/api-mock` and the `@zitadel/api` client the mock calls, which the live examples render, and the docs' typecheck. The build's cache key holds the deployment's ID, which Skew Protection bakes into it, so it never comes from the cache; never moon, which cannot archive the symlinks Vercel's Next.js adapter writes | `/docs` and `/docs/*`: pages, assets (`/docs/_next/`), search, `llms.txt`, each page's `.md` and the MCP server | | `website` | Next.js; `turbo run build --filter=@zitadel/website`, after the atoms of `@zitadel/components` its showcase renders; keyed by the deployment's ID, as the docs' | `/`, the front page, `/changelog` (`/changelog/` redirects there) with its entries' pages and its feed under it, `/changelog/*`, and their assets at `/_next/*`; the host's `/llms.txt`; the host's favicons at `/favicon.ico` and `/favicons/*`, which the console, the login UI and the docs link | From bcef5e40c192bddc4f6b47b75a78c2f1683cfb7b Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Oct 2026 20:17:16 -0700 Subject: [PATCH 6/9] build(deploy): never take the cloud's launcher from turbo's cache The launcher runs with the deployment's secrets (`migrate`) and becomes its function, and branch builds (previews, CI over OIDC) can write the team's remote cache, whose keys leave out PATH: an entry planted under production's key would have run in production. @zitadel/cloud#build is now uncached; Go's own build cache, which Vercel keeps per branch, keeps the compile short. Its inputs still decide when it is affected, and GOFLAGS, GOOS and GOARCH (which Vercel's Go builder sets for the function's architecture) pass through. Co-Authored-By: Claude Opus 5.5 --- apps/cloud/README.md | 7 ++++--- apps/cloud/build-launcher.sh | 6 +----- apps/cloud/turbo.json | 24 +++++++++++++----------- docs/design/build/turborepo-migration.md | 4 +++- docs/design/build/turborepo.md | 10 +++++----- docs/runbooks/cloud.md | 11 +++++------ 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/apps/cloud/README.md b/apps/cloud/README.md index 04a912a96d..811e626af3 100644 --- a/apps/cloud/README.md +++ b/apps/cloud/README.md @@ -52,8 +52,9 @@ This folder is the package `@zitadel/cloud`, whose tasks ([`turbo.json`](turbo.json)) build the `server` and `schemas` services. The `server` service's build command installs, since the Go preset runs no `installCommand`, and runs `deploy`, which turbo orders after `build` (the -launcher) and `migrate`. The last two are never cached, and `migrate` sees -the deployment's whole environment. The `schemas` service +launcher) and `migrate`. None of the three is cached: the launcher runs with +the deployment's secrets, and any branch's build can write the team's cache. +`migrate` sees the deployment's whole environment. The `schemas` service runs `schemas` and is rooted here, where `package.json` has no dependencies, so Vercel detects no framework (a service takes no `framework: null`); a framework dependency in it would change how that @@ -63,7 +64,7 @@ tests run in the server's `test`. | File | Role | |---|---| | [`launcher/`](launcher/) | the function binary: maps `CLOUD_DATABASE_URL` / `CLOUD_MIGRATOR_DATABASE_URL`, the master key, Vercel's `PORT` and its `x-vercel-id` request ID onto the server's configuration; at build time it creates a preview's database, checks the function's configuration and migrates | -| [`build-launcher.sh`](build-launcher.sh) | the `build` task: compiles the launcher, without the console, into `dist/nextgen-launcher`; turbo keys it by the branch it stamps in, which picks a preview's database | +| [`build-launcher.sh`](build-launcher.sh) | the `build` task: compiles the launcher, without the console, into `dist/nextgen-launcher`; it stamps in the branch, which picks a preview's database | | [`deploy.sh`](deploy.sh) | the `deploy` task, last in the `server` service's build: copies the migrated launcher to `$VERCEL_OUTPUT_FILE` | | [`setup-secrets.sh`](setup-secrets.sh) | mints an environment's master key and root admin (with [`admin-document.mjs`](admin-document.mjs)) and sets them on the Vercel project; the originals go to 1Password | diff --git a/apps/cloud/build-launcher.sh b/apps/cloud/build-launcher.sh index 44f2dc3f13..10cbd9451b 100644 --- a/apps/cloud/build-launcher.sh +++ b/apps/cloud/build-launcher.sh @@ -16,8 +16,7 @@ cd "$(dirname "$0")/../.." version="$(node -p 'require("./apps/server/package.json").version')" commit="${VERCEL_GIT_COMMIT_SHA:-$(git rev-parse HEAD 2>/dev/null || echo unknown)}" # The branch picks a preview's database; stamped in so the serving function -# knows it even where Vercel exposes no Git metadata at runtime. turbo.json -# hashes it, so a cached binary never serves another branch. +# knows it even where Vercel exposes no Git metadata at runtime. ref="${VERCEL_GIT_COMMIT_REF:-}" date="$(date -u +%Y-%m-%dT%H:%M:%SZ)" out=apps/cloud/dist/nextgen-launcher @@ -36,9 +35,6 @@ echo "built $out (version ${version}+cloud, commit ${commit})" # carries the cache from build to build, so every rebuild's packages would # pile up towards the build cache's limit. On Vercel, drop what no build # used in the last two hours (Go refreshes an entry's time at most hourly). -# Here rather than in a task that runs on every deployment: when turbo -# restores the binary, Go has refreshed nothing, and the prune would empty -# the cache. if [ -n "${VERCEL:-}" ]; then find -H "$(go env GOCACHE)" -mindepth 2 -type f -mmin +120 -delete fi diff --git a/apps/cloud/turbo.json b/apps/cloud/turbo.json index d5cdad62fa..09b0df9787 100644 --- a/apps/cloud/turbo.json +++ b/apps/cloud/turbo.json @@ -7,22 +7,24 @@ // in @zitadel/server#test (`go test ./...`). "tasks": { // The `server` service's function binary (build-launcher.sh), which - // `migrate` runs and `deploy` hands to Vercel. Its inputs are the Go - // files and what they embed - // (//go:embed), as in apps/server/moon.yml's serverInputs, and the - // server's version, which it stamps in. The OpenAPI YAML reaches the + // `migrate` runs and `deploy` hands to Vercel. Never cached: it runs with + // the deployment's secrets and becomes its function, and branch builds + // (previews, CI) can write the team's remote cache, so an entry planted + // under production's key would run in production. Go's own build cache, + // in Vercel's build cache of the branch, keeps the compile short. + // + // Its inputs still decide when it is affected: the Go files and what + // they embed (//go:embed), as in apps/server/moon.yml's serverInputs, and + // the server's version, which it stamps in. The OpenAPI YAML reaches the // binary only through the generated Go code. Turbo hashes the ignored // files a glob matches, so the trees that hold other Go code are // excluded: node_modules, agents' worktrees and, on Vercel, the Go // toolchain and module cache inside the checkout (.vercel/cache/golang). - // - // It also stamps in the branch, which picks a preview's database - // (launcher/database.go), so VERCEL_GIT_COMMIT_REF is hashed: a binary is - // never restored into another branch's deployment. The commit and date it - // stamps in are those of the build that made it (turbo passes VERCEL_* - // through unhashed). CGO_ENABLED is pinned in the script. + // Vercel's Go builder sets GOOS and GOARCH for the function's + // architecture; CGO_ENABLED is pinned in the script. "build": { - "env": ["GOFLAGS", "GOOS", "GOARCH", "VERCEL_GIT_COMMIT_REF"], + "cache": false, + "passThroughEnv": ["GOFLAGS", "GOOS", "GOARCH"], "inputs": [ "$TURBO_DEFAULT$", "$TURBO_ROOT$/**/*.go", diff --git a/docs/design/build/turborepo-migration.md b/docs/design/build/turborepo-migration.md index 794632d2ce..b7eccb3e1f 100644 --- a/docs/design/build/turborepo-migration.md +++ b/docs/design/build/turborepo-migration.md @@ -457,7 +457,9 @@ sets them up: anyone who can push a branch could plant one. Forks cannot: their pull requests get no build, and so no token, until a team member approves one (the project's Git fork protection). A planted entry is removed by - clearing the team's cache (owners only). + clearing the team's cache (owners only). The cloud's launcher, which runs + with production's secrets, is never cached for that reason + (`@zitadel/cloud#build`). - **GitHub Actions on pull requests** run the pull request's code, and the OIDC policy gives every run of this repository a token that can write (Vercel's docs describe no read-only policy; forks get no token). diff --git a/docs/design/build/turborepo.md b/docs/design/build/turborepo.md index c1bcea26c9..06ed9348e0 100644 --- a/docs/design/build/turborepo.md +++ b/docs/design/build/turborepo.md @@ -106,9 +106,8 @@ builds, and locally with `NEXTGEN_TASK_RUNNER=turbo`. leaves them out only in the Vite service's build, and the same packages would get other keys in the storybook's and the server's builds. `VERCEL_*` still reaches every task unhashed (turbo's built-in list): a - build whose output depends on one lists it in its `env` (the cloud's - launcher `VERCEL_GIT_COMMIT_REF`, the Next.js builds - `VERCEL_DEPLOYMENT_ID`). + build whose output depends on one lists it in its `env` (the Next.js + builds `VERCEL_DEPLOYMENT_ID`). - A task cannot set a variable's value. Values a lane needs (moon's task `env:` blocks) are set in the `package.json` script as defaults that a caller's own value overrides, as it overrides moon's task `env`: @@ -174,8 +173,9 @@ Vercel (`TURBO_TELEMETRY_DISABLED=1`); set it locally too. On Vercel, every service of the hosted cloud builds through turbo (repo-root `vercel.json`): the console, the docs and the website with `turbo run build`, the server with `@zitadel/cloud`'s `build`, `migrate` -and `deploy`, in that order (`dependsOn`), the last two never cached, and -the schemas with its `schemas`. The server's build command installs before +and `deploy`, in that order (`dependsOn`) and none of them cached, and the +schemas with its `schemas`. The launcher is not cached because it runs with +the deployment's secrets, and any branch's build can write the cache. The server's build command installs before it runs turbo: Vercel's Go builder runs no `installCommand`, and turbo comes with the install. `migrate` passes the whole environment through (`passThroughEnv: ["*"]`): the launcher checks every variable the function diff --git a/docs/runbooks/cloud.md b/docs/runbooks/cloud.md index a486bf9063..8465f0ee7e 100644 --- a/docs/runbooks/cloud.md +++ b/docs/runbooks/cloud.md @@ -26,7 +26,7 @@ One Vercel deployment, five [services](https://vercel.com/docs/services): | Service | Built by | Serves | |---|---|---| -| `server` | Go preset; the build command installs (the preset runs no `installCommand`) and runs `turbo run deploy --filter=@zitadel/cloud`: `@zitadel/cloud`'s `build` compiles `apps/cloud/launcher` without the console (`-tags noconsole`), then the uncached `migrate` migrates and `deploy` hands the binary to the preset | every path except the console's, the docs', the website's and the Mixpanel proxy's, including `/console/runtime.json` | +| `server` | Go preset; the build command installs (the preset runs no `installCommand`) and runs `turbo run deploy --filter=@zitadel/cloud`: `@zitadel/cloud`'s `build` compiles `apps/cloud/launcher` without the console (`-tags noconsole`), then `migrate` migrates and `deploy` hands the binary to the preset, none of them cached | every path except the console's, the docs', the website's and the Mixpanel proxy's, including `/console/runtime.json` | | `console` | Vite; `turbo run build` with `CONSOLE_BASE_PATH=/console/` and `CONSOLE_VERCEL_INSIGHTS=1`, so it reports Vercel Web Analytics and Speed Insights; the service serves the build where it lands, `internal/staticui/console/dist` | `/console/*` (bare `/console` redirects to the slash form; `/ui/console` and `/ui/console/*` redirect here with a 307) | | `docs` | Next.js; `turbo run build --filter=@zitadel/docs`, after `@zitadel/components`, `@zitadel/api-mock` and the `@zitadel/api` client the mock calls, which the live examples render, and the docs' typecheck. The build's cache key holds the deployment's ID, which Skew Protection bakes into it, so it never comes from the cache; never moon, which cannot archive the symlinks Vercel's Next.js adapter writes | `/docs` and `/docs/*`: pages, assets (`/docs/_next/`), search, `llms.txt`, each page's `.md` and the MCP server | | `website` | Next.js; `turbo run build --filter=@zitadel/website`, after the atoms of `@zitadel/components` its showcase renders; keyed by the deployment's ID, as the docs' | `/`, the front page, `/changelog` (`/changelog/` redirects there) with its entries' pages and its feed under it, `/changelog/*`, and their assets at `/_next/*`; the host's `/llms.txt`; the host's favicons at `/favicon.ico` and `/favicons/*`, which the console, the login UI and the docs link | @@ -564,10 +564,10 @@ project and imports the root admin. registers through the console's sign-in page. - **Turbo's remote cache:** every service builds through turbo and reads and writes the team's remote cache, production and previews in separate - entries: the console, the launcher (a preview's also keyed by its branch, - which picks its database), the schemas and the packages the docs and the + entries: the console, the schemas and the packages the docs and the website render. The docs' and the website's own builds never come from it: - their keys hold the deployment's ID. Each build's log shows `cache hit` or + their keys hold the deployment's ID. The launcher is never cached: it runs + with the deployment's secrets, and any branch's build can write the cache. Each build's log shows `cache hit` or `cache miss` per task, and the deployment's Run Summary shows why a task missed. A build with a result that is wrong although its inputs are right (a broken or planted @@ -598,8 +598,7 @@ project and imports the root admin. pnpm compares the lockfile with the one that `node_modules` was installed from and installs when they differ. Go would keep each rebuild's compiled packages for five days, so `build-launcher.sh` drops those no build used - in the last two hours, after it compiled: a binary turbo restores - refreshes none. A new Go version starts the Go caches over: when the + in the last two hours. A new Go version starts the Go caches over: when the toolchain Vercel picks from `go.mod`'s `toolchain` line differs from the restored one, its Go builder downloads it and deletes `.vercel/cache/golang`, module cache included. The next build of each From 226143e059c211215d0783456c716335ee5ea07c Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Oct 2026 21:27:30 -0700 Subject: [PATCH 7/9] build: route the docs' generation through the task graph, not their scripts The docs' `dev` script bundled the API reference and the CLI reference before `next dev`, the `generate` task's work, which both graphs already run first (moon's docs:dev runs `next dev` itself). It is plain `next dev` now, as the `build` script became `next build`. turborepo.md states the rule: a script does its own task's work only, and a step that is a task of its own is a dependsOn edge and a moon dep, never `a && b`. Co-Authored-By: Claude Opus 5.5 --- apps/docs/package.json | 2 +- docs/design/build/turborepo.md | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/docs/package.json b/apps/docs/package.json index f1e38d9fe0..903961e5e0 100644 --- a/apps/docs/package.json +++ b/apps/docs/package.json @@ -9,7 +9,7 @@ "type": "module", "scripts": { "generate": "node scripts/bundle-openapi.mjs && node scripts/bundle-cli.mjs", - "dev": "node scripts/bundle-openapi.mjs && node scripts/bundle-cli.mjs && next dev --port 3003", + "dev": "next dev --port 3003", "build": "next build", "start": "next start --port 3003", "typecheck": "next typegen && tsc --noEmit -p tsconfig.app.json && tsc --noEmit -p tsconfig.spec.json && node scripts/sdk-samples.ts && tsc -p tsconfig.samples.json", diff --git a/docs/design/build/turborepo.md b/docs/design/build/turborepo.md index 06ed9348e0..07390d8955 100644 --- a/docs/design/build/turborepo.md +++ b/docs/design/build/turborepo.md @@ -27,6 +27,13 @@ builds, and locally with `NEXTGEN_TASK_RUNNER=turbo`. `cd` to the directory the command needs. `@zitadel/cloud` (`apps/cloud`, the hosted cloud's launcher and schemas) has no moon project: its `dev` is moon's `workspace:cloud`. +- A script does its own task's work only. A step that is a task of its own, + such as the package's `generate` or another package's `build`, is a + `dependsOn` edge here and a `deps` entry in `moon.yml`, never `a && b` in + the script: chained, it reruns on every run, and neither runner can cache, + order or skip it. So the docs' `build` and `dev` are plain `next build` and + `next dev`. A release build that cleans and rebuilds its own `dist` + (`build-release`) is still one task's work. - Task names are script names: `dev:real`, `test:integration` and `sync:export` keep their colons. The e2e lanes are `@zitadel/e2e`'s `console-smoke`, `console-real`, `console-embedded`, `console-platform`, From 18a65daa634c9a049f7e8b2e6c8f5a8a9de3fbeb Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Oct 2026 21:30:45 -0700 Subject: [PATCH 8/9] docs: say why the cloud's server build installs and calls turbo by path Vercel's Go builder runs no installCommand and runs the build command through build-utils' execCommand with the container's PATH and Go's, while the JS builders (@vercel/next, @vercel/static-build) put every node_modules/.bin up to the repo root on PATH first. So the server's build command installs itself and calls node_modules/.bin/turbo; vercel.json takes no comments, so the runbook's layout row says it. Co-Authored-By: Claude Opus 5.5 --- docs/runbooks/cloud.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/runbooks/cloud.md b/docs/runbooks/cloud.md index 8465f0ee7e..57f341943a 100644 --- a/docs/runbooks/cloud.md +++ b/docs/runbooks/cloud.md @@ -26,7 +26,7 @@ One Vercel deployment, five [services](https://vercel.com/docs/services): | Service | Built by | Serves | |---|---|---| -| `server` | Go preset; the build command installs (the preset runs no `installCommand`) and runs `turbo run deploy --filter=@zitadel/cloud`: `@zitadel/cloud`'s `build` compiles `apps/cloud/launcher` without the console (`-tags noconsole`), then `migrate` migrates and `deploy` hands the binary to the preset, none of them cached | every path except the console's, the docs', the website's and the Mixpanel proxy's, including `/console/runtime.json` | +| `server` | Go preset; the build command installs, because Vercel's Go builder runs no `installCommand`, and then runs `node_modules/.bin/turbo run deploy --filter=@zitadel/cloud`. turbo goes by its path because, unlike the JS builders, the Go builder puts no `node_modules/.bin` on `PATH`. `@zitadel/cloud`'s `build` compiles `apps/cloud/launcher` without the console (`-tags noconsole`), then `migrate` migrates and `deploy` hands the binary to the preset, none of them cached | every path except the console's, the docs', the website's and the Mixpanel proxy's, including `/console/runtime.json` | | `console` | Vite; `turbo run build` with `CONSOLE_BASE_PATH=/console/` and `CONSOLE_VERCEL_INSIGHTS=1`, so it reports Vercel Web Analytics and Speed Insights; the service serves the build where it lands, `internal/staticui/console/dist` | `/console/*` (bare `/console` redirects to the slash form; `/ui/console` and `/ui/console/*` redirect here with a 307) | | `docs` | Next.js; `turbo run build --filter=@zitadel/docs`, after `@zitadel/components`, `@zitadel/api-mock` and the `@zitadel/api` client the mock calls, which the live examples render, and the docs' typecheck. The build's cache key holds the deployment's ID, which Skew Protection bakes into it, so it never comes from the cache; never moon, which cannot archive the symlinks Vercel's Next.js adapter writes | `/docs` and `/docs/*`: pages, assets (`/docs/_next/`), search, `llms.txt`, each page's `.md` and the MCP server | | `website` | Next.js; `turbo run build --filter=@zitadel/website`, after the atoms of `@zitadel/components` its showcase renders; keyed by the deployment's ID, as the docs' | `/`, the front page, `/changelog` (`/changelog/` redirects there) with its entries' pages and its feed under it, `/changelog/*`, and their assets at `/_next/*`; the host's `/llms.txt`; the host's favicons at `/favicon.ico` and `/favicons/*`, which the console, the login UI and the docs link | From 3cbd88e752fd4dbc6e0fbf4e7ac0d94517fdd663 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 27 Oct 2026 21:31:58 -0700 Subject: [PATCH 9/9] build(deploy): pass the whole environment to the cloud's local dev task @zitadel/cloud#dev listed CLOUD_DEV_*, NEXTGEN_* and GOFLAGS (the old //#cloud's list), so under turbo the local cloud's server never saw the OpenTelemetry SDK's OTEL_* variables, or anything else a service reads. `vercel dev` hands the environment to every service, as `pnpm run cloud` does, and the task is uncached, so it passes everything through. Co-Authored-By: Claude Opus 5.5 --- apps/cloud/turbo.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/cloud/turbo.json b/apps/cloud/turbo.json index 09b0df9787..073da7d206 100644 --- a/apps/cloud/turbo.json +++ b/apps/cloud/turbo.json @@ -86,6 +86,10 @@ // The cloud on localhost: `vercel dev` over vercel.json's services // (scripts/run-cloud.mjs), which builds what the services' dev servers // read itself. `pnpm run cloud` runs the same script without a runner. + // It sees the whole environment, as `pnpm run cloud` does: `vercel dev` + // hands it to every service (the server reads NEXTGEN_* and the + // OpenTelemetry SDK's OTEL_*, each dev server its own), and a list would + // quietly drop whatever it missed. "dev": { "inputs": [ "$TURBO_DEFAULT$", @@ -93,7 +97,7 @@ "$TURBO_ROOT$/scripts/dev-process.mjs", "$TURBO_ROOT$/scripts/task-runner.mjs" ], - "passThroughEnv": ["CLOUD_DEV_PORT", "CLOUD_DEV_DEBUG", "NEXTGEN_*", "GOFLAGS"] + "passThroughEnv": ["*"] } } }