Skip to content

Deploying your own

Portona is the engine. Each deployment lives in its own repository, usually private, that holds its access policies and Cloudflare settings. The public repository never contains deployment values.

  • package.json depending on a Portona release tarball, such as https://github.com/mylescarrick/portona/releases/download/v0.4.1/portona-0.4.1.tgz. The lockfile records the tarball’s hash.
  • cloudflare.config.ts with the Worker’s name and these bindings:
    • DB: a D1 database
    • BACKUPS: an R2 bucket
    • BROKER_AUDIENCE: the audience tokens must carry, unique to this deployment
    • BROKER_MASTER_KEY: bindings.secret()
    • EXCHANGE_RATE_LIMIT: bindings.rateLimit(...)
    • PORTONA_ACCESS: bindings.json(access), PORTONA_POLICIES: bindings.json(policies), or both
  • access.ts typed with import type { Access } from "portona/config", or policies.ts typed with Policies. See Environments and grants.
  • Optionally PORTONA_ADMIN: bindings.json({ teamDomain, audience, emails } satisfies AdminConfig) to turn on the admin pages.
Terminal window
bunx --no-install portona-assemble [--mode staging] # validates access, writes .cloudflare/output
bunx --no-install cf deploy --prebuilt

portona-assemble decodes PORTONA_ACCESS, PORTONA_POLICIES and PORTONA_ADMIN strictly, so an unknown or misspelt key fails the build. It then wraps your config around the prebuilt Worker from the release. The compatibility date and flags come from the prebuilt Worker, because its code was bundled for them.

Give each deployment, such as staging and production, its own audience, D1 database, R2 bucket, master key and rate-limit namespace. A staging token is then refused by production.

Generate one with:

Terminal window
openssl rand -base64 32

Store it as the Worker secret BROKER_MASTER_KEY, and save a copy with its key id in a password manager before storing any secret. Without it, every secret and every backup is unrecoverable.

A second key, PORTONA_WRAPPING_KEY, wraps each environment’s own data key. It’s escrowed to your team’s RSA SSH keys, so creating it needs only a cf login that can write Workers, and an agent can do it. List two or more people who can recover it in portona-recipients.pub, then, from a clean checkout of your deployment repository’s origin/main:

Terminal window
bunx --no-install portona-keys init --mode staging

It commits and pushes its escrow file to keys/ before it sets the Worker secret. Then have each recipient run the recovery drill. Wrapping key escrow has the details, the recovery commands and the limits.

The admin page’s Keys view shows the key’s id. Don’t set it to a master key: the Worker refuses one.

With the key set, every admin change to a PORTONA_ACCESS environment also stores the value under that environment’s own data key, in the same transaction. The first write creates the data key. The master-key rows stay complete, so a Worker without the key, or an earlier release, works as before. Backfill data keys on the Keys view copies existing secrets across and moves each environment’s reads to its data-keyed copies once all of them are copied; see Wrapping key escrow. Backups hold the data keys and copies too; see Backups and recovery.

Pushing a v* tag to the Portona repository runs CI and publishes portona-<version>.tgz as a GitHub release. The changelog describes each release. To upgrade:

  1. Move your dependency to the new tarball’s URL in a reviewed commit, and run bun install.

  2. Apply any new migrations to each deployment’s D1 before deploying it. cf evaluates cloudflare.config.ts when it runs inside your repository, so run it from outside, with absolute paths:

    Terminal window
    repo=$PWD
    (cd ~ && "$repo/node_modules/.bin/cf" d1 migrations apply <database-id> \
    --dir "$repo/node_modules/portona/migrations")

    It takes the database’s ID, not its name. A fresh database needs every migration, which this applies in order.

  3. Deploy staging, check it, then deploy production.

Migrations only add, so the previous release keeps working on a migrated database; never undo one.

Roll back with a Cloudflare version rollback of the Worker, which keeps the bindings that version was deployed with. Reverting the dependency alone isn’t enough when your access configuration uses something the older release can’t read. For example, a 0.3 Worker reads only PORTONA_POLICIES, so rolling a deployment that uses PORTONA_ACCESS back to 0.3 means restoring its old policies too. Those grant whole environments, so name-narrowed grants widen again. If that’s not acceptable, turn on maintenance instead of rolling back.