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.
What a deployment repository needs
Section titled “What a deployment repository needs”package.jsondepending on a Portona release tarball, such ashttps://github.com/mylescarrick/portona/releases/download/v0.4.1/portona-0.4.1.tgz. The lockfile records the tarball’s hash.cloudflare.config.tswith the Worker’s name and these bindings:DB: a D1 databaseBACKUPS: an R2 bucketBROKER_AUDIENCE: the audience tokens must carry, unique to this deploymentBROKER_MASTER_KEY: bindings.secret()EXCHANGE_RATE_LIMIT: bindings.rateLimit(...)PORTONA_ACCESS: bindings.json(access),PORTONA_POLICIES: bindings.json(policies), or both
access.tstyped withimport type { Access } from "portona/config", orpolicies.tstyped withPolicies. See Environments and grants.- Optionally
PORTONA_ADMIN: bindings.json({ teamDomain, audience, emails } satisfies AdminConfig)to turn on the admin pages.
Deploy
Section titled “Deploy”bunx --no-install portona-assemble [--mode staging] # validates access, writes .cloudflare/outputbunx --no-install cf deploy --prebuiltportona-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.
Master key
Section titled “Master key”Generate one with:
openssl rand -base64 32Store 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.
Wrapping key
Section titled “Wrapping key”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:
bunx --no-install portona-keys init --mode stagingIt 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.
Releases
Section titled “Releases”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:
-
Move your dependency to the new tarball’s URL in a reviewed commit, and run
bun install. -
Apply any new migrations to each deployment’s D1 before deploying it.
cfevaluatescloudflare.config.tswhen 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.
-
Deploy staging, check it, then deploy production.
Migrations only add, so the previous release keeps working on a migrated database; never undo one.
Rolling back
Section titled “Rolling back”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.