|
| 1 | +name: Cloudflare Preview Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Cloudflare Preview Build"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +jobs: |
| 10 | + deploy: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' && github.repository == 'doocs/md' |
| 13 | + permissions: |
| 14 | + contents: read |
| 15 | + deployments: write |
| 16 | + pull-requests: write |
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@v5 |
| 20 | + |
| 21 | + - name: Download PR artifact |
| 22 | + uses: dawidd6/action-download-artifact@v11 |
| 23 | + with: |
| 24 | + workflow: ${{ github.event.workflow_run.workflow_id }} |
| 25 | + run_id: ${{ github.event.workflow_run.id }} |
| 26 | + name: cloudflare-pr |
| 27 | + |
| 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 |
| 40 | + 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 | + |
| 46 | + - name: Set up node |
| 47 | + uses: actions/setup-node@v6 |
| 48 | + with: |
| 49 | + node-version: 22 |
| 50 | + |
| 51 | + - name: Setup pnpm |
| 52 | + uses: pnpm/action-setup@v4 |
| 53 | + with: |
| 54 | + version: 10 |
| 55 | + |
| 56 | + - name: Install dependencies |
| 57 | + run: | |
| 58 | + cd apps/web |
| 59 | + pnpm install --prod |
| 60 | +
|
| 61 | + - name: Deploy to Cloudflare Workers |
| 62 | + id: deploy |
| 63 | + uses: cloudflare/wrangler-action@v3 |
| 64 | + with: |
| 65 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 66 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 67 | + command: deploy --name md-pr-${{ steps.pr.outputs.id }} |
| 68 | + workingDirectory: apps/web |
| 69 | + env: |
| 70 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 71 | + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 72 | + |
| 73 | + - name: Comment PR with preview link |
| 74 | + uses: actions-cool/maintain-one-comment@v3 |
| 75 | + with: |
| 76 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 77 | + body: | |
| 78 | + 🚀 Cloudflare Workers Preview has been successfully deployed! |
| 79 | +
|
| 80 | + **Preview URL:** https://md-pr-${{ steps.pr.outputs.id }}.doocs.workers.dev |
| 81 | +
|
| 82 | + <sub>Built with commit ${{ github.event.workflow_run.head_sha }}</sub> |
| 83 | +
|
| 84 | + <!-- Cloudflare Workers Preview Comment --> |
| 85 | + body-include: '<!-- Cloudflare Workers Preview Comment -->' |
| 86 | + number: ${{ steps.pr.outputs.id }} |
| 87 | + |
| 88 | + - name: Deploy failed |
| 89 | + if: ${{ failure() }} |
| 90 | + uses: actions-cool/maintain-one-comment@v3 |
| 91 | + with: |
| 92 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 93 | + body: | |
| 94 | + 😭 Cloudflare Workers Preview deployment failed. |
| 95 | +
|
| 96 | + Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details. |
| 97 | +
|
| 98 | + <!-- Cloudflare Workers Preview Comment --> |
| 99 | + body-include: '<!-- Cloudflare Workers Preview Comment -->' |
| 100 | + number: ${{ steps.pr.outputs.id }} |
| 101 | + |
| 102 | + failed: |
| 103 | + runs-on: ubuntu-latest |
| 104 | + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure' && github.repository == 'doocs/md' |
| 105 | + steps: |
| 106 | + - name: Download PR artifact |
| 107 | + uses: dawidd6/action-download-artifact@v11 |
| 108 | + with: |
| 109 | + workflow: ${{ github.event.workflow_run.workflow_id }} |
| 110 | + run_id: ${{ github.event.workflow_run.id }} |
| 111 | + name: cloudflare-pr |
| 112 | + |
| 113 | + - name: Save PR id |
| 114 | + id: pr |
| 115 | + run: | |
| 116 | + pr_id=$(<pr-id.txt) |
| 117 | + if ! [[ "$pr_id" =~ ^[0-9]+$ ]]; then |
| 118 | + echo "Error: pr-id.txt does not contain a valid numeric PR id. Please check." |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | + echo "id=$pr_id" >> $GITHUB_OUTPUT |
| 122 | +
|
| 123 | + - name: Comment PR with build failure |
| 124 | + uses: actions-cool/maintain-one-comment@v3 |
| 125 | + with: |
| 126 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 127 | + body: | |
| 128 | + 😭 Cloudflare Workers Preview build failed. |
| 129 | +
|
| 130 | + Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}) for details. |
| 131 | +
|
| 132 | + <!-- Cloudflare Workers Preview Comment --> |
| 133 | + body-include: '<!-- Cloudflare Workers Preview Comment -->' |
| 134 | + number: ${{ steps.pr.outputs.id }} |
0 commit comments