From 38a01a97364a26d46c4c0a4b6117a88934251e84 Mon Sep 17 00:00:00 2001 From: Den Delimarsky Date: Wed, 11 Mar 2026 20:18:49 +0000 Subject: [PATCH 1/2] Fix README example: use wrangler directly, not deploy action The cloudflare-pages-preview/deploy action reads context.payload.pull_request.number unconditionally in its comment step, which throws on workflow_dispatch. The previous example would crash. Updated to call wrangler-action directly and post the comment manually. --- slash-commands/README.md | 43 +++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/slash-commands/README.md b/slash-commands/README.md index 80b3628..90684c2 100644 --- a/slash-commands/README.md +++ b/slash-commands/README.md @@ -172,37 +172,48 @@ jobs: # ... your blog build steps → output to blog/public ... + # cloudflare-pages-preview/deploy cannot be used here — its comment + # step reads context.payload.pull_request.number, which is undefined + # on workflow_dispatch and will throw. Call wrangler directly: + - name: Inject noindex + run: | + echo -e '/*\n X-Robots-Tag: noindex, nofollow, noarchive, nosnippet' > blog/public/_headers + find blog/public -name '*.html' -print0 | \ + xargs -0 -r sed -i 's|||' + - id: deploy - uses: modelcontextprotocol/actions/cloudflare-pages-preview/deploy@main + uses: cloudflare/wrangler-action@v3 with: - directory: blog/public - project-name: mcp-blog-preview - api-token: ${{ secrets.CF_PAGES_PREVIEW_API_TOKEN }} - account-id: ${{ secrets.CF_PAGES_PREVIEW_ACCOUNT_ID }} - branch: pr-${{ inputs.pr_number }} + apiToken: ${{ secrets.CF_PAGES_PREVIEW_API_TOKEN }} + accountId: ${{ secrets.CF_PAGES_PREVIEW_ACCOUNT_ID }} + command: pages deploy blog/public --project-name=mcp-blog-preview --branch=pr-${{ inputs.pr_number }} - # NOTE: cloudflare-pages-preview/deploy reads the PR number from - # context.payload.pull_request, which is undefined on workflow_dispatch. - # Post the preview comment yourself: - uses: actions/github-script@v8 env: PR_NUMBER: ${{ inputs.pr_number }} - PREVIEW_URL: ${{ steps.deploy.outputs.url }} + HEAD_SHA: ${{ inputs.head_sha }} + DEPLOY_URL: ${{ steps.deploy.outputs.deployment-url }} + ALIAS_URL: ${{ steps.deploy.outputs.pages-deployment-alias-url }} with: script: | await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: Number(process.env.PR_NUMBER), - body: `Blog preview: ${process.env.PREVIEW_URL}`, + body: `## Blog Preview (staged via /stageblog)\n\n` + + `| | |\n|---|---|\n` + + `| **Preview (stable)** | ${process.env.ALIAS_URL} |\n` + + `| **This commit** | ${process.env.DEPLOY_URL} |\n` + + `| **Commit** | \`${process.env.HEAD_SHA.slice(0,7)}\` |\n\n` + + `_Includes drafts and future-dated posts. noindex headers set._`, }); ``` -> **Known limitation:** `cloudflare-pages-preview/deploy` currently assumes -> `context.payload.pull_request` exists for its sticky-comment step, which is -> not the case on `workflow_dispatch`. Until the deploy action accepts an -> explicit `pr-number` input, post the preview comment from the caller -> workflow as shown above. (Fixing deploy is a follow-up.) +> **Known limitation:** `cloudflare-pages-preview/deploy` currently reads +> `context.payload.pull_request.number` unconditionally in its comment step, +> which throws on `workflow_dispatch`. Until the deploy action accepts an +> explicit `pr-number` input, bypass it and call `cloudflare/wrangler-action` +> directly as shown above. (Fixing deploy is a follow-up.) ## Inputs From a4bb78216adb16dfa9bc43db8926c65a229ee510 Mon Sep 17 00:00:00 2001 From: Den Delimarsky Date: Wed, 11 Mar 2026 20:25:04 +0000 Subject: [PATCH 2/2] Add pr-number input to cloudflare-pages-preview actions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both deploy and cleanup previously read context.payload.pull_request.number directly, which throws on workflow_dispatch and other non-PR triggers. New optional inputs: - deploy: pr-number (default: event PR number), commit-sha (default: github.sha) - cleanup: pr-number (default: event PR number) If pr-number resolves to empty/0, the comment step is skipped gracefully instead of crashing. This lets /stageblog's companion workflow_dispatch receiver call the composite deploy action directly instead of inlining wrangler + noindex injection. The noindex logic staying in the actions repo (rather than in a script file inside the caller repo that gets checked out at the fork's SHA) is also the secure path — fork authors can't tamper with it. The slash-commands README example is updated to use the deploy action with the new inputs. The earlier commit on this branch (which rewrote the example to call wrangler directly) is effectively superseded. --- cloudflare-pages-preview/README.md | 3 ++ cloudflare-pages-preview/cleanup/action.yml | 14 +++++- cloudflare-pages-preview/deploy/action.yml | 24 +++++++++- slash-commands/README.md | 50 +++++---------------- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/cloudflare-pages-preview/README.md b/cloudflare-pages-preview/README.md index 7d33224..8ce3cba 100644 --- a/cloudflare-pages-preview/README.md +++ b/cloudflare-pages-preview/README.md @@ -139,6 +139,8 @@ Cloudflare API and GitHub API. | `comment-marker` | | `` | Sticky-comment identifier | | `comment-title` | | `Preview Deployed` | Heading in PR comment | | `github-token` | | `${{ github.token }}` | Token for PR commenting | +| `pr-number` | | `${{ github.event.pull_request.number }}` | PR number to comment on — override when running on `workflow_dispatch` or other triggers without `payload.pull_request` | +| `commit-sha` | | `${{ github.sha }}` | SHA shown in the sticky comment — override when the checked-out ref differs from `github.sha` | ### `cleanup` @@ -150,6 +152,7 @@ Cloudflare API and GitHub API. | `branch` | | `pr-` | CF Pages branch alias to delete | | `comment-marker` | | `` | Sticky-comment identifier | | `github-token` | | `${{ github.token }}` | Token for PR commenting | +| `pr-number` | | `${{ github.event.pull_request.number }}` | PR number to update the sticky comment on — override when running on `workflow_dispatch` or other triggers without `payload.pull_request` | ## Outputs diff --git a/cloudflare-pages-preview/cleanup/action.yml b/cloudflare-pages-preview/cleanup/action.yml index 1fe9810..c01f822 100644 --- a/cloudflare-pages-preview/cleanup/action.yml +++ b/cloudflare-pages-preview/cleanup/action.yml @@ -26,6 +26,13 @@ inputs: description: GitHub token for updating the PR comment required: false default: ${{ github.token }} + pr-number: + description: >- + PR number to update the sticky comment on. Only needed on triggers + where context.payload.pull_request is unavailable (e.g. workflow_dispatch). + Defaults to the event payload's PR number. + required: false + default: ${{ github.event.pull_request.number }} runs: using: composite @@ -65,11 +72,16 @@ runs: uses: actions/github-script@v8 env: COMMENT_MARKER: ${{ inputs.comment-marker }} + PR_NUMBER: ${{ inputs.pr-number }} with: github-token: ${{ inputs.github-token }} script: | const { owner, repo } = context.repo; - const prNumber = context.payload.pull_request.number; + const prNumber = Number(process.env.PR_NUMBER); + if (!prNumber) { + core.info('No PR number available — skipping comment update.'); + return; + } const marker = process.env.COMMENT_MARKER; const { data: comments } = await github.rest.issues.listComments({ diff --git a/cloudflare-pages-preview/deploy/action.yml b/cloudflare-pages-preview/deploy/action.yml index f930855..b331c62 100644 --- a/cloudflare-pages-preview/deploy/action.yml +++ b/cloudflare-pages-preview/deploy/action.yml @@ -40,6 +40,20 @@ inputs: description: GitHub token for posting/updating the PR comment required: false default: ${{ github.token }} + pr-number: + description: >- + PR number to comment on. Only needed on triggers where + context.payload.pull_request is unavailable (e.g. workflow_dispatch). + Defaults to the event payload's PR number. + required: false + default: ${{ github.event.pull_request.number }} + commit-sha: + description: >- + Commit SHA shown in the sticky comment. Useful when the checked-out + ref differs from github.sha (e.g. building a pinned head SHA on + workflow_dispatch). Defaults to github.sha. + required: false + default: ${{ github.sha }} outputs: deployment-url: @@ -93,11 +107,17 @@ runs: COMMENT_MARKER: ${{ inputs.comment-marker }} COMMENT_TITLE: ${{ inputs.comment-title }} INJECT_NOINDEX: ${{ inputs.inject-noindex }} + PR_NUMBER: ${{ inputs.pr-number }} + COMMIT_SHA: ${{ inputs.commit-sha }} with: github-token: ${{ inputs.github-token }} script: | const { owner, repo } = context.repo; - const prNumber = context.payload.pull_request.number; + const prNumber = Number(process.env.PR_NUMBER); + if (!prNumber) { + core.info('No PR number available — skipping comment.'); + return; + } const deployUrl = process.env.DEPLOY_URL; const aliasUrl = process.env.ALIAS_URL || deployUrl; const marker = process.env.COMMENT_MARKER; @@ -115,7 +135,7 @@ runs: |---|---| | **Preview (stable)** | ${aliasUrl} | | **This commit** | ${deployUrl} | - | **Commit** | \`${context.sha.slice(0, 7)}\` | + | **Commit** | \`${process.env.COMMIT_SHA.slice(0, 7)}\` | _Includes drafts and future-dated posts.${noindexNote}_`; diff --git a/slash-commands/README.md b/slash-commands/README.md index 90684c2..b0aba74 100644 --- a/slash-commands/README.md +++ b/slash-commands/README.md @@ -172,49 +172,19 @@ jobs: # ... your blog build steps → output to blog/public ... - # cloudflare-pages-preview/deploy cannot be used here — its comment - # step reads context.payload.pull_request.number, which is undefined - # on workflow_dispatch and will throw. Call wrangler directly: - - name: Inject noindex - run: | - echo -e '/*\n X-Robots-Tag: noindex, nofollow, noarchive, nosnippet' > blog/public/_headers - find blog/public -name '*.html' -print0 | \ - xargs -0 -r sed -i 's|||' - - - id: deploy - uses: cloudflare/wrangler-action@v3 + - uses: modelcontextprotocol/actions/cloudflare-pages-preview/deploy@main with: - apiToken: ${{ secrets.CF_PAGES_PREVIEW_API_TOKEN }} - accountId: ${{ secrets.CF_PAGES_PREVIEW_ACCOUNT_ID }} - command: pages deploy blog/public --project-name=mcp-blog-preview --branch=pr-${{ inputs.pr_number }} - - - uses: actions/github-script@v8 - env: - PR_NUMBER: ${{ inputs.pr_number }} - HEAD_SHA: ${{ inputs.head_sha }} - DEPLOY_URL: ${{ steps.deploy.outputs.deployment-url }} - ALIAS_URL: ${{ steps.deploy.outputs.pages-deployment-alias-url }} - with: - script: | - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: Number(process.env.PR_NUMBER), - body: `## Blog Preview (staged via /stageblog)\n\n` + - `| | |\n|---|---|\n` + - `| **Preview (stable)** | ${process.env.ALIAS_URL} |\n` + - `| **This commit** | ${process.env.DEPLOY_URL} |\n` + - `| **Commit** | \`${process.env.HEAD_SHA.slice(0,7)}\` |\n\n` + - `_Includes drafts and future-dated posts. noindex headers set._`, - }); + directory: blog/public + project-name: mcp-blog-preview + api-token: ${{ secrets.CF_PAGES_PREVIEW_API_TOKEN }} + account-id: ${{ secrets.CF_PAGES_PREVIEW_ACCOUNT_ID }} + branch: pr-${{ inputs.pr_number }} + pr-number: ${{ inputs.pr_number }} + commit-sha: ${{ inputs.head_sha }} + comment-title: "Blog Preview (staged via /stageblog)" + comment-marker: "" ``` -> **Known limitation:** `cloudflare-pages-preview/deploy` currently reads -> `context.payload.pull_request.number` unconditionally in its comment step, -> which throws on `workflow_dispatch`. Until the deploy action accepts an -> explicit `pr-number` input, bypass it and call `cloudflare/wrangler-action` -> directly as shown above. (Fixing deploy is a follow-up.) - ## Inputs | Input | Required | Default | Description |