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
9 changes: 9 additions & 0 deletions .baseline/identity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"_comment": "THIS TREE'S OWN IDENTITY — the FROM side of every rebrand. tooling/rebrand/rebrand.mjs reads it to know what to rename AWAY from; a product's identity is the --values file, which is the TO side. Change a value here only together with the tree: these strings must be what the tree actually contains, or the rename silently misses and the product ships carrying this baseline's name. `productName` is the SITE_NAME seam in apps/web/utils/site.ts (upstream's footer brand); `domain` and `subdomain` are placeholders, because this baseline has never deployed under a name of its own; `githubOwner` is the owner in this repository's source link.",
"slug": "subscription-payments",
"pascal": "SubscriptionPayments",
"productName": "ACME",
"domain": "subscription-payments.example.com",
"subdomain": "your-workers-subdomain",
"githubOwner": "sourceplane"
}
172 changes: 172 additions & 0 deletions .baseline/product/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# subscription-payments

Vercel's [Next.js Subscription Payments Starter](https://github.com/vercel/nextjs-subscription-payments)
— Supabase Auth with cookie sessions through `@supabase/ssr`, an account
page, a Postgres schema of users, customers, products, prices and
subscriptions kept in sync by Stripe's webhooks, Stripe Checkout to subscribe
and the Customer Portal to manage — on **Cloudflare Workers + Static Assets**
through [`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare), with a
**Supabase** project per environment. Terraform provisions the project and
Orun's `db-migrate` applies the schema, row-level security on every table
included, all on every merge to `main`.

## Live deployment

<!-- 02-docs:begin -->
Not recorded yet. The bootstrap's last phase writes the live URLs, the
workspace, whether Stripe is connected and the secrets inventory into
[ai/context/deployment.md](ai/context/deployment.md); how the product deploys
and how to operate it is in
[ai/context/operations.md](ai/context/operations.md).
<!-- 02-docs:end -->

## What is here

| Surface | Where |
|---|---|
| The product's name | `apps/web/utils/site.ts` |
| Pricing (`/`), sign-in views (`/signin/[id]`), the account page (`/account`), the auth code exchanges, the Stripe webhook (`/api/webhooks`), `/api/health` | `apps/web/app` |
| Supabase clients (browser, server, admin, the session-refreshing proxy) and their queries | `apps/web/utils/supabase`, `apps/web/proxy.ts` |
| Stripe: the client and whether it is connected, Checkout and the Customer Portal, the sample plans shown before it is | `apps/web/utils/stripe` |
| Workers build and deploy contract | `apps/web/next.config.mjs`, `open-next.config.ts`, `wrangler.template.jsonc` → `wrangler.jsonc`, `component.yaml` |
| The deploy-time wiring: the wrangler render and the Worker-secret push | `tooling/wire` |
| The schema as migrations, and their runner | `packages/db` (`@saas/db`) |
| The Supabase project and the migration step | `infra/terraform/supabase`, `infra/db-migrate` |
| Local development with the Supabase CLI and the Stripe CLI | `apps/web/supabase`, `apps/web/fixtures`, `apps/web/types_db.ts` |
| Tests | `tests/web`, `tests/db` (vitest) |

## Getting started

```bash
pnpm install
pnpm run wire:fixture # re-render apps/web/wrangler.jsonc from its template (it is committed)
pnpm typecheck
pnpm lint
pnpm test # tests/db runs every migration in PGlite and checks row-level security
pnpm build # next build + opennextjs-cloudflare build → apps/web/.open-next
```

The build needs no Supabase project and no Stripe account: it inlines the
placeholder project from the committed `wrangler.jsonc`.

## Running it locally

1. **A Supabase project.** Either the Supabase CLI's local stack
(`pnpm --filter @app/web supabase:start`, which applies
`apps/web/supabase/migrations`; pnpm 10 blocks the `supabase` package's
binary download until you `pnpm approve-builds` it), or a hosted project
you apply `packages/db/src/migrations` to.
2. **Environment.** Copy `apps/web/.env.local.example` to
`apps/web/.env.local`: the project URL, anon and service-role keys, and —
optionally — the Stripe keys. `next dev` reads it:

```bash
pnpm --filter @app/web dev # http://localhost:3000
```

3. **Or the built Worker** under `wrangler dev`, which reads the Supabase URL
and anon key from `wrangler.jsonc` (point them at your project in
`wiring.fixture.json` and re-render, or override them in `.dev.vars`) and
its secrets from `apps/web/.dev.vars` (copy `.dev.vars.example`):

```bash
pnpm build
pnpm --filter @app/web preview # http://localhost:8787
```

4. **Stripe.** `pnpm --filter @app/web stripe:login`, then forward webhooks
to the server you are running and create the template's plans:

```bash
pnpm --filter @app/web stripe:listen # → localhost:3000/api/webhooks
pnpm --filter @app/web stripe:fixtures # Hobby and Freelancer, monthly and yearly
```

Test checkout with card `4242 4242 4242 4242`, any future date, any CVC.
Without Stripe keys everything else runs, and the pricing page says
checkout is not connected.

## How it deploys

Merges to `main` converge through [Orun](https://github.com/sourceplane/orun):
`.github/workflows/ci.yml` plans the changed components and runs each lane
against the workspace in `intent.yaml`. Per environment the order is
enforced — **the Supabase project, then the migrations, then the Worker** —
because each reads what the one before it published: the project's
credentials, then `WIRING_SUPABASE` (the project URL and anon key the build
inlines, and the service-role key the deploy hands the Worker as a secret).
`stage` deploys first and `prod` once stage has converged on the same
commit; `dev` only verifies. Every deploy is smoke-tested over HTTP,
`/api/health` included, which answers only when the Worker reaches its
migrated project with a valid service-role key.

The pipeline holds one credential, GitHub's own `GITHUB_TOKEN`. The
Cloudflare token and account id and the Supabase access token and org id are
brokered per run from the workspace's connections; the Supabase project's
credentials and keys are published by the Terraform apply as orun secrets.
Nothing provider-shaped is stored in the repository or in GitHub, and the
service-role key is never in a wrangler file or a browser bundle.

Every table in `public` has row-level security, with the template's
policies: a visitor reads the plans and their own user row and subscriptions,
never anyone else's, and the user→Stripe-customer map is the service role's
alone. Migration `020_row_level_security` stops a deploy that would leave a
table without it.

## Connect Stripe

Stripe is a third party no platform brokers, so the product deploys without
it: sign-up, sign-in and the account page work, the pricing page shows the
template's sample plans and says **"Connect Stripe to enable checkout."**,
and the webhook answers 503. To turn payments on, per environment (test mode
first):

1. Add a webhook endpoint at
`https://subscription-payments-web-prod.your-workers-subdomain.workers.dev/api/webhooks`
(and `-stage` for stage) for `product.*`, `price.*`,
`checkout.session.completed` and `customer.subscription.*`.
2. Set the three Worker secrets:

```bash
wrangler secret put STRIPE_SECRET_KEY --name subscription-payments-web-prod
wrangler secret put STRIPE_WEBHOOK_SECRET --name subscription-payments-web-prod # the endpoint's signing secret
wrangler secret put STRIPE_PUBLISHABLE_KEY --name subscription-payments-web-prod # pk_…: public, read per request
```

3. Create your products and prices in Stripe — after the endpoint exists,
so the webhook syncs them into the database — or run
`pnpm --filter @app/web stripe:fixtures` with that account for the
template's two plans.
4. Configure the Customer Portal (Stripe → Settings → Billing → Customer
portal).

Worker secrets survive every later deploy; the next request sees them.

## Supabase Auth email

The Supabase project's Auth sends visitors back to the Worker (its Site URL
and redirect allow-list are set by `infra/terraform/supabase`), and new
sign-ups are signed straight in, because a project without SMTP of its own
emails only its organization's members. Before real users arrive, give the
project an SMTP server (Supabase dashboard → Authentication → Emails → SMTP
Settings), then set `authEmailConfirmation: "true"` in
`infra/terraform/supabase/component.yaml` and merge: sign-up then confirms
the address first, and magic links and password resets reach anyone. OAuth
providers are enabled in the same dashboard.

## Custom domain

The app answers on workers.dev from its first deploy. To serve it on your
own domain, attach the domain to the Worker in Cloudflare (Workers & Pages →
`subscription-payments-web-prod` → Settings → Domains & Routes), set
`NEXT_PUBLIC_SITE_URL` in `apps/web/wrangler.template.jsonc` and the Auth
origin (`app_origin`) in `infra/terraform/supabase/terraform/main.tf` to it,
run `pnpm run wire:fixture`, merge, and point Stripe's webhook at the new
origin.

## CI

| Workflow | What it runs |
|---|---|
| `checks.yml` | install, wire fixture, typecheck, lint, test, build — every pull request (not pushes: on `main`, `ci.yml` must be the only run a commit has) |
| `ci.yml` | the Orun pipeline: `orun plan --changed`, then one job per lane; applies, migrates and deploys stage → prod on merge |
31 changes: 31 additions & 0 deletions .baseline/tasks/01-product.TaskContract.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Task contract template for the 01-product landing.
# Unbound to a key on purpose: `orun.task/ensure@v1` seals and attaches it to
# the key the allocator issues. It lives under `.baseline/`, not `tasks/`,
# because `tasks/` is this repository's own work tree (`intent.yaml` `work:`),
# where an unkeyed contract is an error. gates: [] is a DECLARATION — merge
# alone finishes this task; the main convergence is the gate the bootstrap
# watches, not one the plane observes as a PR check.
#
# The whole product in one landing. `affects` names COMPONENTS, by
# metadata.name — every one the bootstrap's convergence plans on stage and
# prod (the two test packages verify on dev, which the bootstrap keeps off
# main until 02-docs, so a marker on them would re-plan nothing): the phase's
# retouch hook reads this list to decide which component.yaml files get the
# redeploy marker, so a re-run after a failed convergence has a diff and
# re-plans exactly these — the deploy set and the task plane's statement of
# the work are one list.
apiVersion: orun.io/v1
kind: TaskContract
spec:
goal: The product's repository is born and linked, the whole product lands in one pull request, and one convergence provisions the Supabase project, migrates the schema with its row-level security and deploys the app on stage and prod
affects:
- supabase
- db
- db-migrate
- web
doneWhen:
- repo pushed, main pinned as default, workspace linked
- SUPABASE_PROJECT_REF, SUPABASE_DB_PASSWORD, SUPABASE_DB_URL and WIRING_SUPABASE published on stage and prod
- migrations applied and the app deployed, its service-role key a Worker secret, and smoke-tested on stage and prod, in the order supabase → db-migrate → web
- the pricing page and /api/health answer on both workers.dev URLs
gates: []
14 changes: 14 additions & 0 deletions .baseline/tasks/02-docs.TaskContract.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Task contract template for the 02-docs landing. See 01-product's header.
apiVersion: orun.io/v1
kind: TaskContract
spec:
goal: The live deployment is recorded from verified facts as agent-readable state in the product's repo, and the bootstrap's deferrals are put back
affects:
- ai/context/deployment.md
- ai/context/operations.md
- README.md
- intent.yaml
doneWhen:
- committed deployment manifest matches probed reality
- prod waits on stage again, and dev verifies every merge to main again
gates: []
135 changes: 135 additions & 0 deletions .github/workflows/baseline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
name: Baseline CI

# THE FACTORY'S OWN LANES — this repository tested as a BASELINE: that its
# build document parses with the orun a bootstrap actually runs, that its card
# agrees with it, that every component is placed by exactly one phase, that a
# placed tree comes up branded and carrying nothing of the factory, that the
# product's own push-to-main plan deploys infra before app, and that the
# product installs, builds, migrates, deploys (dry), serves and signs a user
# up, in and out with no Stripe key — through Supabase Auth and the Data API,
# with every table in `public` behind row-level security.
#
# ─── WHY THIS IS NOT IN ci.yml OR checks.yml ──────────────────────────────
#
# `repo-blueprint.yaml`'s `github-workflows` module copies the WHOLE `.github`
# directory into every product built from this repository. These jobs run
# `testing/*.sh`, and `testing/` is deliberately not placed — so in a product
# they would be steps that cannot pass, red on its very first pull request. This
# file is in the blueprint's `ignore` list, and `testing/coverage.test.sh`
# fails on any workflow that reads `testing/` and is not.

on:
push:
branches: [main]
pull_request:

concurrency:
group: baseline-ci-${{ github.event_name == 'pull_request' && github.ref || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

permissions:
contents: read

jobs:
# Tier 0. bash, python3 and git; node for the tools. No workspace install,
# no network, no credential — the job that most certainly fails a PR.
baseline-contract:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v5
with:
node-version: 22
# setup-node v5 turns on the pnpm cache by itself when package.json
# names a packageManager, and then fails on a runner with no pnpm.
# This job installs nothing.
package-manager-cache: false
- name: Contract tests (manifest, phases, coverage, rebrand, retouch, bootstrap-defer)
run: |
set -euo pipefail
python3 -c 'import yaml' 2>/dev/null || pip install --quiet pyyaml
for t in manifest phases coverage rebrand retouch bootstrap-defer; do
echo "── $t"
bash "testing/$t.test.sh"
done

# Tier 1. THE BLUEPRINT, READ AND PLACED BY THE BINARY A BOOTSTRAP RUNS.
#
# Pinned at the bootstrap floor (BOOTSTRAP.md), not the product's lane pin:
# every test here reads YAML with PyYAML, which parses anything well-formed;
# only the pinned orun can say whether the document is one a bootstrap can
# run. `--status` parses it, validates every hook against the action
# registry, compiles every `when` and narration template, and writes
# nothing. Then the leak gate (what a product would carry) and the
# placement tier (the offline bootstrap: both phases placed and finished by
# the blueprint's own hooks, then the product's own `orun plan` for the
# 01-product merge — supabase, then db-migrate, then web, enforced — and a
# --resume that changes nothing).
blueprint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: sourceplane/orun-action@v1.2.0
with:
version: v2.58.11
- uses: pnpm/action-setup@v4
with:
version: 10.12.1
- uses: actions/setup-node@v5
with:
node-version: 22
- name: repo-blueprint.yaml, read by the pinned CLI
run: |
set -euo pipefail
orun --version
python3 -c 'import yaml' 2>/dev/null || pip install --quiet pyyaml
# Required inputs must be supplied for the document to resolve; the
# values are arbitrary and nothing is written.
orun new --blueprint repo-blueprint.yaml --status \
--out "$(mktemp -d)" \
--set reponame=acme-pay \
--set "productname=Acme Cloud" \
--set productdomain=acme.dev \
--set githuborg=acme-inc
- name: the leak gate — what a product carries
run: bash testing/leak.test.sh
- name: placement — the offline bootstrap, and the product's own plan
run: |
git config --global user.email ci@example.invalid
git config --global user.name "baseline ci"
bash testing/placement.test.sh

# Tier 2. THE PRODUCT IT PRODUCES WORKS: the 01-product landing under the
# checks its pull request waits on; a Supabase project's API on the runner —
# its own Postgres, Supabase Auth and the Data API (the pinned GoTrue and
# PostgREST releases, checksummed) behind one gateway that checks the API
# key; the migrations applied by the product's own runner (through a
# stand-in for the one Management API endpoint it calls); the stage lane's
# own structure check, build, output check and dry deploy in the job tree
# orun stages, handed the stage wiring document; and the built Worker served
# over that project with no Stripe key — its smoke, every route, a sign-up,
# sign-out, sign-in and the account page through its own server actions, and
# the anon key refused every row the policies do not give it. Pinned at the
# product's lane version, because that is the orun its CI runs. The runner
# image carries PostgreSQL 16 (not started); the test starts its own on a
# random port.
product:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: sourceplane/orun-action@v1.2.0
with:
version: v2.78.4
- uses: pnpm/action-setup@v4
with:
version: 10.12.1
- uses: actions/setup-node@v5
with:
node-version: 22
cache: pnpm
- name: e2e — the bootstrapped product builds, migrates, deploys (dry) and serves
run: |
python3 -c 'import yaml' 2>/dev/null || pip install --quiet pyyaml
git config --global user.email ci@example.invalid
git config --global user.name "baseline ci"
bash testing/e2e.test.sh
21 changes: 14 additions & 7 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
name: Checks

# Credential-free verification: install, render the offline wrangler fixtures,
# typecheck, lint, test and build. Runs on every push and pull request and is
# the merge gate until the product is linked to an Orun workspace, at which
# point `ci.yml` (Orun plan/run with brokered Cloudflare credentials) takes
# over — it stays gated behind the `ORUN_CI` repository variable until then.
# Credential-free verification: install, render the offline wrangler fixture,
# typecheck, lint, test and build, on every pull request, linked or not. No
# database and no Stripe account: nothing here connects to either.
# `ci.yml` is the Orun pipeline beside it (plan/run with brokered Cloudflare
# and Supabase credentials, deploys on merge); it plans nothing until
# intent.yaml names a workspace.
#
# PULL REQUESTS ONLY — not pushes to main. A push to main must start exactly
# one workflow, `ci.yml`, because a bootstrap's convergence watch
# (`orun.run/watch@v1` with `sha:`) takes the newest run for the merge commit
# from ANY workflow: with this one on push too, the watch can read this run
# green while ci.yml's deploy lanes failed, and call the product converged.
# On main the convergence builds and tests everything anyway.
on:
push:
branches: [main]
pull_request:
workflow_dispatch:

concurrency:
group: checks-${{ github.event_name == 'pull_request' && github.ref || github.run_id }}
Expand Down
Loading
Loading