diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 640c999..70b45e2 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -12,6 +12,20 @@ on: - ".github/workflows/python.yml" - "pyproject.toml" - "uv.lock" + push: + branches: [main] + paths: + - "kypp/**" + - "tests/**" + - "worker/src/**" + - "worker/test/**" + - "worker/migrations/**" + - "docs/features/**" + - "scripts/featuremap.py" + - ".github/workflows/python.yml" + - "pyproject.toml" + - "uv.lock" + workflow_call: workflow_dispatch: concurrency: group: python-${{ github.ref }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..862e543 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,41 @@ +name: release +# A v* tag is the release: once both test suites pass on the tagged commit, this job deploys +# kypp.vu1n.dev. A push to main never deploys. +on: + push: + tags: ["v*"] +concurrency: + group: release + cancel-in-progress: false +jobs: + python: + uses: ./.github/workflows/python.yml + worker: + uses: ./.github/workflows/worker.yml + deploy: + needs: [python, worker] + runs-on: ubuntu-latest + permissions: + contents: write + id-token: write # the job's OIDC token is its tumbler caller (token row kypp-release) + defaults: + run: + working-directory: worker + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: "22.21" + - run: npm ci + - name: deploy kypp.vu1n.dev + env: + TUMBLER_URL: https://tumbler.vu1n.dev + run: | + curl -fsSL "$TUMBLER_URL/cli" -o "$RUNNER_TEMP/tumbler" && chmod +x "$RUNNER_TEMP/tumbler" + export TUMBLER_TOKEN=$(curl -fsS -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \ + "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=$TUMBLER_URL" | jq -r .value) + "$RUNNER_TEMP/tumbler" run -p kypp --scope workers:write -k CLOUDFLARE_API_TOKEN,CLOUDFLARE_ACCOUNT_ID -- npm run deploy:production + - name: GitHub release + env: + GH_TOKEN: ${{ github.token }} + run: gh release view "$GITHUB_REF_NAME" >/dev/null 2>&1 || gh release create "$GITHUB_REF_NAME" --verify-tag --generate-notes --title "$GITHUB_REF_NAME" diff --git a/.github/workflows/worker.yml b/.github/workflows/worker.yml index b627a8f..dea516a 100644 --- a/.github/workflows/worker.yml +++ b/.github/workflows/worker.yml @@ -5,6 +5,7 @@ on: push: branches: [main] paths: ["worker/**", ".github/workflows/worker.yml"] + workflow_call: jobs: test: runs-on: ubuntu-latest diff --git a/docs/features/hosted-worker.md b/docs/features/hosted-worker.md index 932bc8e..5966787 100644 --- a/docs/features/hosted-worker.md +++ b/docs/features/hosted-worker.md @@ -74,6 +74,8 @@ TypeSafe API's shape, for GitHub Actions OIDC tokens (`worker/src/oidc.ts`) and `env.production`, which repeats every binding and var because envs don't inherit them. Change one, change both. Deploy the install only with `npm run deploy:production`: a plain `wrangler deploy` targets the template and turns workers.dev back on. +- kypp.vu1n.dev deploys only from `release.yml` on a `v*` tag whose tests passed. `deploy:production` + skips under Workers Builds (`WORKERS_CI`), which still builds main, so a push to main deploys nothing. - The OAuth resource id is exactly `/mcp`. A path or query suffix on the MCP URL breaks token audience checks, so the project travels as a tool argument or the `X-Kypp-Project` header. diff --git a/worker/README.md b/worker/README.md index ecfdbdc..c894301 100644 --- a/worker/README.md +++ b/worker/README.md @@ -158,9 +158,12 @@ npm run deploy # applies migrations, then deploys; The owner's install (`kypp.vu1n.dev`) is the `production` environment in `wrangler.jsonc`: its D1 and KV ids, custom domain, `workers_dev: false` and vars are committed there, and the top level stays the template the button copies. Deploy it with `npm run deploy:production` (migrations, then -`wrangler deploy --env production`). To deploy it on every push to main without keeping a Cloudflare -token in GitHub, connect the Worker to the repo with Workers Builds (Worker > Settings > Builds): root -`worker`, deploy command `npm run deploy:production`, no build variables. A deploy without +`wrangler deploy --env production`). It deploys on release, not on every push: a `v*` tag runs +`.github/workflows/release.yml`, which runs both test suites on the tag and then `npm run +deploy:production` with a Cloudflare key from tumbler. The job's GitHub OIDC token is its caller (token +row `kypp-release`, bound to `repo:vu1n/kypp:ref:refs/tags/v*`), so no Cloudflare token lives in GitHub. +`deploy:production` refuses anything but a clean v* checkout, and under Workers Builds (still connected, +building main) it skips. Roll back by tagging a revert. A deploy without `--env production` targets the template and would turn workers.dev back on, so don't run plain `wrangler deploy` against this install. For your own install, add your own env block the same way. diff --git a/worker/deploy-production.mjs b/worker/deploy-production.mjs new file mode 100644 index 0000000..e05ae0f --- /dev/null +++ b/worker/deploy-production.mjs @@ -0,0 +1,16 @@ +// `npm run deploy:production`: deploys kypp.vu1n.dev, only from a v* tag (release.yml runs it). +import { execFileSync } from "node:child_process"; + +// Workers Builds still runs this on every push to main; a push to main must not deploy. +if (process.env.WORKERS_CI) { + console.log("kypp: skipped. Production deploys from v* tags (.github/workflows/release.yml), not from main."); + process.exit(0); +} +const git = (...a) => execFileSync("git", a, { encoding: "utf8" }).trim(); +const tag = git("tag", "--points-at", "HEAD", "--list", "v*").split("\n")[0]; +if (!tag) { console.error("kypp: HEAD is not a v* tag; production deploys only a release"); process.exit(1); } +if (git("status", "--porcelain")) { console.error(`kypp: ${tag} has uncommitted or untracked files (wrangler would deploy or migrate them too)`); process.exit(1); } +console.log(`kypp: deploying ${tag}`); +const run = (...a) => execFileSync("npx", ["wrangler", ...a], { stdio: "inherit" }); +run("d1", "migrations", "apply", "DB", "--remote", "--env", "production"); +run("deploy", "--env", "production"); diff --git a/worker/package.json b/worker/package.json index 70e3e40..eafe0f6 100644 --- a/worker/package.json +++ b/worker/package.json @@ -9,7 +9,7 @@ "typecheck": "tsc --noEmit", "test": "node --test --experimental-strip-types test/*.test.ts", "db:migrate": "wrangler d1 migrations apply DB --remote", - "deploy:production": "wrangler d1 migrations apply DB --remote --env production && wrangler deploy --env production" + "deploy:production": "node deploy-production.mjs" }, "dependencies": { "@cloudflare/workers-oauth-provider": "^1.2.1",