Cloudflare Workers Preview #9
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
| name: Cloudflare Workers Preview | |
| on: | |
| workflow_run: | |
| workflows: ["Cloudflare Preview Build"] | |
| types: | |
| - completed | |
| workflow_dispatch: | |
| concurrency: | |
| group: cloudflare-preview-${{ github.event.workflow_run.event.pull_request.number }} | |
| cancel-in-progress: true | |
| jobs: | |
| deploy-preview: | |
| runs-on: ubuntu-latest | |
| if: github.repository == 'doocs/md' | |
| permissions: | |
| contents: read | |
| deployments: write | |
| pull-requests: write | |
| steps: | |
| - name: Download build artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: cloudflare-dist | |
| path: apps/web/dist | |
| - name: Download PR number | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: cloudflare-pr | |
| path: . | |
| - name: Read PR number | |
| id: read-pr | |
| run: echo "pr_number=$(cat pr-id.txt)" >> $GITHUB_OUTPUT | |
| - name: Deploy to Cloudflare Workers | |
| id: deploy | |
| uses: cloudflare/wrangler-action@v3 | |
| with: | |
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
| command: deploy --name md-pr-${{ steps.read-pr.outputs.pr_number }} | |
| workingDirectory: apps/web | |
| env: | |
| CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
| - name: Get deployment URL | |
| id: deployment-url | |
| run: | | |
| PREVIEW_URL="https://md-pr-${{ steps.read-pr.outputs.pr_number }}.doocs.workers.dev" | |
| echo "url=$PREVIEW_URL" >> $GITHUB_OUTPUT | |
| echo "Preview URL: $PREVIEW_URL" | |
| - name: Comment PR with preview link | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const previewUrl = '${{ steps.deployment-url.outputs.url }}'; | |
| const prNumber = Number('${{ steps.read-pr.outputs.pr_number }}'); | |
| const comments = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| }); | |
| const botComment = comments.data.find(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('Cloudflare Workers Preview') | |
| ); | |
| const commentBody = `## 🚀 Cloudflare Workers Preview | |
| Your preview is ready! | |
| **Preview URL:** ${previewUrl} | |
| <sub>Built with commit ${context.workflow_run.head_sha}</sub>`; | |
| if (botComment) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: botComment.id, | |
| body: commentBody | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: commentBody | |
| }); | |
| } |