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
22 changes: 22 additions & 0 deletions apps/api/component.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ spec:
inputs:
- packages/**
- pnpm-lock.yaml
# INFRA BEFORE APP, ENFORCED ON MAIN: the Hyperdrive config its build renders
# the id of, and the migrations its keep-alive writes into, before this
# Worker deploys (a job resolves its secrets when it STARTS). Advisory on a
# pull request, which deploys nothing.
dependsOn:
- component: cloudflare-hyperdrive
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 those roots lease-publish, one per deployed
# environment; tooling/wire/live.mjs picks this job's.
secretEnv:
WIRING_CLOUDFLARE_HYPERDRIVE_STAGE: "secret://next-forge/next-forge/stage/WIRING_CLOUDFLARE_HYPERDRIVE"
WIRING_CLOUDFLARE_HYPERDRIVE_PROD: "secret://next-forge/next-forge/prod/WIRING_CLOUDFLARE_HYPERDRIVE"
# Every SaaS the template wires is OPTIONAL, an Orun secret per environment
# that the product's owner stores when they sign up (README, "Connecting a
# service"). Best-effort, wire-now-seed-later: a key nobody has stored is
Expand Down
33 changes: 33 additions & 0 deletions apps/app/component.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,39 @@ spec:
inputs:
- packages/**
- pnpm-lock.yaml
# INFRA BEFORE APP, ENFORCED ON MAIN. A merge converges the whole product in
# one run: the Supabase project, the Hyperdrive config over it and the R2
# bucket, then the migrations, then this Worker. Its build renders the
# Hyperdrive id and the bucket name from the wiring documents those roots
# lease-publish, and its pages read the migrated table; a job resolves its
# secrets when it STARTS, so it waits on all three, `mode: enforced`. A pull
# request deploys nothing, so nothing on one waits: advisory there.
dependsOn:
- component: cloudflare-hyperdrive
mode: enforced
modeRules:
- mode: advisory
when:
triggerRef: github-pull-request
- component: cloudflare-r2
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 those roots lease-publish, one per deployed
# environment; tooling/wire/live.mjs picks this job's.
secretEnv:
WIRING_CLOUDFLARE_HYPERDRIVE_STAGE: "secret://next-forge/next-forge/stage/WIRING_CLOUDFLARE_HYPERDRIVE"
WIRING_CLOUDFLARE_HYPERDRIVE_PROD: "secret://next-forge/next-forge/prod/WIRING_CLOUDFLARE_HYPERDRIVE"
WIRING_CLOUDFLARE_R2_STAGE: "secret://next-forge/next-forge/stage/WIRING_CLOUDFLARE_R2"
WIRING_CLOUDFLARE_R2_PROD: "secret://next-forge/next-forge/prod/WIRING_CLOUDFLARE_R2"
# Every SaaS the template wires is OPTIONAL, an Orun secret per environment
# that the product's owner stores when they sign up (README, "Connecting a
# service"). Best-effort, wire-now-seed-later: a key nobody has stored is
Expand Down
15 changes: 15 additions & 0 deletions apps/web/component.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ spec:
inputs:
- packages/**
- pnpm-lock.yaml
# INFRA BEFORE APP, ENFORCED ON MAIN: the KV namespace its build renders the
# id of, applied before this Worker deploys (a job resolves its secrets when
# it STARTS). Advisory on a pull request, which deploys nothing.
dependsOn:
- component: cloudflare-kv
mode: enforced
modeRules:
- mode: advisory
when:
triggerRef: github-pull-request
# The wiring documents those roots lease-publish, one per deployed
# environment; tooling/wire/live.mjs picks this job's.
secretEnv:
WIRING_CLOUDFLARE_KV_STAGE: "secret://next-forge/next-forge/stage/WIRING_CLOUDFLARE_KV"
WIRING_CLOUDFLARE_KV_PROD: "secret://next-forge/next-forge/prod/WIRING_CLOUDFLARE_KV"
# Every SaaS the template wires is OPTIONAL, an Orun secret per environment
# that the product's owner stores when they sign up (README, "Connecting a
# service"). Best-effort, wire-now-seed-later: a key nobody has stored is
Expand Down
6 changes: 3 additions & 3 deletions apps/web/wiring.fixture.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"cloudflare-kv/dev": {
"app_kv_id": "00000000000000000000000000000002",
"app_kv_title": "dev-next-forge-rate-limit-dev"
"app_kv_title": "dev-next-forge-app-kv-dev"
},
"cloudflare-kv/stage": {
"app_kv_id": "00000000000000000000000000000001",
"app_kv_title": "stg-next-forge-rate-limit-stage"
"app_kv_title": "stg-next-forge-app-kv-stage"
},
"cloudflare-kv/prod": {
"app_kv_id": "00000000000000000000000000000003",
"app_kv_title": "prod-next-forge-rate-limit-prod"
"app_kv_title": "prod-next-forge-app-kv-prod"
}
}
74 changes: 74 additions & 0 deletions infra/db-migrate/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
apiVersion: sourceplane.io/v1
kind: Component
metadata:
name: db-migrate
description: Applies database migrations to stage and prod Supabase instances
labels:
domain: infra
scope: database
team: platform
layer: data

spec:
type: db-migrate
lifecycle: production
owner: platform
domain: infra

# Migrations read the Supabase connection from the orun secrets the
# `supabase` apply lease-publishes, so on main they wait for it — and for
# the Hyperdrive config over that database. Nothing here needs Hyperdrive;
# the edge is there because a migration is the one step of a convergence a
# re-run cannot take back, so it runs only once everything the Worker needs
# besides the schema exists, immediately before the Worker deploys. The
# order on main is supabase → cloudflare-hyperdrive → db-migrate → web.
# Enforced on main, advisory on a pull request (which applies nothing).
dependsOn:
- component: db
- component: supabase
mode: enforced
modeRules:
- mode: advisory
when:
triggerRef: github-pull-request
- component: cloudflare-hyperdrive
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: next-forge

# The migration runner's Management-API token (required in apply mode)
# resolves from orun secrets — minted fresh per run from the Supabase
# integration (management-access template). Replaces the ambient
# SUPABASE_ACCESS_TOKEN CI value.
secretEnv:
SUPABASE_ACCESS_TOKEN: "secret://next-forge/next-forge/{{ .environment }}/SUPABASE_ACCESS_TOKEN"
# The runner's DB identity (de-AWS — replaces the Secrets Manager read).
SUPABASE_PROJECT_REF: "secret://next-forge/next-forge/{{ .environment }}/SUPABASE_PROJECT_REF"
SUPABASE_DB_PASSWORD: "secret://next-forge/next-forge/{{ .environment }}/SUPABASE_DB_PASSWORD"
SUPABASE_DB_URL: "secret://next-forge/next-forge/{{ .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
68 changes: 68 additions & 0 deletions infra/terraform/cloudflare-hyperdrive/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
apiVersion: sourceplane.io/v1
kind: Component

metadata:
name: cloudflare-hyperdrive
description: Provisions Cloudflare Hyperdrive resources for stage and prod Supabase Postgres databases
labels:
domain: infra
scope: runtime-database-gateway

spec:
type: terraform
lifecycle: production
owner: platform
domain: infra

# Hyperdrive's origin is the Supabase project's Postgres, whose ref and
# password the `supabase` apply lease-publishes (SUPABASE_PROJECT_REF,
# SUPABASE_DB_PASSWORD, read below). A job resolves its secrets when it
# starts, so on main this waits for that apply, `mode: enforced`; a pull
# request applies nothing, so there it is advisory.
dependsOn:
- component: supabase
mode: enforced
modeRules:
- mode: advisory
when:
triggerRef: github-pull-request

parameters:
orgName: sourceplane
stackName: cloudflare-hyperdrive
terraformDir: terraform
terraformVersion: "1.15.3"
# SEC-JOB: the wiring document (hyperdrive id/name) is lease-published
# post-apply; worker deploys read it back as WIRING_CLOUDFLARE_HYPERDRIVE_<ENV>.
secretOutputs: "WIRING_CLOUDFLARE_HYPERDRIVE=wiring"

# All three values resolve from orun secrets (project rung, brokered from
# the Cloudflare integration): the provider credential is minted fresh per
# run from the hyperdrive-edit template (Workers Hyperdrive Write — the
# workers-deploy token deliberately cannot touch Hyperdrive), and the
# account id is a non-secret connection fact (account-id template).
# Component-level because plan-only lanes refresh against the live API too.
secretEnv:
CLOUDFLARE_API_TOKEN: "secret://next-forge/next-forge/{{ .environment }}/CLOUDFLARE_HYPERDRIVE_TOKEN"
CLOUDFLARE_ACCOUNT_ID: "secret://next-forge/next-forge/{{ .environment }}/CLOUDFLARE_ACCOUNT_ID"
TF_VAR_cloudflare_account_id: "secret://next-forge/next-forge/{{ .environment }}/CLOUDFLARE_ACCOUNT_ID"
# Supabase origin, lease-published by the supabase component's
# secretOutputs — the replacement for the Secrets Manager data source.
TF_VAR_supabase_project_ref: "secret://next-forge/next-forge/{{ .environment }}/SUPABASE_PROJECT_REF"
TF_VAR_supabase_db_password: "secret://next-forge/next-forge/{{ .environment }}/SUPABASE_DB_PASSWORD"

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

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

40 changes: 40 additions & 0 deletions infra/terraform/cloudflare-hyperdrive/terraform/adopt.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# --- Self-healing adoption (AWS-free state seeding) ---
#
# Same pattern as cloudflare-kv/terraform/adopt.tf: when the platform state
# does not yet track the Hyperdrive config but Cloudflare already has one
# under this name, import it at plan time instead of creating a duplicate.
# Fresh products resolve to id = "" and create normally; a resource already in
# state short-circuits the lookup. Note the origin password still comes from
# config — after adoption the first apply re-asserts the origin credentials
# from SUPABASE_DB_PASSWORD, which is exactly the re-sync the cutover needs.

data "external" "adopt_hyperdrive_config" {
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 == "cloudflare_hyperdrive_config" and .name == "postgres")] | length > 0' \
"$state_file" >/dev/null 2>&1; then
jq -n '{id: ""}'
exit 0
fi
resp="$(curl -fsS --retry 3 -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
"https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/hyperdrive/configs")"
printf '%s' "$resp" | jq -c --arg n "$name" \
'{id: ([.result[] | select(.name == $n) | .id] | first // "")}'
EOT
]
query = {
name = local.hyperdrive_name
}
}

import {
for_each = toset(compact([data.external.adopt_hyperdrive_config.result.id]))
to = cloudflare_hyperdrive_config.postgres
id = "${var.cloudflare_account_id}/${each.value}"
}
5 changes: 5 additions & 0 deletions infra/terraform/cloudflare-hyperdrive/terraform/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Terraform backend configuration is provided by Orun at runtime
# through environment variables and automatic backend initialization.
#
# S3 backend details are set via CLI args in the Orun runner.
# Local placeholder file for clarity.
Loading
Loading