Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cloud-turbo-package.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

---


3 changes: 3 additions & 0 deletions .moon/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
turbo's retries and `test:all` calls Vitest directly, because moon has its
Expand Down
10 changes: 6 additions & 4 deletions apps/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 26 additions & 11 deletions apps/cloud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,39 @@ 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.
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`. 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
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-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 |

`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)).

Expand Down
21 changes: 6 additions & 15 deletions apps/cloud/build.sh → apps/cloud/build-launcher.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
#!/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 `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, 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
# 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.
#
# 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).
#
# Output: $VERCEL_OUTPUT_FILE on Vercel, dist/nextgen-launcher locally.
set -eu
cd "$(dirname "$0")/../.."

Expand All @@ -26,7 +19,7 @@ commit="${VERCEL_GIT_COMMIT_SHA:-$(git rev-parse HEAD 2>/dev/null || echo unknow
# 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}"
out=apps/cloud/dist/nextgen-launcher
mkdir -p "$(dirname "$out")"

CGO_ENABLED=0 go build -trimpath -tags noconsole \
Expand All @@ -42,8 +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).
if [ -n "${VERCEL_OUTPUT_FILE:-}" ]; then
if [ -n "${VERCEL:-}" ]; then
find -H "$(go env GOCACHE)" -mindepth 2 -type f -mmin +120 -delete
fi

"$out" migrate
16 changes: 16 additions & 0 deletions apps/cloud/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh
# 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")"

if [ -z "${VERCEL_OUTPUT_FILE:-}" ]; then
echo "no VERCEL_OUTPUT_FILE: the launcher stays in apps/cloud/dist/nextgen-launcher"
exit 0
fi
mkdir -p "$(dirname "$VERCEL_OUTPUT_FILE")"
cp dist/nextgen-launcher "$VERCEL_OUTPUT_FILE"
echo "handed dist/nextgen-launcher to the Go builder"
6 changes: 3 additions & 3 deletions apps/cloud/launcher/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion apps/cloud/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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
Expand Down
4 changes: 2 additions & 2 deletions apps/cloud/launcher/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,15 @@ 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") {
if strings.Contains(line, "go build") && !strings.HasPrefix(strings.TrimSpace(line), "#") {
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")
}

Expand Down
14 changes: 14 additions & 0 deletions apps/cloud/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@zitadel/cloud",
"version": "0.0.0",
"private": true,
"license": "AGPL-3.0-only",
"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"
}
}
103 changes: 103 additions & 0 deletions apps/cloud/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"$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
// `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).
// Vercel's Go builder sets GOOS and GOARCH for the function's
// architecture; CGO_ENABLED is pinned in the script.
"build": {
"cache": false,
"passThroughEnv": ["GOFLAGS", "GOOS", "GOARCH"],
"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 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
// 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.
// 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$",
"$TURBO_ROOT$/scripts/run-cloud.mjs",
"$TURBO_ROOT$/scripts/dev-process.mjs",
"$TURBO_ROOT$/scripts/task-runner.mjs"
],
"passThroughEnv": ["*"]
}
}
}
6 changes: 4 additions & 2 deletions apps/docs/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"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",
"build": "node scripts/bundle-openapi.mjs && node scripts/bundle-cli.mjs && next build",
"dev": "next dev --port 3003",
"build": "next build",
Comment thread
fforootd marked this conversation as resolved.
Resolved
"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 .",
Expand Down
21 changes: 19 additions & 2 deletions apps/docs/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
// 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,
// 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_*"],
"inputs": [
"$TURBO_DEFAULT$",
"$TURBO_ROOT$/api/openapi/**/*.yaml",
Expand All @@ -30,6 +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_*"],
"dependsOn": [
"generate",
"@zitadel/api#generate",
Expand All @@ -39,16 +48,24 @@
],
"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.
//
// The build waits for typecheck, as in moon.yml: `next build` empties
// .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",
Expand Down
2 changes: 1 addition & 1 deletion apps/server/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading
Loading