|
| 1 | +name: Preview |
| 2 | + |
| 3 | +# Per-PR preview deploys of the Cloudflare host to a dedicated preview account. |
| 4 | +# Every same-repo PR gets its own isolated stack (Worker + D1 + Access app) |
| 5 | +# behind Cloudflare Access; it is torn down when the PR closes. Fork PRs are |
| 6 | +# skipped — they must not run with the deploy token. |
| 7 | +# |
| 8 | +# Required repo configuration: |
| 9 | +# secret CLOUDFLARE_PREVIEW_API_TOKEN — scoped token (Workers/D1/R2/Access edit) |
| 10 | +# var CLOUDFLARE_PREVIEW_ACCOUNT_ID — the preview Cloudflare account |
| 11 | +# var PREVIEW_ACCESS_TEAM_DOMAIN — Zero Trust team domain |
| 12 | +# var PREVIEW_ACCESS_EMAILS — comma-separated emails allowed through Access |
| 13 | + |
| 14 | +on: |
| 15 | + pull_request: |
| 16 | + types: [opened, reopened, synchronize, closed] |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: read |
| 20 | + pull-requests: write |
| 21 | + |
| 22 | +concurrency: |
| 23 | + group: preview-${{ github.event.pull_request.number }} |
| 24 | + cancel-in-progress: ${{ github.event.action != 'closed' }} |
| 25 | + |
| 26 | +jobs: |
| 27 | + deploy: |
| 28 | + name: Deploy preview |
| 29 | + if: github.event.action != 'closed' && github.event.pull_request.head.repo.full_name == github.repository |
| 30 | + runs-on: ubuntu-latest |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - uses: oven-sh/setup-bun@v2 |
| 35 | + with: |
| 36 | + bun-version: 1.3.11 |
| 37 | + |
| 38 | + - run: bun install --frozen-lockfile |
| 39 | + |
| 40 | + - name: Deploy |
| 41 | + id: deploy |
| 42 | + working-directory: apps/host-cloudflare |
| 43 | + env: |
| 44 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_PREVIEW_API_TOKEN }} |
| 45 | + CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_PREVIEW_ACCOUNT_ID }} |
| 46 | + PREVIEW_ACCESS_TEAM_DOMAIN: ${{ vars.PREVIEW_ACCESS_TEAM_DOMAIN }} |
| 47 | + PREVIEW_ACCESS_EMAILS: ${{ vars.PREVIEW_ACCESS_EMAILS }} |
| 48 | + run: bun scripts/preview.ts deploy --pr ${{ github.event.pull_request.number }} |
| 49 | + |
| 50 | + - name: Comment preview URL |
| 51 | + uses: actions/github-script@v7 |
| 52 | + env: |
| 53 | + PREVIEW_URL: ${{ steps.deploy.outputs.url }} |
| 54 | + with: |
| 55 | + script: | |
| 56 | + const marker = "<!-- executor-preview -->"; |
| 57 | + const body = [ |
| 58 | + marker, |
| 59 | + "### Cloudflare preview", |
| 60 | + "", |
| 61 | + `| | |`, |
| 62 | + `|---|---|`, |
| 63 | + `| Console | ${process.env.PREVIEW_URL} |`, |
| 64 | + `| MCP | \`${process.env.PREVIEW_URL}/mcp\` |`, |
| 65 | + `| Deployed commit | ${context.payload.pull_request.head.sha} |`, |
| 66 | + "", |
| 67 | + "Sign-in is Cloudflare Access (one-time PIN to an allowed email). " + |
| 68 | + "The preview has its own database and encryption key; it is destroyed when this PR closes.", |
| 69 | + ].join("\n"); |
| 70 | + const { data: comments } = await github.rest.issues.listComments({ |
| 71 | + ...context.repo, |
| 72 | + issue_number: context.issue.number, |
| 73 | + per_page: 100, |
| 74 | + }); |
| 75 | + const existing = comments.find((c) => c.body && c.body.startsWith(marker)); |
| 76 | + if (existing) { |
| 77 | + await github.rest.issues.updateComment({ ...context.repo, comment_id: existing.id, body }); |
| 78 | + } else { |
| 79 | + await github.rest.issues.createComment({ ...context.repo, issue_number: context.issue.number, body }); |
| 80 | + } |
| 81 | +
|
| 82 | + teardown: |
| 83 | + name: Tear down preview |
| 84 | + if: github.event.action == 'closed' && github.event.pull_request.head.repo.full_name == github.repository |
| 85 | + runs-on: ubuntu-latest |
| 86 | + steps: |
| 87 | + - uses: actions/checkout@v4 |
| 88 | + |
| 89 | + - uses: oven-sh/setup-bun@v2 |
| 90 | + with: |
| 91 | + bun-version: 1.3.11 |
| 92 | + |
| 93 | + # destroy talks straight to the Cloudflare API — no install needed. |
| 94 | + - name: Destroy |
| 95 | + env: |
| 96 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_PREVIEW_API_TOKEN }} |
| 97 | + CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_PREVIEW_ACCOUNT_ID }} |
| 98 | + run: bun apps/host-cloudflare/scripts/preview.ts destroy --pr ${{ github.event.pull_request.number }} |
| 99 | + |
| 100 | + - name: Mark comment as torn down |
| 101 | + uses: actions/github-script@v7 |
| 102 | + with: |
| 103 | + script: | |
| 104 | + const marker = "<!-- executor-preview -->"; |
| 105 | + const { data: comments } = await github.rest.issues.listComments({ |
| 106 | + ...context.repo, |
| 107 | + issue_number: context.issue.number, |
| 108 | + per_page: 100, |
| 109 | + }); |
| 110 | + const existing = comments.find((c) => c.body && c.body.startsWith(marker)); |
| 111 | + if (existing) { |
| 112 | + await github.rest.issues.updateComment({ |
| 113 | + ...context.repo, |
| 114 | + comment_id: existing.id, |
| 115 | + body: `${marker}\n### Cloudflare preview\n\nTorn down — the PR is closed.`, |
| 116 | + }); |
| 117 | + } |
0 commit comments