Cloudflare Workers Preview #13
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.pull_requests[0].number }} | |
| cancel-in-progress: true | |
| jobs: | |
| deploy-preview: | |
| runs-on: ubuntu-latest | |
| if: github.repository == 'doocs/md' && github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' | |
| permissions: | |
| actions: read | |
| contents: read | |
| deployments: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Download PR number | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: cloudflare-pr | |
| path: . | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| repository: ${{ github.repository }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| - name: Read PR number | |
| id: read-pr | |
| run: echo "pr_number=$(cat pr-id.txt)" >> $GITHUB_OUTPUT | |
| - name: Download build artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: cloudflare-dist | |
| path: apps/web/dist | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| repository: ${{ github.repository }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v6 | |
| with: | |
| node-version: 22 | |
| - 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 }} --assets dist | |
| workingDirectory: apps/web | |
| - 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 | |
| }); | |
| } |