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
21 changes: 21 additions & 0 deletions apps/web/component.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ spec:
# Its own lane per environment: the framework build is the longest job in
# the plan, and a shared lane would wait on it.
lane: isolated
# 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 its server reads the tables
# db-migrate creates 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, the key 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 project this environment talks to: its URL and anon key, in the wiring
# document the `supabase` Terraform root lease-publishes after its apply.
# Rendered into wrangler.jsonc by the build (and inlined into the bundle),
Expand Down
68 changes: 68 additions & 0 deletions infra/db-migrate/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
apiVersion: sourceplane.io/v1
kind: Component
metadata:
name: db-migrate
description: Applies the SaaS schema (packages/db's migrations) to the stage and prod Supabase projects
labels:
domain: infra
scope: database
team: platform
layer: data

spec:
type: db-migrate
lifecycle: production
owner: platform
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: sveltekit-saas

# 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://sveltekit-saas/sveltekit-saas/{{ .environment }}/SUPABASE_ACCESS_TOKEN"
SUPABASE_PROJECT_REF: "secret://sveltekit-saas/sveltekit-saas/{{ .environment }}/SUPABASE_PROJECT_REF"
SUPABASE_DB_PASSWORD: "secret://sveltekit-saas/sveltekit-saas/{{ .environment }}/SUPABASE_DB_PASSWORD"
SUPABASE_DB_URL: "secret://sveltekit-saas/sveltekit-saas/{{ .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
70 changes: 70 additions & 0 deletions infra/terraform/supabase/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
apiVersion: sourceplane.io/v1
kind: Component

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

spec:
type: terraform
lifecycle: production
owner: platform
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);
# WIRING_SUPABASE — the project URL and the publishable (anon) key, the
# document the app's build renders into its config; and the service-role
# key, which the app's server uses where upstream does (the contact form,
# Stripe customers, account deletion) and which its deploy lane — that
# environment's only — hands the Worker as a secret.
secretOutputs: "SUPABASE_PROJECT_REF=project_ref,SUPABASE_DB_PASSWORD=database_password,SUPABASE_DB_URL=connection_uri,WIRING_SUPABASE=wiring,SUPABASE_SERVICE_ROLE_KEY=service_role_key"

# 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://sveltekit-saas/sveltekit-saas/{{ .environment }}/SUPABASE_ACCESS_TOKEN"
TF_VAR_supabaseOrgId: "secret://sveltekit-saas/sveltekit-saas/{{ .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
73 changes: 73 additions & 0 deletions infra/terraform/supabase/terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions infra/terraform/supabase/terraform/adopt.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# --- Self-healing adoption (AWS-free state seeding) ---
#
# Same pattern as cloudflare-kv/terraform/adopt.tf: when the platform state
# does not yet track the Supabase project but the organization already has one
# under this name, import it by ref at plan time instead of colliding with the
# duplicate-name guard. Fresh products resolve to id = "" and create normally;
# a resource already in state short-circuits the lookup.
#
# Cutover caveat: database_password is config-driven (random_password). After
# adoption the first apply RESETS the database password to the newly generated
# one and publishes it through the job-output secrets — db-migrate, hyperdrive
# and the worker fleet re-wire from those in the same run, but anything still
# consuming the pre-adoption credentials breaks until that run converges.

data "external" "adopt_project" {
program = ["bash", "-c", <<-EOT
set -euo pipefail
name="$(jq -r .name)"
state_file="$(mktemp)"
trap 'rm -f "$state_file"' EXIT
code="$(curl -s -o "$state_file" -w '%%{http_code}' \
-u "$TF_HTTP_USERNAME:$TF_HTTP_PASSWORD" "$TF_HTTP_ADDRESS" || echo 000)"
if [ "$code" = "200" ] && jq -e \
'[.resources[]? | select(.type == "supabase_project" and .name == "this")] | length > 0' \
"$state_file" >/dev/null 2>&1; then
jq -n '{id: ""}'
exit 0
fi
resp="$(curl -fsS --retry 3 -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \
"https://api.supabase.com/v1/projects")"
# The org id reaches this job as TF_VAR_supabaseOrgId (the component maps
# the SUPABASE_ORG_ID secret to the terraform variable); plain
# SUPABASE_ORG_ID exists only in older wirings. Accept either; with
# neither set, match by name alone.
org="$${TF_VAR_supabaseOrgId:-$${SUPABASE_ORG_ID:-}}"
printf '%s' "$resp" | jq -c --arg n "$name" --arg org "$org" \
'{id: ([.[] | select(.name == $n and ($org == "" or .organization_id == $org)) | .id] | first // "")}'
EOT
]
query = {
name = local.project_name
}
}

import {
for_each = toset(compact([data.external.adopt_project.result.id]))
to = supabase_project.this
id = each.value
}
Loading
Loading