-
Notifications
You must be signed in to change notification settings - Fork 6
Deploy Worker on release with post-deploy smoke test + rollback #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| name: Deploy to Cloudflare | ||
|
|
||
| # Deploys the Worker when a new version is released (craft publishes the GitHub | ||
| # Release), so production always tracks a released tag — never raw main. Also | ||
| # runnable manually via workflow_dispatch. | ||
| on: | ||
| release: | ||
| types: [published] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| deploy: | ||
| name: Deploy to Cloudflare | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: pnpm/action-setup@v4 | ||
|
|
||
| - uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: "22" | ||
| cache: pnpm | ||
|
|
||
| - run: pnpm install --frozen-lockfile | ||
|
|
||
| # `pnpm run deploy` runs wrangler deploy + the Sentry release & sourcemap | ||
| # upload. wrangler and sentry-cli both read their credentials from the env | ||
| # vars below, so no interactive login is needed in CI. | ||
| - name: Deploy worker | ||
| id: deploy | ||
| run: pnpm run deploy | ||
| env: | ||
| CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
| CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
| SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }} | ||
|
|
||
| - name: Wait for propagation | ||
| if: steps.deploy.outcome == 'success' | ||
| run: sleep 15 | ||
|
|
||
| # Assert the live worker answers the MCP handshake and reports the version | ||
| # we just released. Fails the job (→ rollback) on any mismatch. | ||
| - name: Smoke test | ||
| id: smoke | ||
| if: steps.deploy.outcome == 'success' | ||
| run: node bin/smoke.mjs "https://plausible-mcp.sentry.dev/mcp" "$(node -p "require('./package.json').version")" | ||
|
|
||
| - name: Roll back on smoke failure | ||
| if: steps.smoke.outcome == 'failure' | ||
| uses: cloudflare/wrangler-action@v3 | ||
| continue-on-error: true | ||
| with: | ||
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
| accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
| command: rollback | ||
| packageManager: pnpm | ||
|
|
||
| - name: Fail the job if smoke test failed | ||
| if: steps.smoke.outcome == 'failure' | ||
| run: exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #!/usr/bin/env node | ||
| // Post-deploy smoke test for the Cloudflare Worker. | ||
| // Runs the MCP `initialize` handshake against the live /mcp endpoint and asserts | ||
| // the worker is reachable and reports the expected version. Exits non-zero on | ||
| // failure so CI can roll back. | ||
| // | ||
| // Scope: initialize only. It confirms the deploy is live and running the right | ||
| // build. It deliberately does NOT call tools/list — that path validates the | ||
| // bearer as a Plausible API key, so it would need a real key + live site to | ||
| // exercise, and tool registration is already covered by the unit tests. | ||
| // | ||
| // Usage: node bin/smoke.mjs [url] [expectedVersion] | ||
| // url defaults to $SMOKE_URL or https://plausible-mcp.sentry.dev/mcp | ||
| // expectedVersion defaults to $SMOKE_EXPECTED_VERSION (optional; skipped if unset) | ||
|
|
||
| const URL = | ||
| process.argv[2] || process.env.SMOKE_URL || "https://plausible-mcp.sentry.dev/mcp"; | ||
| const EXPECTED = process.argv[3] || process.env.SMOKE_EXPECTED_VERSION || null; | ||
| // initialize never calls Plausible, so any bearer works for a liveness check. | ||
| const BEARER = process.env.SMOKE_BEARER || "smoke-test"; | ||
|
|
||
| function fail(msg) { | ||
| console.error(`SMOKE FAIL: ${msg}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| console.log(`Smoke testing ${URL}${EXPECTED ? ` (expecting v${EXPECTED})` : ""}`); | ||
|
|
||
| const res = await fetch(URL, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Accept: "application/json, text/event-stream", | ||
| Authorization: `Bearer ${BEARER}`, | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: "2.0", | ||
| id: 1, | ||
| method: "initialize", | ||
| params: { | ||
| protocolVersion: "2025-11-25", | ||
| capabilities: {}, | ||
| clientInfo: { name: "ci-smoke", version: "1" }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| if (!res.ok) fail(`initialize returned HTTP ${res.status}`); | ||
|
|
||
| // Response is an SSE stream ("data: {json}"); pull the JSON-RPC frame out. | ||
| const text = await res.text(); | ||
| const frame = text | ||
| .split("\n") | ||
| .map((l) => l.match(/^data:\s*(.+)$/)?.[1]) | ||
| .filter(Boolean) | ||
| .map((j) => { | ||
| try { | ||
| return JSON.parse(j); | ||
| } catch { | ||
| return null; | ||
| } | ||
| }) | ||
| .find((m) => m?.id === 1); | ||
|
|
||
| const serverInfo = frame?.result?.serverInfo; | ||
| if (!serverInfo) fail(`no serverInfo in response: ${text.slice(0, 200)}`); | ||
| console.log(`serverInfo: ${serverInfo.name} v${serverInfo.version}`); | ||
| if (serverInfo.name !== "plausible-mcp") fail(`unexpected name "${serverInfo.name}"`); | ||
| if (EXPECTED && serverInfo.version !== EXPECTED) | ||
| fail(`version mismatch: live=${serverInfo.version} expected=${EXPECTED}`); | ||
|
|
||
| console.log("SMOKE OK"); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The server version is hardcoded in
src/server.ts, but the smoke test compares it against the dynamic version frompackage.json, which will cause future deployment failures.Severity: CRITICAL
Suggested Fix
To prevent future deployment failures, replace the hardcoded version string in
src/server.ts. Import theversionfrompackage.jsondirectly intosrc/server.tsso that the server always reports the correct, current version. This ensures the smoke test will pass after a version bump.Prompt for AI Agent
Also affects:
src/server.tsDid we get this right? 👍 / 👎 to inform future reviews.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validated as a false positive — no change needed.
src/server.tsandpackage.jsondon't drift:bin/bump-version.sh(added in #23, craft'spreReleaseCommand)seds the released version intosrc/server.tson every release, with agrep -qguard that fails the release loudly if the substitution doesn't take.deploy.ymltriggers on the release tag, so the checked-out commit always hasserver.ts == package.json(confirmed: both0.4.0on main today).So the smoke test comparing the live
serverInfo.versiontopackage.jsonis an intentional end-to-end check on that sync — if it ever broke, the smoke fails and the deploy rolls back. That's the guard working as designed, not a latent failure.The suggested fix (import the version from
package.jsonintoserver.ts) is the approach we deliberately rejected in #23: the samecreateServer()is bundled into the Cloudflare Worker, which has no filesystem, and a JSON import breaks thetscrootDirlayout. The build-time sed is what works for both targets.