Skip to content
Draft
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
131 changes: 131 additions & 0 deletions .github/workflows/decision-events-worker-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
name: decision-events worker release

# CI tier policy: see .github/ci-policy.md. This is a manual release
# workflow: workflow_dispatch does not run tests or deploy on PRs or pushes.

on:
workflow_dispatch:
inputs:
confirm_target:
description: "I confirm the existing decision-events Worker and D1 ID in the configured Cloudflare account are the intended target."
required: true
type: boolean
default: false
promote:
description: "Create a 100% deployment after uploading the version."
required: true
type: boolean
default: false

permissions:
contents: read

concurrency:
group: decision-events-worker-production
cancel-in-progress: false

jobs:
release:
if: github.ref == 'refs/heads/main'
name: release decision-events Worker
runs-on: ubuntu-latest
timeout-minutes: 20
environment: decision-events-production
defaults:
run:
working-directory: pm-stack/decision-events-worker
steps:
- name: Checkout
uses: actions/checkout@v7

- name: Set up Node
uses: actions/setup-node@v7
with:
node-version: 24.22.0

- name: Set up Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.3.3

- name: Install locked dependencies
run: npm ci

- name: Run Worker tests and typecheck
run: |
bun test
npm run typecheck
npm run test:release

- name: Build and inspect offline Build Output
run: |
export WRANGLER_LOG_PATH="$RUNNER_TEMP/wrangler-decision-events.log"
./node_modules/.bin/cf workers versions create \
--worker decision-events \
--tag offline-${GITHUB_SHA:0:12} \
--message "offline Build Output check" \
--dry-run
npm run check:build-output

- name: Require explicit target confirmation
env:
CONFIRM_TARGET: ${{ inputs.confirm_target }}
run: |
if [[ "$CONFIRM_TARGET" != "true" ]]; then
echo "Set confirm_target after verifying the existing account, Worker, and D1 target." >&2
exit 1
fi

- name: Verify existing Worker and D1 identity (read-only)
env:
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
set -euo pipefail
if [[ ! "$CLOUDFLARE_ACCOUNT_ID" =~ ^[0-9a-fA-F]{32}$ || -z "$CLOUDFLARE_API_TOKEN" ]]; then
echo "Configure the account ID variable and API token secret in decision-events-production." >&2
exit 1
fi
./node_modules/.bin/cf -q workers get decision-events > "$RUNNER_TEMP/decision-events-worker.json"
./node_modules/.bin/cf -q d1 get 68ade85e-6835-46a8-af6a-a9f2f2d966ae > "$RUNNER_TEMP/decision-events-d1.json"
node scripts/check-target.mjs \
"$RUNNER_TEMP/decision-events-worker.json" \
"$RUNNER_TEMP/decision-events-d1.json"

- name: Upload immutable Worker version
id: upload
env:
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
set -euo pipefail
if [[ ! "$CLOUDFLARE_ACCOUNT_ID" =~ ^[0-9a-fA-F]{32}$ ]]; then
echo "Set the decision-events-production CLOUDFLARE_ACCOUNT_ID variable." >&2
exit 1
fi
if [[ -z "$CLOUDFLARE_API_TOKEN" ]]; then
echo "Set the decision-events-production CLOUDFLARE_API_TOKEN secret." >&2
exit 1
fi
export WRANGLER_LOG_PATH="$RUNNER_TEMP/wrangler-decision-events.log"
tag="git-$(printf '%s' "$GITHUB_SHA" | cut -c1-12)"
./node_modules/.bin/cf workers versions create \
--worker decision-events \
--tag "$tag" \
--message "GitHub Actions release ${GITHUB_SHA}"
echo "tag=$tag" >> "$GITHUB_OUTPUT"

- name: Promote uploaded version to 100 percent
if: ${{ inputs.promote }}
env:
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
RELEASE_TAG: ${{ steps.upload.outputs.tag }}
run: |
set -euo pipefail
version_id="$(node scripts/resolve-deployable-version.mjs "$RELEASE_TAG")"
versions="$(node -e 'process.stdout.write(JSON.stringify([{version_id: process.argv[1], percentage: 100}]))' "$version_id")"
./node_modules/.bin/cf workers deployments create \
--worker decision-events \
--strategy percentage \
--versions "$versions"
2 changes: 2 additions & 0 deletions pm-stack/decision-events-worker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.cloudflare/
node_modules/
69 changes: 54 additions & 15 deletions pm-stack/decision-events-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,63 @@ Turso. Credentials are resolved **by name** only:
- Turso token: `turso.fleet-platform-rw-token`
- Webhook secret: `cloudflare.decision-events.webhook-secret`

## Deploy
## GitHub Actions release (#9335 Track B)

```sh
# Token by name, piped — never on disk/argv (fleet wrangler pattern):
fleet secrets get cloudflare.api-token-day-to-day | \
python3 ~/tools/cloudflare/wrangler-authed.py d1 create decision-events
# put the returned database_id in wrangler.toml, then:
fleet secrets get cloudflare.api-token-day-to-day | \
python3 ~/tools/cloudflare/wrangler-authed.py d1 execute decision-events --remote --file schema.sql
fleet secrets get cloudflare.api-token-day-to-day | \
python3 ~/tools/cloudflare/wrangler-authed.py deploy
fleet secrets get cloudflare.decision-events.webhook-secret | \
python3 ~/tools/cloudflare/wrangler-secret.py # or: wrangler secret put WEBHOOK_SECRET
```
The manual Wrangler deploy path has been retired. Use
`.github/workflows/decision-events-worker-release.yml` from `main`. It
builds and checks the Worker offline, uploads a version with
`cf workers versions create`, and optionally promotes the selected
version to 100% with `cf workers deployments create`. The `promote`
workflow input defaults to false, so a run can upload a version without
changing the active deployment.

The release uses `cf@1.0.0-rc.2` and Wrangler `4.163.0`, the compatible
Build Output toolchain pinned in this package. The programmatic
`cloudflare.config.ts` is the deployment declaration; `wrangler.toml`
continues to support local Wrangler development and tests. Both retain
Worker `decision-events`, compatibility date `2025-09-15`,
`workers_dev = true`, D1 binding `DB` to database ID
`68ade85e-6835-46a8-af6a-a9f2f2d966ae`, DO binding `SET_EVENTS` to class
`DecisionSetEvents`, and SQLite storage. `wrangler.toml` retains the
existing `v1` migration declaration; the release's offline parity check
asserts that it is unchanged. The Worker has no custom domain or custom
route configured.

### GitHub environment prerequisites

Configure the protected GitHub Environment `decision-events-production`
with:

- Variable `CLOUDFLARE_ACCOUNT_ID`, set to the account that already owns
the `decision-events` Worker and the configured D1 database.
- Secret `CLOUDFLARE_API_TOKEN`, scoped to that account and permitted to
read Worker/D1 identity, upload Worker versions, and create Worker
deployments.
- Required reviewers before any version upload or promotion.

The Worker and D1 database must already exist in that account. This
workflow deliberately does not create either resource or alter D1 schema
or Durable Object migration state. If the Worker does not exist,
`cf workers versions create` cannot bootstrap it; stop and handle that
separately. `WEBHOOK_SECRET` must already be installed on the Worker
before producer cutover, and its value stays in the secret store.

The release changes the Worker version/deployment only. It does not
submit decision events, change producer settings, apply SQL, modify
custom routes/domains, or retire the Turso sink. Producer cutover still
requires a separate decision: verify `/health` reports the secret
configured, check the aggregator outbox/state with `--dry-run`, confirm
the exact `workers.dev` URL and credentials, then explicitly approve
the first real sync. With the webhook URL set, the aggregator defaults
to writing to both Turso and this Worker.

## Tests

```sh
bun test # contract tests for src/events.ts (no CF runtime needed)
bunx tsc --noEmit # typecheck (needs bun install for workers-types)
bun test # contract tests for src/events.ts (no CF runtime needed)
npm run typecheck # TypeScript types from generated config
npm run test:release # offline target/version selection tests
./node_modules/.bin/cf workers versions create --worker decision-events \
--tag offline-local --message "offline Build Output check" --dry-run
npm run check:build-output # checks D1, DO, migration, and route invariants
```
Loading