|
1 | | -name: Cloudflare Preview Deploy |
| 1 | +name: Cloudflare Workers Preview |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | workflow_run: |
5 | 5 | workflows: ["Cloudflare Preview Build"] |
6 | 6 | types: |
7 | 7 | - completed |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: cloudflare-preview-${{ github.event.workflow_run.event.pull_request.number }} |
| 12 | + cancel-in-progress: true |
8 | 13 |
|
9 | 14 | jobs: |
10 | | - deploy: |
| 15 | + deploy-preview: |
11 | 16 | runs-on: ubuntu-latest |
12 | | - if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' && github.repository == 'doocs/md' |
| 17 | + if: github.repository == 'doocs/md' |
13 | 18 | permissions: |
14 | 19 | contents: read |
15 | 20 | deployments: write |
16 | 21 | pull-requests: write |
17 | | - steps: |
18 | | - - name: Checkout |
19 | | - uses: actions/checkout@v5 |
20 | 22 |
|
21 | | - - name: Download PR artifact |
22 | | - uses: dawidd6/action-download-artifact@v11 |
| 23 | + steps: |
| 24 | + - name: Download build artifact |
| 25 | + uses: actions/download-artifact@v4 |
23 | 26 | with: |
24 | | - workflow: ${{ github.event.workflow_run.workflow_id }} |
25 | | - run_id: ${{ github.event.workflow_run.id }} |
26 | | - name: cloudflare-pr |
| 27 | + name: cloudflare-dist |
| 28 | + path: apps/web/dist |
27 | 29 |
|
28 | | - - name: Save PR id |
29 | | - id: pr |
30 | | - run: | |
31 | | - pr_id=$(<pr-id.txt) |
32 | | - if ! [[ "$pr_id" =~ ^[0-9]+$ ]]; then |
33 | | - echo "Error: pr-id.txt does not contain a valid numeric PR id. Please check." |
34 | | - exit 1 |
35 | | - fi |
36 | | - echo "id=$pr_id" >> $GITHUB_OUTPUT |
37 | | -
|
38 | | - - name: Download dist artifact |
39 | | - uses: dawidd6/action-download-artifact@v11 |
| 30 | + - name: Download PR number |
| 31 | + uses: actions/download-artifact@v4 |
40 | 32 | with: |
41 | | - workflow: ${{ github.event.workflow_run.workflow_id }} |
42 | | - run_id: ${{ github.event.workflow_run.id }} |
43 | | - workflow_conclusion: success |
44 | | - name: cloudflare-dist |
45 | | - path: deploy-package |
| 33 | + name: cloudflare-pr |
| 34 | + path: . |
46 | 35 |
|
47 | | - - name: Update wrangler config |
48 | | - run: | |
49 | | - cd deploy-package |
50 | | - # Update the assets directory path in wrangler.jsonc |
51 | | - sed -i 's|"directory": "./public/"|"directory": "./dist/"|g' wrangler.jsonc |
| 36 | + - name: Read PR number |
| 37 | + id: read-pr |
| 38 | + run: echo "pr_number=$(cat pr-id.txt)" >> $GITHUB_OUTPUT |
52 | 39 |
|
53 | 40 | - name: Deploy to Cloudflare Workers |
54 | 41 | id: deploy |
55 | 42 | uses: cloudflare/wrangler-action@v3 |
56 | 43 | with: |
57 | 44 | apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
58 | 45 | accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
59 | | - command: deploy --name md-pr-${{ steps.pr.outputs.id }} |
60 | | - workingDirectory: deploy-package |
61 | | - packageManager: npm |
| 46 | + command: deploy --name md-pr-${{ steps.read-pr.outputs.pr_number }} |
| 47 | + workingDirectory: apps/web |
62 | 48 | env: |
63 | 49 | CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
64 | 50 | CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
65 | 51 |
|
66 | | - - name: Comment PR with preview link |
67 | | - uses: actions-cool/maintain-one-comment@v3 |
68 | | - with: |
69 | | - token: ${{ secrets.GITHUB_TOKEN }} |
70 | | - body: | |
71 | | - 🚀 Cloudflare Workers Preview has been successfully deployed! |
72 | | -
|
73 | | - **Preview URL:** https://md-pr-${{ steps.pr.outputs.id }}.doocs.workers.dev |
74 | | -
|
75 | | - <sub>Built with commit ${{ github.event.workflow_run.head_sha }}</sub> |
76 | | -
|
77 | | - <!-- Cloudflare Workers Preview Comment --> |
78 | | - body-include: '<!-- Cloudflare Workers Preview Comment -->' |
79 | | - number: ${{ steps.pr.outputs.id }} |
80 | | - |
81 | | - - name: Deploy failed |
82 | | - if: ${{ failure() }} |
83 | | - uses: actions-cool/maintain-one-comment@v3 |
84 | | - with: |
85 | | - token: ${{ secrets.GITHUB_TOKEN }} |
86 | | - body: | |
87 | | - 😭 Cloudflare Workers Preview deployment failed. |
88 | | -
|
89 | | - Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details. |
90 | | -
|
91 | | - <!-- Cloudflare Workers Preview Comment --> |
92 | | - body-include: '<!-- Cloudflare Workers Preview Comment -->' |
93 | | - number: ${{ steps.pr.outputs.id }} |
94 | | - |
95 | | - failed: |
96 | | - runs-on: ubuntu-latest |
97 | | - if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure' && github.repository == 'doocs/md' |
98 | | - steps: |
99 | | - - name: Download PR artifact |
100 | | - uses: dawidd6/action-download-artifact@v11 |
101 | | - with: |
102 | | - workflow: ${{ github.event.workflow_run.workflow_id }} |
103 | | - run_id: ${{ github.event.workflow_run.id }} |
104 | | - name: cloudflare-pr |
105 | | - |
106 | | - - name: Save PR id |
107 | | - id: pr |
| 52 | + - name: Get deployment URL |
| 53 | + id: deployment-url |
108 | 54 | run: | |
109 | | - pr_id=$(<pr-id.txt) |
110 | | - if ! [[ "$pr_id" =~ ^[0-9]+$ ]]; then |
111 | | - echo "Error: pr-id.txt does not contain a valid numeric PR id. Please check." |
112 | | - exit 1 |
113 | | - fi |
114 | | - echo "id=$pr_id" >> $GITHUB_OUTPUT |
115 | | -
|
116 | | - - name: Comment PR with build failure |
117 | | - uses: actions-cool/maintain-one-comment@v3 |
118 | | - with: |
119 | | - token: ${{ secrets.GITHUB_TOKEN }} |
120 | | - body: | |
121 | | - 😭 Cloudflare Workers Preview build failed. |
| 55 | + PREVIEW_URL="https://md-pr-${{ steps.read-pr.outputs.pr_number }}.doocs.workers.dev" |
| 56 | + echo "url=$PREVIEW_URL" >> $GITHUB_OUTPUT |
| 57 | + echo "Preview URL: $PREVIEW_URL" |
122 | 58 |
|
123 | | - Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}) for details. |
124 | | -
|
125 | | - <!-- Cloudflare Workers Preview Comment --> |
126 | | - body-include: '<!-- Cloudflare Workers Preview Comment -->' |
127 | | - number: ${{ steps.pr.outputs.id }} |
| 59 | + - name: Comment PR with preview link |
| 60 | + uses: actions/github-script@v7 |
| 61 | + with: |
| 62 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 63 | + script: | |
| 64 | + const previewUrl = '${{ steps.deployment-url.outputs.url }}'; |
| 65 | + const prNumber = Number('${{ steps.read-pr.outputs.pr_number }}'); |
| 66 | +
|
| 67 | + const comments = await github.rest.issues.listComments({ |
| 68 | + owner: context.repo.owner, |
| 69 | + repo: context.repo.repo, |
| 70 | + issue_number: prNumber, |
| 71 | + }); |
| 72 | +
|
| 73 | + const botComment = comments.data.find(comment => |
| 74 | + comment.user.type === 'Bot' && |
| 75 | + comment.body.includes('Cloudflare Workers Preview') |
| 76 | + ); |
| 77 | +
|
| 78 | + const commentBody = `## 🚀 Cloudflare Workers Preview |
| 79 | +
|
| 80 | + Your preview is ready! |
| 81 | +
|
| 82 | + **Preview URL:** ${previewUrl} |
| 83 | +
|
| 84 | + <sub>Built with commit ${context.workflow_run.head_sha}</sub>`; |
| 85 | +
|
| 86 | + if (botComment) { |
| 87 | + await github.rest.issues.updateComment({ |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + comment_id: botComment.id, |
| 91 | + body: commentBody |
| 92 | + }); |
| 93 | + } else { |
| 94 | + await github.rest.issues.createComment({ |
| 95 | + owner: context.repo.owner, |
| 96 | + repo: context.repo.repo, |
| 97 | + issue_number: prNumber, |
| 98 | + body: commentBody |
| 99 | + }); |
| 100 | + } |
0 commit comments