|
| 1 | +name: Preview (Cloudflare Workers) |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [develop] |
| 6 | + types: [opened, synchronize, reopened] |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: cloudflare-preview-${{ github.event.pull_request.number }} |
| 10 | + cancel-in-progress: true |
| 11 | + |
| 12 | +jobs: |
| 13 | + preview: |
| 14 | + name: Upload PR preview |
| 15 | + runs-on: ubuntu-latest |
| 16 | + if: github.event.pull_request.head.repo.full_name == github.repository |
| 17 | + permissions: |
| 18 | + contents: read |
| 19 | + pull-requests: write |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6 |
| 24 | + with: |
| 25 | + ref: ${{ github.event.pull_request.head.sha }} |
| 26 | + persist-credentials: false |
| 27 | + |
| 28 | + - name: Setup pnpm |
| 29 | + uses: pnpm/action-setup@f40ffcd9367d9f12939873eb1018b921a783ffaa # v4 |
| 30 | + with: |
| 31 | + version: 9 |
| 32 | + |
| 33 | + - name: Setup Node.js |
| 34 | + uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6 |
| 35 | + with: |
| 36 | + node-version: 24.11.1 |
| 37 | + cache: pnpm |
| 38 | + |
| 39 | + - name: Install dependencies |
| 40 | + run: pnpm install --frozen-lockfile |
| 41 | + |
| 42 | + - name: Run ESLint |
| 43 | + run: pnpm lint |
| 44 | + |
| 45 | + - name: Check code formatting |
| 46 | + run: pnpm format:check |
| 47 | + |
| 48 | + - name: Run tests with coverage |
| 49 | + run: pnpm test:coverage |
| 50 | + env: |
| 51 | + NODE_OPTIONS: --max-old-space-size=4096 |
| 52 | + |
| 53 | + - name: Build project |
| 54 | + run: pnpm build |
| 55 | + env: |
| 56 | + VITE_GIT_BRANCH: ${{ github.head_ref }} |
| 57 | + VITE_DEV_SITE_URL: https://dev.bayanflow.com |
| 58 | + |
| 59 | + - name: Upload preview version |
| 60 | + id: deploy |
| 61 | + uses: cloudflare/wrangler-action@9acf94ace14e7dc412b076f2c5c20b8ce93c79cd # v3 |
| 62 | + with: |
| 63 | + apiToken: ${{ secrets.CF_API_TOKEN }} |
| 64 | + accountId: ${{ secrets.CF_ACCOUNT_ID }} |
| 65 | + packageManager: pnpm |
| 66 | + command: >- |
| 67 | + versions upload --env staging |
| 68 | + --preview-alias pr-${{ github.event.pull_request.number }} |
| 69 | + --tag pr-${{ github.event.pull_request.number }} |
| 70 | + --message "PR #${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}" |
| 71 | +
|
| 72 | + - name: Comment PR with preview URL |
| 73 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 |
| 74 | + env: |
| 75 | + DEPLOY_OUTPUT: ${{ steps.deploy.outputs['command-output'] }} |
| 76 | + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 77 | + HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 78 | + with: |
| 79 | + script: | |
| 80 | + const output = process.env.DEPLOY_OUTPUT || ''; |
| 81 | + const urlRegex = /Version Preview Alias URL:\s+(https:\/\/[^\s]+)/; |
| 82 | + const match = output.match(urlRegex); |
| 83 | + const prNumber = context.payload.pull_request.number; |
| 84 | + const fallbackUrl = `https://pr-${prNumber}-bayan-flow-staging.workers.dev`; |
| 85 | + const previewUrl = match ? match[1] : fallbackUrl; |
| 86 | + const runUrl = process.env.RUN_URL; |
| 87 | + const headSha = process.env.HEAD_SHA; |
| 88 | + const qrUrl = `https://quickchart.io/qr?size=200&text=${encodeURIComponent(previewUrl)}`; |
| 89 | + const marker = '<!-- cloudflare-preview:bayan-flow-staging -->'; |
| 90 | + const body = [ |
| 91 | + '### <span aria-hidden="true">✅</span> Cloudflare Workers preview for *bayan-flow-staging* ready!', |
| 92 | + '', |
| 93 | + '| Name | Link |', |
| 94 | + '|:-:|------------------------|', |
| 95 | + `| <span aria-hidden="true">🔨</span> Latest commit | ${headSha} |`, |
| 96 | + `| <span aria-hidden="true">🔍</span> Latest deploy log | ${runUrl} |`, |
| 97 | + `| <span aria-hidden="true">😎</span> Deploy Preview | [${previewUrl}](${previewUrl}) |`, |
| 98 | + `| <span aria-hidden="true">📱</span> Preview on mobile | <details><summary> Toggle QR Code... </summary><br /><br /><br /><br />_Use your smartphone camera to open QR code link._</details> |`, |
| 99 | + '', |
| 100 | + '---', |
| 101 | + marker, |
| 102 | + `_Preview alias \`pr-${prNumber}\` on the staging worker. Updates automatically with new commits._`, |
| 103 | + ].join('\n'); |
| 104 | +
|
| 105 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 106 | + owner: context.repo.owner, |
| 107 | + repo: context.repo.repo, |
| 108 | + issue_number: context.issue.number, |
| 109 | + per_page: 100, |
| 110 | + }); |
| 111 | + const existing = comments.find( |
| 112 | + (comment) => |
| 113 | + comment.body?.includes(marker) || |
| 114 | + comment.body?.includes('<!-- cloudflare-preview -->') |
| 115 | + ); |
| 116 | +
|
| 117 | + if (existing) { |
| 118 | + await github.rest.issues.updateComment({ |
| 119 | + owner: context.repo.owner, |
| 120 | + repo: context.repo.repo, |
| 121 | + comment_id: existing.id, |
| 122 | + body, |
| 123 | + }); |
| 124 | + } else { |
| 125 | + await github.rest.issues.createComment({ |
| 126 | + owner: context.repo.owner, |
| 127 | + repo: context.repo.repo, |
| 128 | + issue_number: context.issue.number, |
| 129 | + body, |
| 130 | + }); |
| 131 | + } |
0 commit comments