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
49 changes: 42 additions & 7 deletions apps/web/component.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,42 @@ spec:
# Its own lane per environment: the Next.js build is the longest job in the
# plan, and a shared lane would wait on it.
lane: isolated
# The Supabase project this Worker talks to arrives in SL2, from the
# `supabase` Terraform root: until then every build inlines the committed
# fixture render of the wrangler config, whose placeholder project nothing
# calls.
# INFRA BEFORE APP. The Worker's config carries its project's URL and anon
# key, rendered in the build from the wiring document the `supabase` root
# lease-publishes after its apply, and the browser reads the tables — and
# signs in through the access-token hook — that db-migrate creates and
# enables in that project. A job resolves its secrets when it STARTS, so on
# a merge to main this deploy waits for both (enforced) — one convergence
# runs supabase → db-migrate → web per environment, and the document and the
# schema are there. A pull request deploys nothing, so nothing on one waits
# (advisory).
dependsOn:
- component: supabase
mode: enforced
modeRules:
- mode: advisory
when:
triggerRef: github-pull-request
- component: db-migrate
mode: enforced
modeRules:
- mode: advisory
when:
triggerRef: github-pull-request
# The wiring documents the supabase root lease-publishes, one per deployed
# environment: the project's URL and publishable (anon) key, rendered into
# wrangler.jsonc by the build and inlined into the bundle;
# tooling/wire/live.mjs picks this job's and fails loudly when it is not
# there. OPTIONAL references, so a job resolves the ones that exist: stage's
# deploy waits on stage's chain only, and while the bootstrap runs stage and
# prod side by side, prod's document may not be published yet when stage's
# deploy starts (a required reference would fail that job). The dev verify
# lane renders nothing and reads neither. The chat needs no server secret:
# the browser talks to Supabase with the publishable key, and row-level
# security is what protects the data.
optionalSecretEnv:
WIRING_SUPABASE_STAGE: "secret://supabase-slack-clone/supabase-slack-clone/stage/WIRING_SUPABASE"
WIRING_SUPABASE_PROD: "secret://supabase-slack-clone/supabase-slack-clone/prod/WIRING_SUPABASE"
subscribe:
environments:
- name: dev
Expand Down Expand Up @@ -69,9 +101,12 @@ spec:
installCommand: pnpm install --frozen-lockfile
# wrangler.jsonc is COMMITTED — the fixture render, so the composition's
# structure check (`test -f wrangler.jsonc`, before the build, in a fresh
# checkout) passes, and `next build` inlines the project it names
# (next.config.ts).
buildCommand: pnpm exec turbo run build --filter=@app/web
# checkout) passes. Stage and prod re-render it here, in their own job
# tree, from this environment's wiring document, so the Worker's vars and
# the URL and key `next build` inlines (next.config.ts) are the real
# project's; dev verifies with the fixture and has no wiring.
# ${ORUN_ENVIRONMENT} the runner sets for every step.
buildCommand: if [ "${ORUN_ENVIRONMENT}" != dev ]; then node ../../tooling/wire/live.mjs --template wrangler.template.jsonc --out wrangler.jsonc --components supabase --env "${ORUN_ENVIRONMENT}"; fi && pnpm exec turbo run build --filter=@app/web

# Smoke: the sign-in page renders, its password field included, and /api/health
# answers ok — which only the Worker can, by asking the project's Auth with
Expand Down
7 changes: 7 additions & 0 deletions apps/web/docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ Built by the turbo pipeline from `apps/web` and deployed with wrangler.
## Variables

`NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY`, `NEXT_PUBLIC_SUPABASE_URL`.

## Wiring

Resolved at deploy time from secrets the infrastructure lanes publish (names only):

- `WIRING_SUPABASE_PROD` (optional)
- `WIRING_SUPABASE_STAGE` (optional)
3 changes: 2 additions & 1 deletion apps/web/docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ A Cloudflare Worker with Static Assets (server rendering plus static files), dep

## Depends on

- (none)
- **supabase** — Provisions Supabase projects for stage and prod, points their Auth at the chat, and wires the URL and key into orun secrets
- **db-migrate** — Applies the chat schema (packages/db's migrations) to the stage and prod Supabase projects, then switches on the access-token hook it needs

## Depended on by

Expand Down
76 changes: 76 additions & 0 deletions infra/db-migrate/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
apiVersion: sourceplane.io/v1
kind: Component
metadata:
name: db-migrate
description: Applies the chat schema (packages/db's migrations) to the stage and prod Supabase projects, then switches on the access-token hook it needs
labels:
domain: infra
scope: database
team: platform
layer: data

spec:
type: db-migrate
lifecycle: production
owner: platform
# Its pages, attached to it in the workspace's catalog: what the Docs
# library shows for it, and what the catalog's docs and runbook checks read.
# Generated from what this manifest and its sources declare.
docs:
overview: docs/overview.md
pages:
- { path: docs/architecture.md, role: architecture, title: Architecture }
- { path: docs/runbook.md, role: runbook, title: Runbook }
domain: infra

# THE SCHEMA BETWEEN THE PROJECT AND THE APP. Migrations reach the project
# the `supabase` component creates through the Management API, with the
# project ref and credentials that component lease-publishes after its
# apply — and a job resolves its secrets when it STARTS, so on a merge to
# main this waits for that environment's apply (enforced). apps/web waits on
# this the same way, so one convergence runs supabase → db-migrate → web per
# environment. A pull request applies nothing (this plans offline there), so
# nothing on one waits (advisory). `db` is the package this builds and runs;
# a change to it re-plans this.
dependsOn:
- component: db
- component: supabase
mode: enforced
modeRules:
- mode: advisory
when:
triggerRef: github-pull-request

path: packages/db/src/migrations

parameters:
nodeVersion: "22"
pnpmVersion: "10.12.1"
owner: sourceplane
repo: supabase-slack-clone

# Everything the runner needs, all resolved from orun secrets: a short-lived
# Management-API token minted fresh per run from the Supabase integration
# (management-access template), and the project ref and its database
# credentials, which the `supabase` component lease-publishes.
secretEnv:
SUPABASE_ACCESS_TOKEN: "secret://supabase-slack-clone/supabase-slack-clone/{{ .environment }}/SUPABASE_ACCESS_TOKEN"
SUPABASE_PROJECT_REF: "secret://supabase-slack-clone/supabase-slack-clone/{{ .environment }}/SUPABASE_PROJECT_REF"
SUPABASE_DB_PASSWORD: "secret://supabase-slack-clone/supabase-slack-clone/{{ .environment }}/SUPABASE_DB_PASSWORD"
SUPABASE_DB_URL: "secret://supabase-slack-clone/supabase-slack-clone/{{ .environment }}/SUPABASE_DB_URL"

subscribe:
environments:
- name: stage
profile: plan
profileRules:
- profile: apply
when:
triggerRef: github-push-main

- name: prod
profile: plan
profileRules:
- profile: apply
when:
triggerRef: github-push-main
19 changes: 19 additions & 0 deletions infra/db-migrate/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# db-migrate — architecture

Applies the migrations under `packages/db/src/migrations` in order, recording each one
it applies so a run only ever applies what is new. An already-applied migration
is never edited: a change is a new, later migration.

## Migrations

- `000_control`
- `010_init`
- `020_auth_hook`
- `030_seed`

## Resolved at run time (names only)

- `SUPABASE_ACCESS_TOKEN`
- `SUPABASE_DB_PASSWORD`
- `SUPABASE_DB_URL`
- `SUPABASE_PROJECT_REF`
20 changes: 20 additions & 0 deletions infra/db-migrate/docs/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# db-migrate

Applies the chat schema (packages/db's migrations) to the stage and prod Supabase projects, then switches on the access-token hook it needs

Applies this repository's ordered database migrations per environment: plan on pull requests, apply on merge to `main`.

| | |
| ------------ | ------------------------------------------------------------------------------------------ |
| Type | `db-migrate` |
| Source | `infra/db-migrate` |
| Environments | `stage`: `plan`, `apply` on a push to `main` · `prod`: `plan`, `apply` on a push to `main` |

## Depends on

- **db** — The schema as migrations, and the runner that applies them over the Supabase Management API (built by the db-migrate component before it plans or applies)
- **supabase** — Provisions Supabase projects for stage and prod, points their Auth at the chat, and wires the URL and key into orun secrets

## Depended on by

- **web** — The team chat (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, per environment)
19 changes: 19 additions & 0 deletions infra/db-migrate/docs/runbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# db-migrate — runbook

## How it runs

Pull requests plan it; a merge to `main` applies it to `stage`, `prod`.
CI plans what changed (`orun plan --changed`) and runs each lane with `orun run`;
the plan orders a component after everything it depends on.

## Rollback

Revert the commit on `main`. The next convergence applies the previous desired
state; nothing was changed out of band, so there is nothing else to undo.

## Common failures

- **An applied migration's file changed**: the run stops rather than guess.
Restore the file and write the change as a new migration.
- **The database is unreachable**: the infrastructure lane it depends on has
not applied for this environment; that lane runs first in a convergence.
76 changes: 76 additions & 0 deletions infra/terraform/supabase/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
apiVersion: sourceplane.io/v1
kind: Component

metadata:
name: supabase
description: Provisions Supabase projects for stage and prod, points their Auth at the chat, and wires the URL and key into orun secrets
labels:
domain: infra
scope: database

spec:
type: terraform
lifecycle: production
owner: platform
# Its pages, attached to it in the workspace's catalog: what the Docs
# library shows for it, and what the catalog's docs and runbook checks read.
# Generated from what this manifest and its sources declare.
docs:
overview: docs/overview.md
pages:
- { path: docs/architecture.md, role: architecture, title: Architecture }
- { path: docs/runbook.md, role: runbook, title: Runbook }
domain: infra

parameters:
orgName: sourceplane
stackName: supabase
terraformDir: terraform
terraformVersion: "1.15.3"
# Supabase region is surfaced to Terraform as TF_VAR_supabaseRegion by the
# terraform composition's parameter→TF_VAR loop. The org id is NOT committed
# here: it is account-specific and injected as TF_VAR_supabaseOrgId from the
# SUPABASE_ORG_ID CI secret (see .github/workflows/ci.yml). Keeping it out of
# parameters also prevents the param→TF_VAR loop from overriding the secret.
supabaseRegion: ap-southeast-1
# The app's Worker, which Auth's Site URL and redirect allow-list name:
# <repo>-web-<env> on this workers.dev subdomain — the same value
# apps/web/component.yaml carries (TF_VAR_workersDevSubdomain).
workersDevSubdomain: your-workers-subdomain
# "false": sign-up signs the user straight in, because a project without
# SMTP of its own emails only its organization's members. "true" once the
# project sends email (the product's README, "Email").
authEmailConfirmation: "false"
# SEC-JOB: post-apply, these outputs are queued on the runner's sink and
# lease-published onto the project/{{env}} rung by the runner. The
# project's three database credentials (db-migrate reads them);
# and WIRING_SUPABASE — the project URL and the publishable (anon) key,
# the document the app's build renders into its config. Nothing reads a
# service-role key, so none is published.
secretOutputs: "SUPABASE_PROJECT_REF=project_ref,SUPABASE_DB_PASSWORD=database_password,SUPABASE_DB_URL=connection_uri,WIRING_SUPABASE=wiring"

# Both values resolve from orun secrets (project rung, brokered from the
# Supabase integration): a short-lived Management-API access token minted
# fresh per run (management-access template — the provider reads
# SUPABASE_ACCESS_TOKEN from env natively), and the org id as a non-secret
# connection fact (org-id template) surfaced straight to Terraform. The
# SUPABASE_ORG_ID CI secret and its TF_VAR mapping are retired.
secretEnv:
SUPABASE_ACCESS_TOKEN: "secret://supabase-slack-clone/supabase-slack-clone/{{ .environment }}/SUPABASE_ACCESS_TOKEN"
TF_VAR_supabaseOrgId: "secret://supabase-slack-clone/supabase-slack-clone/{{ .environment }}/SUPABASE_ORG_ID"

subscribe:
environments:
- name: stage
profile: plan-only
profileRules:
- profile: apply
when:
triggerRef: github-push-main

- name: prod
profile: plan-only
profileRules:
- profile: apply
when:
triggerRef: github-push-main
34 changes: 34 additions & 0 deletions infra/terraform/supabase/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# supabase — architecture

A `terraform` root at `infra/terraform/supabase/terraform`.

- **State** lives in the Orun Cloud HTTP state backend — no local state and
no state bucket to manage.
- **Credentials are brokered per run** from the workspace's provider
connections and resolved by name; none is stored in this repository.
- **Adoption** (`adopt.tf`): when state is empty but the resource already
exists, plan-time import adopts it instead of failing.

## Resources

- `random_password.db_password`
- `supabase_project.this`
- `terraform_data.project_ready`
- `supabase_settings.this`

## Published outputs

Written as job-output secrets on each environment after an apply; deploy
lanes downstream resolve them by name.

| Secret | Terraform output |
| ---------------------- | ------------------- |
| `SUPABASE_PROJECT_REF` | `project_ref` |
| `SUPABASE_DB_PASSWORD` | `database_password` |
| `SUPABASE_DB_URL` | `connection_uri` |
| `WIRING_SUPABASE` | `wiring` |

## Resolved at run time (names only)

- `SUPABASE_ACCESS_TOKEN`
- `TF_VAR_supabaseOrgId`
20 changes: 20 additions & 0 deletions infra/terraform/supabase/docs/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# supabase

Provisions Supabase projects for stage and prod, points their Auth at the chat, and wires the URL and key into orun secrets

Terraform-managed infrastructure, planned and applied per environment by its CI lane.

| | |
| ------------ | ---------------------------------------------------------------------------------------------------- |
| Type | `terraform` |
| Source | `infra/terraform/supabase` |
| Environments | `stage`: `plan-only`, `apply` on a push to `main` · `prod`: `plan-only`, `apply` on a push to `main` |

## Depends on

- (none)

## Depended on by

- **db-migrate** — Applies the chat schema (packages/db's migrations) to the stage and prod Supabase projects, then switches on the access-token hook it needs
- **web** — The team chat (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, per environment)
20 changes: 20 additions & 0 deletions infra/terraform/supabase/docs/runbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# supabase — runbook

## How it runs

Pull requests plan it; a merge to `main` applies it to `stage`, `prod`.
CI plans what changed (`orun plan --changed`) and runs each lane with `orun run`;
the plan orders a component after everything it depends on.

## Rollback

Revert the commit on `main`. The next convergence applies the previous desired
state; nothing was changed out of band, so there is nothing else to undo.

## Common failures

- **Provider authentication fails**: the workspace's provider connection is
missing or revoked. Reconnect it in the console; credentials are brokered
from it per run.
- **"Already exists" on a first apply**: state is empty but the resource is
not. `adopt.tf` imports it at plan time; if it still fails, the resource was renamed outside Terraform.
Loading
Loading