|
| 1 | +name: Cloudflare Workers Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +concurrency: |
| 8 | + group: cloudflare-preview-${{ github.event.pull_request.number }} |
| 9 | + cancel-in-progress: true |
| 10 | + |
| 11 | +jobs: |
| 12 | + deploy-preview: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + if: github.repository == 'doocs/md' |
| 15 | + permissions: |
| 16 | + contents: read |
| 17 | + deployments: write |
| 18 | + pull-requests: write |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@v5 |
| 22 | + with: |
| 23 | + ref: ${{ github.event.pull_request.head.sha }} |
| 24 | + |
| 25 | + - name: Set up node |
| 26 | + uses: actions/setup-node@v6 |
| 27 | + with: |
| 28 | + node-version: 22 |
| 29 | + |
| 30 | + - name: Setup pnpm |
| 31 | + uses: pnpm/action-setup@v4 |
| 32 | + with: |
| 33 | + version: 10 |
| 34 | + |
| 35 | + - name: Install dependencies |
| 36 | + run: pnpm install |
| 37 | + |
| 38 | + - name: Build for Cloudflare Workers |
| 39 | + run: pnpm web build:h5-netlify |
| 40 | + env: |
| 41 | + CF_PAGES: 1 |
| 42 | + |
| 43 | + - name: Deploy to Cloudflare Workers |
| 44 | + id: deploy |
| 45 | + uses: cloudflare/wrangler-action@v3 |
| 46 | + with: |
| 47 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 48 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 49 | + command: deploy --name md-pr-${{ github.event.pull_request.number }} |
| 50 | + workingDirectory: apps/web |
| 51 | + env: |
| 52 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 53 | + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 54 | + |
| 55 | + - name: Get deployment URL |
| 56 | + id: deployment-url |
| 57 | + run: | |
| 58 | + PREVIEW_URL="https://md-pr-${{ github.event.pull_request.number }}.doocs.workers.dev" |
| 59 | + echo "url=$PREVIEW_URL" >> $GITHUB_OUTPUT |
| 60 | + echo "Preview URL: $PREVIEW_URL" |
| 61 | +
|
| 62 | + - name: Comment PR with preview link |
| 63 | + uses: actions/github-script@v7 |
| 64 | + with: |
| 65 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 66 | + script: | |
| 67 | + const previewUrl = '${{ steps.deployment-url.outputs.url }}'; |
| 68 | + const prNumber = context.issue.number; |
| 69 | + |
| 70 | + // Find existing comment |
| 71 | + const comments = await github.rest.issues.listComments({ |
| 72 | + owner: context.repo.owner, |
| 73 | + repo: context.repo.repo, |
| 74 | + issue_number: prNumber, |
| 75 | + }); |
| 76 | + |
| 77 | + const botComment = comments.data.find(comment => |
| 78 | + comment.user.type === 'Bot' && |
| 79 | + comment.body.includes('Cloudflare Workers Preview') |
| 80 | + ); |
| 81 | + |
| 82 | + const commentBody = `## 🚀 Cloudflare Workers Preview |
| 83 | + |
| 84 | + Your preview is ready! |
| 85 | + |
| 86 | + **Preview URL:** ${previewUrl} |
| 87 | + |
| 88 | + <sub>Built with commit ${context.sha}</sub>`; |
| 89 | + |
| 90 | + if (botComment) { |
| 91 | + // Update existing comment |
| 92 | + await github.rest.issues.updateComment({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + comment_id: botComment.id, |
| 96 | + body: commentBody |
| 97 | + }); |
| 98 | + } else { |
| 99 | + // Create new comment |
| 100 | + await github.rest.issues.createComment({ |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + issue_number: prNumber, |
| 104 | + body: commentBody |
| 105 | + }); |
| 106 | + } |
0 commit comments