|
| 1 | +name: Deploy Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: deploy-preview-${{ github.event.pull_request.number }} |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +jobs: |
| 16 | + deploy-preview: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + env: |
| 20 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 21 | + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 22 | + CLOUDFLARE_D1_DATABASE_ID: ${{ secrets.CLOUDFLARE_D1_DATABASE_ID }} |
| 23 | + CLOUDFLARE_SESSION_KV_ID: ${{ secrets.CLOUDFLARE_SESSION_KV_ID }} |
| 24 | + TWITCH_BOT_USERNAME: ${{ vars.TWITCH_BOT_USERNAME }} |
| 25 | + TWITCH_SCOPES: ${{ vars.TWITCH_SCOPES }} |
| 26 | + PREVIEW_SUFFIX: pr-${{ github.event.pull_request.number }} |
| 27 | + CLOUDFLARE_WORKERS_SUBDOMAIN: ${{ secrets.CLOUDFLARE_WORKERS_SUBDOMAIN }} |
| 28 | + |
| 29 | + steps: |
| 30 | + - uses: actions/checkout@v5 |
| 31 | + |
| 32 | + - uses: actions/setup-node@v5 |
| 33 | + with: |
| 34 | + node-version: 22 |
| 35 | + cache: npm |
| 36 | + |
| 37 | + - name: Install dependencies |
| 38 | + run: npm ci |
| 39 | + |
| 40 | + - name: Build preview app URL |
| 41 | + id: preview-url |
| 42 | + shell: bash |
| 43 | + run: | |
| 44 | + if [ -n "${CLOUDFLARE_WORKERS_SUBDOMAIN}" ]; then |
| 45 | + echo "app_url=https://request-bot-${PREVIEW_SUFFIX}.${CLOUDFLARE_WORKERS_SUBDOMAIN}.workers.dev" >> "$GITHUB_OUTPUT" |
| 46 | + else |
| 47 | + echo "app_url=" >> "$GITHUB_OUTPUT" |
| 48 | + fi |
| 49 | +
|
| 50 | + - name: Build app |
| 51 | + run: npm run build |
| 52 | + |
| 53 | + - name: Generate preview Wrangler configs |
| 54 | + run: > |
| 55 | + node scripts/write-deploy-configs.mjs |
| 56 | + --mode preview |
| 57 | + --artifact build |
| 58 | + --suffix "${{ env.PREVIEW_SUFFIX }}" |
| 59 | + --app-url "${{ steps.preview-url.outputs.app_url || 'https://example.workers.dev' }}" |
| 60 | +
|
| 61 | + - name: Deploy preview backend |
| 62 | + shell: bash |
| 63 | + run: | |
| 64 | + for attempt in 1 2 3; do |
| 65 | + if npx wrangler deploy --config .generated/wrangler.aux.preview.jsonc; then |
| 66 | + exit 0 |
| 67 | + fi |
| 68 | +
|
| 69 | + if [ "$attempt" -lt 3 ]; then |
| 70 | + echo "Preview backend deploy failed on attempt $attempt. Retrying in 15 seconds..." |
| 71 | + sleep 15 |
| 72 | + fi |
| 73 | + done |
| 74 | +
|
| 75 | + echo "Preview backend deploy failed after 3 attempts." |
| 76 | + exit 1 |
| 77 | +
|
| 78 | + - name: Deploy preview frontend |
| 79 | + shell: bash |
| 80 | + run: | |
| 81 | + for attempt in 1 2 3; do |
| 82 | + if npx wrangler deploy --config .generated/wrangler.preview.jsonc; then |
| 83 | + exit 0 |
| 84 | + fi |
| 85 | +
|
| 86 | + if [ "$attempt" -lt 3 ]; then |
| 87 | + echo "Preview frontend deploy failed on attempt $attempt. Retrying in 15 seconds..." |
| 88 | + sleep 15 |
| 89 | + fi |
| 90 | + done |
| 91 | +
|
| 92 | + echo "Preview frontend deploy failed after 3 attempts." |
| 93 | + exit 1 |
| 94 | +
|
| 95 | + - name: Comment preview details |
| 96 | + if: ${{ steps.preview-url.outputs.app_url != '' }} |
| 97 | + uses: actions/github-script@v7 |
| 98 | + with: |
| 99 | + script: | |
| 100 | + const body = [ |
| 101 | + "Preview deployment updated.", |
| 102 | + "", |
| 103 | + `App: ${{ steps.preview-url.outputs.app_url }}`, |
| 104 | + `Frontend worker: request-bot-${process.env.PREVIEW_SUFFIX}`, |
| 105 | + `Backend worker: request-bot-backend-${process.env.PREVIEW_SUFFIX}`, |
| 106 | + ].join("\n"); |
| 107 | + github.rest.issues.createComment({ |
| 108 | + owner: context.repo.owner, |
| 109 | + repo: context.repo.repo, |
| 110 | + issue_number: context.issue.number, |
| 111 | + body, |
| 112 | + }); |
0 commit comments