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
249 changes: 249 additions & 0 deletions .github/workflows/bedside-and-away-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
name: bedside-and-away release

# Manual-only release. The version upload and production promotion are
# separate workflow_dispatch runs with separate protected environments.
# Configure both environments with required reviewers in repository settings.
on:
workflow_dispatch:
inputs:
action:
description: Select a dry run, upload-only, or separately approved promotion
type: choice
options:
- dry-run
- upload
- promote
default: dry-run
required: true
version_id:
description: Exact version ID from a completed upload run (required for promote)
type: string
required: false
source_sha:
description: Exact 40-character source SHA from that upload run (required for promote)
type: string
required: false

permissions:
contents: read

concurrency:
group: bedside-and-away-${{ inputs.action }}
cancel-in-progress: false

jobs:
upload:
if: github.ref == 'refs/heads/main' && (inputs.action == 'dry-run' || inputs.action == 'upload')
name: Build and upload Worker version only
runs-on: ubuntu-latest
timeout-minutes: 30
environment:
name: bedside-and-away-version-upload
env:
WORKER: bedside-and-away-backend
TURSO_URL: ${{ vars.BEDAWAY_TURSO_URL }}
GATEWAY_ORIGIN: ${{ vars.BEDAWAY_GATEWAY_ORIGIN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
steps:
- uses: actions/checkout@v7

- uses: actions/setup-node@v7
with:
node-version: 24

- name: Install the repository-pinned cf version
run: |
set -euo pipefail
CF_VERSION="$(sed -n 's/^[[:space:]]*version = "\([^"]*\)";/\1/p' pkgs/cf/default.nix | head -n 1)"
test -n "$CF_VERSION"
npm install --global "cf@${CF_VERSION}" --no-audit --no-fund
cf --version

- name: Validate inputs and build the production Worker manifest
working-directory: pkgs/bedside-and-away
run: |
set -euo pipefail
test -n "$TURSO_URL"
test -n "$GATEWAY_ORIGIN"
test -n "$CLOUDFLARE_ACCOUNT_ID"
test "$WORKER" = bedside-and-away-backend
node external/cf-build.mjs
jq -e --arg name "$WORKER" '
.name == $name and
.workersDev == true and
.previewUrls == false and
(has("routes") | not) and
(has("route") | not) and
.env.TURSO_URL.type == "text" and
.env.GATEWAY_ORIGIN.type == "text" and
.env.TURSO_AUTH_TOKEN.type == "secret" and
.env.GATEWAY_HMAC_KEY.type == "secret"
' .cloudflare/output/v0/workers/default/worker.config.json >/dev/null
node --input-type=module -e '
import fs from "node:fs";
const config = JSON.parse(fs.readFileSync(".cloudflare/output/v0/workers/default/worker.config.json", "utf8"));
const database = new URL(process.env.TURSO_URL);
const origin = new URL(process.env.GATEWAY_ORIGIN);
if (config.env.TURSO_URL.value !== database.href || config.env.GATEWAY_ORIGIN.value !== origin.origin) {
throw new Error("generated Worker environment differs from the configured production endpoints");
}
'

- name: Run native cf Versions dry run
working-directory: pkgs/bedside-and-away
run: |
set -euo pipefail
SHA="${GITHUB_SHA}"
TAG="git-${SHA:0:12}"
MESSAGE="deploy ${SHA} via protected GitHub Actions cf Versions workflow"
cf workers versions create --prebuilt --worker "$WORKER" --tag "$TAG" --message "$MESSAGE" --dry-run > "$RUNNER_TEMP/bedaway-cf-dry-run.out"

- name: Upload tagged version without promoting it
if: inputs.action == 'upload'
working-directory: pkgs/bedside-and-away
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
set -euo pipefail
test -n "$CLOUDFLARE_API_TOKEN"
SHA="${GITHUB_SHA}"
TAG="git-${SHA:0:12}"
MESSAGE="deploy ${SHA} via protected GitHub Actions cf Versions workflow"
cf workers versions create --prebuilt --worker "$WORKER" --tag "$TAG" --message "$MESSAGE" > "$RUNNER_TEMP/bedaway-upload.json"
cf workers versions list --worker-id "$WORKER" > "$RUNNER_TEMP/bedaway-versions.json"
VERSION_ID="$(jq -er --arg tag "$TAG" --arg message "$MESSAGE" '
(if type == "array" then .
elif (.result | type) == "array" then .result
elif (.result.items | type) == "array" then .result.items
elif (.versions | type) == "array" then .versions
else error("unexpected cf versions list response") end)
| map(select((.annotations["workers/tag"] // .metadata.annotations["workers/tag"]) == $tag
and (.annotations["workers/message"] // .metadata.annotations["workers/message"] // .metadata.message // .message) == $message))
| sort_by(.metadata.created_on // .created_on // .createdOn // "")
| last | .id // empty
' "$RUNNER_TEMP/bedaway-versions.json")"
test -n "$VERSION_ID"
{
echo "## Uploaded Worker version (not promoted)"
echo
echo "- Worker: `$WORKER`"
echo "- Version ID: `$VERSION_ID`"
echo "- Source SHA: `$SHA`"
echo "- Tag: `$TAG`"
echo
echo "Promotion requires a separate `workflow_dispatch` run with action `promote`, this exact version ID, and this exact source SHA."
} >> "$GITHUB_STEP_SUMMARY"

promote:
if: github.ref == 'refs/heads/main' && inputs.action == 'promote'
name: Promote explicitly reviewed Worker version
runs-on: ubuntu-latest
timeout-minutes: 20
environment:
name: bedside-and-away-production
env:
WORKER: bedside-and-away-backend
VERSION_ID: ${{ inputs.version_id }}
SOURCE_SHA: ${{ inputs.source_sha }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
WORKER_URL: ${{ vars.BEDAWAY_WORKER_URL }}
steps:
- uses: actions/checkout@v7

- uses: actions/setup-node@v7
with:
node-version: 24

- name: Install the repository-pinned cf version
run: |
set -euo pipefail
CF_VERSION="$(sed -n 's/^[[:space:]]*version = "\([^"]*\)";/\1/p' pkgs/cf/default.nix | head -n 1)"
test -n "$CF_VERSION"
npm install --global "cf@${CF_VERSION}" --no-audit --no-fund

- name: Verify exact version ID and source SHA before promotion
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
set -euo pipefail
[[ "$SOURCE_SHA" =~ ^[0-9a-f]{40}$ ]]
test -n "$VERSION_ID"
test -n "$CLOUDFLARE_API_TOKEN"
test -n "$CLOUDFLARE_ACCOUNT_ID"
test "$WORKER" = bedside-and-away-backend
TAG="git-${SOURCE_SHA:0:12}"
MESSAGE="deploy ${SOURCE_SHA} via protected GitHub Actions cf Versions workflow"
cf workers versions list --worker-id "$WORKER" > "$RUNNER_TEMP/bedaway-versions.json"
jq -e --arg id "$VERSION_ID" --arg tag "$TAG" --arg message "$MESSAGE" '
(if type == "array" then .
elif (.result | type) == "array" then .result
elif (.result.items | type) == "array" then .result.items
elif (.versions | type) == "array" then .versions
else error("unexpected cf versions list response") end)
| any(.[]; .id == $id and
(.annotations["workers/tag"] // .metadata.annotations["workers/tag"]) == $tag and
(.annotations["workers/message"] // .metadata.annotations["workers/message"] // .metadata.message // .message) == $message)
' "$RUNNER_TEMP/bedaway-versions.json" >/dev/null
export BEDAWAY_VERSION_ID="$VERSION_ID"
node --input-type=module <<'NODE'
const endpoint = `https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT_ID}/workers/scripts/${process.env.WORKER}/versions?deployable=true`;
const response = await fetch(endpoint, {
headers: { authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}` },
signal: AbortSignal.timeout(20_000),
});
if (!response.ok) throw new Error(`cf deployable-version check returned HTTP ${response.status}`);
const body = await response.json();
const rows = Array.isArray(body.result) ? body.result : body.result?.items;
if (!Array.isArray(rows) || !rows.some((row) => row.id === process.env.BEDAWAY_VERSION_ID)) {
throw new Error("reviewed version is absent from the deployable set; promotion refused");
}
NODE
{
echo "## Reviewed Worker version promotion"
echo
echo "- Worker: `$WORKER`"
echo "- Version ID: `$VERSION_ID`"
echo "- Source SHA: `$SOURCE_SHA`"
echo "- Tag: `$TAG`"
echo "- State: version ID, tag, full source message, and deployable status matched before promotion."
} >> "$GITHUB_STEP_SUMMARY"

- name: Create the 100% deployment for that exact version
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
set -euo pipefail
VERSIONS="[{\"version_id\":\"${VERSION_ID}\",\"percentage\":100}]"
cf workers deployments create --worker "$WORKER" --strategy percentage --versions "$VERSIONS"

- name: Verify active version and unsigned rejection
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
set -euo pipefail
cf workers deployments list --worker "$WORKER" > "$RUNNER_TEMP/bedaway-deployments.json"
node --input-type=module <<'NODE'
import fs from "node:fs";
const data = JSON.parse(fs.readFileSync(`${process.env.RUNNER_TEMP}/bedaway-deployments.json`, "utf8"));
const rows = Array.isArray(data) ? data : Array.isArray(data.result) ? data.result : Array.isArray(data.result?.items) ? data.result.items : data.deployments;
if (!Array.isArray(rows) || rows.length === 0) throw new Error("cf returned no deployments");
const time = (row) => {
const raw = row.metadata?.created_on ?? row.created_on ?? row.createdOn ?? 0;
return typeof raw === "number" ? raw : Date.parse(raw) || 0;
};
const latest = [...rows].sort((a, b) => time(b) - time(a))[0];
const active = (latest.versions ?? []).filter((v) => Number(v.percentage) > 0);
if (active.length !== 1 || active[0].version_id !== process.env.VERSION_ID || Number(active[0].percentage) !== 100) {
throw new Error("latest deployment does not serve the exact reviewed version at 100%");
}
NODE
node --input-type=module -e '
const u = new URL(process.env.WORKER_URL);
if (u.protocol !== "https:" || u.pathname !== "/" || u.search || u.hash ||
!u.hostname.startsWith(`${process.env.WORKER}.`) || !u.hostname.endsWith(".workers.dev")) {
throw new Error("BEDAWAY_WORKER_URL must be the production Worker workers.dev origin");
}
'
STATUS="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' --max-time 20 "${WORKER_URL}api/health")"
test "$STATUS" = 401 || { echo "unsigned Worker request returned HTTP $STATUS (expected 401)" >&2; exit 1; }
echo "Verified reviewed version ${VERSION_ID} at 100%; unsigned API request rejected with HTTP 401."
8 changes: 8 additions & 0 deletions pkgs/bedside-and-away/design/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ Use one authoritative Turso placement behind the existing private gateway. Retai
6. Verify deployed SHA/version, unsigned rejection, authenticated navigation, writes, version conflict handling and variants/steering on disposable data. Do not imply external research or Archer delivery from a saved request.
7. If a change fails, restore the prior gateway and Worker versions only if they can read the stored schema and honor all policy changes. If new ACLs were saved and old code cannot enforce them, freeze writes and keep the secured API in place pending repair. Never silently overwrite data written after the backup.

## Protected native cf release draft

`.github/workflows/bedside-and-away-release.yml` proposes a manual GitHub Actions route using the repository-pinned native `cf` CLI. It has separate protected environments for version upload and production promotion. The `upload` action only creates a version tagged `git-<sha12>` and records its exact version ID/full source SHA in the run summary. It does not create a deployment. A separate `promote` dispatch requires both values and verifies the exact version ID, tag, full-SHA source message, and deployable status before creating a 100% deployment. No upload run promotes automatically.

The workflow targets only `bedside-and-away-backend`, validates the existing workers.dev/preview URL settings and absence of custom routes, and never applies Worker triggers. It requires that the external Worker already exists; bootstrap remains on the current operator path. The earlier private Sites instance and its D1 copy remain separate; preview/test Workers and routes stay on separate targets with synthetic stores. The workers.dev API is internet-reachable and must reject unsigned requests. Configure required reviewers for both `bedside-and-away-version-upload` and `bedside-and-away-production` environments before using this path.

This Actions workflow is a draft release path. Keep the existing manual native-cf operator workflow available until the Actions route is verified through cutover. No live deployment, secret change, or Sites migration is implied by this draft.

The prior Sites D1 copy is a distinct installation and cannot be linked by nickname or browser ID. Two signed identities and an audited source/target mapping are prerequisites before moving any existing user content. Rotation and backups need configured operators and verified evidence; no automatic rotations or new background jobs are enabled here.
8 changes: 7 additions & 1 deletion pkgs/bedside-and-away/external/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ Primary protocol reference: https://github.com/tursodatabase/turso/blob/main/ser

## Native cf deployment

Use the native `cf` binary, not Wrangler. `node external/cf-build.mjs` writes Cloudflare's prebuilt v0 output from the staged source, using non-secret TURSO_URL and GATEWAY_ORIGIN. Run `cf deploy --prebuilt --dry-run --provision=false` before any upload. The Worker route must reject all unsigned requests. Never replace an existing unrelated Worker or Tailscale listener; require exact project and host ownership before enabling the app listener. Verify against a real authenticated Tailnet client and confirm the old Site tables are still empty before cutover. The only browser-local state on the old app is device UUID and remembered store selection; it is not an account key or private draft storage. The new origin intentionally creates a new browser ID.
Use the native `cf` binary, not Wrangler. `node external/cf-build.mjs` writes Cloudflare's prebuilt v0 output from the staged source, using non-secret TURSO_URL and GATEWAY_ORIGIN. Run `cf deploy --prebuilt --dry-run --provision=false` before any upload. This existing manual operator path remains available until a protected Actions release has been verified through cutover; the workflow below does not replace it yet. The Worker route must reject all unsigned requests. Never replace an existing unrelated Worker or Tailscale listener; require exact project and host ownership before enabling the app listener. Verify against a real authenticated Tailnet client and confirm the old Site tables are still empty before cutover. The only browser-local state on the old app is device UUID and remembered store selection; it is not an account key or private draft storage. The new origin intentionally creates a new browser ID.

### Protected Actions release draft

`.github/workflows/bedside-and-away-release.yml` is a manual-only proposed release route, not yet the fallback operator path. It uses the `cf` version pinned in `pkgs/cf/default.nix` and the current external Worker manifest. Configure the `bedside-and-away-version-upload` and `bedside-and-away-production` GitHub Environments with required reviewers before relying on either gate. The upload environment needs the Cloudflare account ID and API token plus the credential-free Turso URL and gateway origin; the production environment needs the account ID, API token and workers.dev origin. Upload and promotion are distinct runs: `upload` creates a SHA-tagged version and reports its exact ID, but never deploys it. A separate `promote` dispatch requires that version ID and the full source SHA; it verifies the ID, tag, full-SHA message and deployable status before creating a 100% deployment. Promotion reviewers can inspect the dispatch's exact version ID and source SHA before approving the production environment.

The workflow requires the external Worker to exist already; it does not bootstrap or replace an unrelated Worker. It does not apply Worker triggers/routes, touch the prior private Sites instance or its D1 data, or deploy preview/test Workers. Existing production secrets are referenced as secret bindings, not placed in repository files or Actions logs. Keep preview/test Workers on distinct names with synthetic databases. The workers.dev API hostname remains internet-reachable and must reject unsigned requests. Continue using the existing manual operator fallback, including for bootstrap, until workflow and cutover evidence is verified.