From 0b76ff1c9c2f9edf53b5ca675a760d8a89b79e74 Mon Sep 17 00:00:00 2001 From: Rahul Varghese Date: Sat, 2 May 2026 19:29:44 +0530 Subject: [PATCH] feat: add Cloudflare deployment readiness to Worker and Pages compositions - Add preDeployCommand, migrationCommand, and smokeCommand inputs to cloudflare-worker-turbo and cloudflare-worker compositions - Accept wrangler.toml alongside wrangler.jsonc in cloudflare-worker-turbo (already fixed in cloudflare-worker) - Add wranglerConfig input for explicit config file specification - Add clear CI annotations (DRY-RUN, SKIPPED, LIVE DEPLOY, DEPLOY SUCCESS) to all Cloudflare deploy steps to prevent false-green silent skips - Validate Cloudflare credentials explicitly before attempting deploy - Add smokeCommand input to cloudflare-pages-turbo for post-deploy verification - Add docs/production-deploys.md documenting the --changed zero-job trap and recommended patterns for reliable production deployments - Bump stack version to 0.12.0 - Regenerate registry/index.json --- README.md | 1 + .../cloudflare-pages-turbo/compositions.yaml | 19 +++- .../cloudflare-worker-turbo/compositions.yaml | 48 ++++++++- .../api-edge/apps/api-edge/component.yaml | 10 +- .../tests/smoke/component.yaml | 2 +- .../cloudflare-worker/compositions.yaml | 46 ++++++++- docs/production-deploys.md | 98 +++++++++++++++++++ registry/index.json | 4 +- stack.yaml | 2 +- 9 files changed, 218 insertions(+), 12 deletions(-) create mode 100644 docs/production-deploys.md diff --git a/README.md b/README.md index 1310648..dfb63f8 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ Every composition now lives under `compositions//` so docs, examples, test - [Core concepts](docs/concepts.md) - [Authoring guide](docs/authoring.md) - [Verification model](docs/verification.md) +- [Production deploys](docs/production-deploys.md) - [Roadmap](docs/roadmap.md) - [Using this stack from OCI](docs/using-this-stack-from-oci.md) diff --git a/compositions/cloudflare-pages-turbo/compositions.yaml b/compositions/cloudflare-pages-turbo/compositions.yaml index 14f6897..ab3d0a6 100644 --- a/compositions/cloudflare-pages-turbo/compositions.yaml +++ b/compositions/cloudflare-pages-turbo/compositions.yaml @@ -53,6 +53,9 @@ spec: buildCommand: type: string description: Override the default turbo build command + smokeCommand: + type: string + description: Post-deploy verification command (e.g. curl the live Pages URL) additionalProperties: false additionalProperties: true jobs: @@ -102,12 +105,15 @@ spec: shell: bash run: | if [ "{{.Environment}}" != "production" ] || [ "${GITHUB_REF:-}" != "refs/heads/{{.productionBranch}}" ]; then - echo "Skipping Cloudflare Pages project reconciliation for {{.Component}} in {{.Environment}} on ${GITHUB_REF:-local}." + echo "::notice::SKIPPED: Cloudflare Pages project reconciliation for {{.Component}} in {{.Environment}} on ${GITHUB_REF:-local} (not production branch)." exit 0 fi + echo "::group::Validate Cloudflare credentials" : "${CLOUDFLARE_ACCOUNT_ID:?CLOUDFLARE_ACCOUNT_ID is required for deployment}" : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN is required for deployment}" + echo "CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN are set." + echo "::endgroup::" api_root="https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/pages/projects" response_file="${RUNNER_TEMP:-/tmp}/cloudflare-pages-project.json" @@ -140,14 +146,23 @@ spec: shell: bash run: | if [ "{{.Environment}}" != "production" ] || [ "${GITHUB_REF:-}" != "refs/heads/{{.productionBranch}}" ]; then - echo "Skipping Cloudflare Pages deploy for {{.Component}} in {{.Environment}} on ${GITHUB_REF:-local}." + echo "::notice::SKIPPED: Cloudflare Pages deploy for {{.Component}} in {{.Environment}} on ${GITHUB_REF:-local} (not production branch)." exit 0 fi : "${CLOUDFLARE_ACCOUNT_ID:?CLOUDFLARE_ACCOUNT_ID is required for deployment}" : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN is required for deployment}" + echo "::notice::LIVE DEPLOY: Uploading {{.Component}} to Cloudflare Pages from ${GITHUB_REF:-}." pnpm exec wrangler pages deploy "{{.outputDir}}" \ --project-name "{{.projectName}}" \ --branch "{{.productionBranch}}" + + {{if .smokeCommand}} + echo "::group::Post-deploy smoke test" + {{.smokeCommand}} + echo "::endgroup::" + {{end}} + + echo "::notice::DEPLOY SUCCESS: {{.Component}} deployed to Cloudflare Pages." onFailure: stop diff --git a/compositions/cloudflare-worker-turbo/compositions.yaml b/compositions/cloudflare-worker-turbo/compositions.yaml index ead3486..cca06fc 100644 --- a/compositions/cloudflare-worker-turbo/compositions.yaml +++ b/compositions/cloudflare-worker-turbo/compositions.yaml @@ -39,6 +39,9 @@ spec: productionBranch: type: string description: Branch that is allowed to deploy the Worker app + wranglerConfig: + type: string + description: Wrangler config filename (defaults to detecting wrangler.jsonc or wrangler.toml) preBuildCommand: type: string description: Optional command to run before the build (e.g. codegen, schema generation) @@ -48,9 +51,18 @@ spec: typecheckCommand: type: string description: Override the default turbo typecheck command + preDeployCommand: + type: string + description: Command to run before deploy (e.g. resource provisioning, D1 database creation) + migrationCommand: + type: string + description: Command to run D1 migrations or other pre-deploy schema changes deployCommand: type: string description: Override the default deploy command + smokeCommand: + type: string + description: Post-deploy verification command (e.g. curl the live endpoint) additionalProperties: false additionalProperties: true jobs: @@ -88,7 +100,11 @@ spec: shell: bash run: | test -f package.json - test -f wrangler.jsonc + {{if .wranglerConfig}} + test -f "{{.wranglerConfig}}" + {{else}} + test -f wrangler.jsonc || test -f wrangler.toml + {{end}} onFailure: stop - name: build-worker run: "{{if .buildCommand}}{{.buildCommand}}{{else}}pnpm exec turbo run build --filter=./{{end}}" @@ -100,18 +116,44 @@ spec: shell: bash run: | if [ "{{.Environment}}" = "dev" ]; then - echo "Running wrangler deploy dry-run for {{.Component}} in dev environment." + echo "::notice::DRY-RUN: Running wrangler deploy --dry-run for {{.Component}} in dev environment." pnpm exec wrangler deploy --dry-run exit 0 fi if [ "{{.Environment}}" != "production" ] || [ "${GITHUB_REF:-}" != "refs/heads/{{.productionBranch}}" ]; then - echo "Skipping Cloudflare Worker deploy for {{.Component}} in {{.Environment}} on ${GITHUB_REF:-local}." + echo "::notice::SKIPPED: Cloudflare Worker deploy for {{.Component}} in {{.Environment}} on ${GITHUB_REF:-local} (not production branch)." exit 0 fi + echo "::group::Validate Cloudflare credentials" : "${CLOUDFLARE_ACCOUNT_ID:?CLOUDFLARE_ACCOUNT_ID is required for deploy}" : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN is required for deploy}" + echo "CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN are set." + echo "::endgroup::" + {{if .preDeployCommand}} + echo "::group::Pre-deploy" + {{.preDeployCommand}} + echo "::endgroup::" + {{end}} + + {{if .migrationCommand}} + echo "::group::Run migrations" + {{.migrationCommand}} + echo "::endgroup::" + {{end}} + + echo "::group::Deploy Worker" + echo "::notice::LIVE DEPLOY: Deploying {{.Component}} to Cloudflare from ${GITHUB_REF:-}." {{if .deployCommand}}{{.deployCommand}}{{else}}pnpm run deploy{{end}} + echo "::endgroup::" + + {{if .smokeCommand}} + echo "::group::Post-deploy smoke test" + {{.smokeCommand}} + echo "::endgroup::" + {{end}} + + echo "::notice::DEPLOY SUCCESS: {{.Component}} deployed to Cloudflare." onFailure: stop diff --git a/compositions/cloudflare-worker-turbo/examples/api-edge/apps/api-edge/component.yaml b/compositions/cloudflare-worker-turbo/examples/api-edge/apps/api-edge/component.yaml index ae4d423..63644a4 100644 --- a/compositions/cloudflare-worker-turbo/examples/api-edge/apps/api-edge/component.yaml +++ b/compositions/cloudflare-worker-turbo/examples/api-edge/apps/api-edge/component.yaml @@ -16,8 +16,16 @@ spec: nodeVersion: "20" pnpmVersion: "10.12.1" productionBranch: main + # Optional: specify wrangler config file explicitly + # wranglerConfig: wrangler.jsonc + # Optional: run D1 migrations before deploy + # migrationCommand: "pnpm exec wrangler d1 migrations apply my-db --remote" + # Optional: provision resources before deploy + # preDeployCommand: "./scripts/bootstrap-cloudflare.sh" + # Optional: verify live endpoint after deploy + # smokeCommand: "curl -sf https://my-api.workers.dev/health" labels: team: platform layer: runtime surface: api - runtime: cloudflare \ No newline at end of file + runtime: cloudflare diff --git a/compositions/cloudflare-worker-turbo/tests/smoke/component.yaml b/compositions/cloudflare-worker-turbo/tests/smoke/component.yaml index ae4d423..f14d397 100644 --- a/compositions/cloudflare-worker-turbo/tests/smoke/component.yaml +++ b/compositions/cloudflare-worker-turbo/tests/smoke/component.yaml @@ -20,4 +20,4 @@ spec: team: platform layer: runtime surface: api - runtime: cloudflare \ No newline at end of file + runtime: cloudflare diff --git a/compositions/cloudflare-worker/compositions.yaml b/compositions/cloudflare-worker/compositions.yaml index a760d2b..d6278e4 100644 --- a/compositions/cloudflare-worker/compositions.yaml +++ b/compositions/cloudflare-worker/compositions.yaml @@ -53,6 +53,18 @@ spec: productionBranch: type: string description: Branch that is allowed to deploy the Worker app + wranglerConfig: + type: string + description: Wrangler config filename (defaults to detecting wrangler.jsonc or wrangler.toml) + preDeployCommand: + type: string + description: Command to run before deploy (e.g. resource provisioning, D1 database creation) + migrationCommand: + type: string + description: Command to run D1 migrations or other pre-deploy schema changes + smokeCommand: + type: string + description: Post-deploy verification command (e.g. curl the live endpoint) additionalProperties: false additionalProperties: true jobs: @@ -81,7 +93,11 @@ spec: shell: bash run: | test -f package.json + {{if .wranglerConfig}} + test -f "{{.wranglerConfig}}" + {{else}} test -f wrangler.jsonc || test -f wrangler.toml + {{end}} onFailure: stop - name: build-worker run: "{{.buildCommand}}" @@ -99,18 +115,44 @@ spec: shell: bash run: | if [ "{{.Environment}}" = "dev" ]; then - echo "Running wrangler deploy dry-run for {{.Component}} in dev environment." + echo "::notice::DRY-RUN: Running wrangler deploy --dry-run for {{.Component}} in dev environment." pnpm exec wrangler deploy --dry-run exit 0 fi if [ "{{.Environment}}" != "production" ] || [ "${GITHUB_REF:-}" != "refs/heads/{{.productionBranch}}" ]; then - echo "Skipping Cloudflare Worker deploy for {{.Component}} in {{.Environment}} on ${GITHUB_REF:-local}." + echo "::notice::SKIPPED: Cloudflare Worker deploy for {{.Component}} in {{.Environment}} on ${GITHUB_REF:-local} (not production branch)." exit 0 fi + echo "::group::Validate Cloudflare credentials" : "${CLOUDFLARE_ACCOUNT_ID:?CLOUDFLARE_ACCOUNT_ID is required for deploy}" : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN is required for deploy}" + echo "CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN are set." + echo "::endgroup::" + {{if .preDeployCommand}} + echo "::group::Pre-deploy" + {{.preDeployCommand}} + echo "::endgroup::" + {{end}} + + {{if .migrationCommand}} + echo "::group::Run migrations" + {{.migrationCommand}} + echo "::endgroup::" + {{end}} + + echo "::group::Deploy Worker" + echo "::notice::LIVE DEPLOY: Deploying {{.Component}} to Cloudflare from ${GITHUB_REF:-}." {{.deployCommand}} + echo "::endgroup::" + + {{if .smokeCommand}} + echo "::group::Post-deploy smoke test" + {{.smokeCommand}} + echo "::endgroup::" + {{end}} + + echo "::notice::DEPLOY SUCCESS: {{.Component}} deployed to Cloudflare." onFailure: stop diff --git a/docs/production-deploys.md b/docs/production-deploys.md new file mode 100644 index 0000000..71f813c --- /dev/null +++ b/docs/production-deploys.md @@ -0,0 +1,98 @@ +# Production deploys + +This document explains how to configure reliable production deployments using stack-tectonic compositions on the production branch. + +## The `--changed` trap + +When using `orun run --changed` on production branch pushes (e.g. after a squash merge to `main`), the change detection compares the merge commit against its parent. Because a squash merge produces a single commit whose parent is the previous `main` HEAD, and the new commit already includes all changes, `--changed` may resolve to zero changed components: + +```text +0 components x 3 envs -> 0 jobs +``` + +This produces a **green CI run with zero deployments** — a false positive that masks the absence of any live deployment. + +## Recommended patterns + +### Pattern 1: Run all production jobs on main (recommended for small repos) + +Remove `--changed` from the production branch push step: + +```yaml +- name: Execute + env: + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + run: kiox -- orun run +``` + +This ensures every deployable component runs its production job on every push to main. For repos with fewer than 10 components, this adds minimal overhead. + +### Pattern 2: Changed-only with correct base ref + +If the repo has many components and full runs are expensive, pass explicit base/head refs: + +```yaml +- name: Execute + env: + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + run: kiox -- orun run --changed --base "${{ github.event.before }}" --head "${{ github.sha }}" +``` + +Using `github.event.before` as the base ensures the diff covers the actual changes introduced by the merge. + +### Pattern 3: Separate deploy workflow on main + +Keep `--changed` for PR validation but add a dedicated production deploy step: + +```yaml +jobs: + deploy: + if: github.ref == 'refs/heads/main' && github.event_name == 'push' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: sourceplane/kiox-action@v2.1.2 + with: + version: v0.4.3 + - name: Deploy all production components + env: + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + run: kiox -- orun run --env production +``` + +## Composition behavior + +Stack-tectonic Cloudflare compositions (`cloudflare-worker-turbo`, `cloudflare-worker`, `cloudflare-pages-turbo`, `cloudflare-pages`) all follow the same deploy semantics: + +| Condition | Behavior | CI annotation | +| --- | --- | --- | +| Environment = `dev` | Dry-run only | `DRY-RUN` | +| Environment != `production` or branch != `productionBranch` | Skip deploy | `SKIPPED` | +| Environment = `production` AND branch = `productionBranch` AND credentials present | Live deploy | `LIVE DEPLOY` → `DEPLOY SUCCESS` | +| Credentials missing on production deploy | Hard failure | Step fails with clear error | + +## Verifying a deploy actually happened + +After a production push, check CI logs for: +- `::notice::LIVE DEPLOY: Deploying to Cloudflare` +- `::notice::DEPLOY SUCCESS: deployed to Cloudflare` + +If you see only `SKIPPED` or `DRY-RUN` annotations, the deploy did not run. + +A green run with `0 components x 3 envs -> 0 jobs` means **no deployment occurred**. This is never evidence of a successful deploy. + +## Consumer repo checklist + +Before relying on production deploys: + +1. Set `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN` as GitHub repository secrets +2. Ensure your workflow passes these as environment variables to the `orun run` step +3. Choose one of the patterns above for your production branch push behavior +4. Replace `PLACEHOLDER` database IDs in `wrangler.jsonc` with real resource IDs +5. If using D1, configure `migrationCommand` in your `component.yaml` inputs +6. Optionally add `smokeCommand` to verify the live endpoint after deploy diff --git a/registry/index.json b/registry/index.json index b7ba696..35d5348 100644 --- a/registry/index.json +++ b/registry/index.json @@ -2,8 +2,8 @@ "stack": { "name": "stack-tectonic", "title": "Stack Tectonic", - "version": "0.11.0", - "oci": "oci://ghcr.io/sourceplane/stack-tectonic:0.11.0" + "version": "0.12.0", + "oci": "oci://ghcr.io/sourceplane/stack-tectonic:0.12.0" }, "catalog": { "compositions": [ diff --git a/stack.yaml b/stack.yaml index 942972d..c4d5159 100644 --- a/stack.yaml +++ b/stack.yaml @@ -3,7 +3,7 @@ kind: Stack metadata: name: stack-tectonic title: Stack Tectonic - version: 0.11.0 + version: 0.12.0 description: Versioned OCI-hosted catalog of platform compositions and blueprints for Sourceplane repositories. owner: sourceplane tags: