|
| 1 | +name: Deploy PR Preview to Cloudflare Workers |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +concurrency: |
| 8 | + group: pr-preview-${{ github.event.pull_request.number }} |
| 9 | + cancel-in-progress: true |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + deploy-preview: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + timeout-minutes: 15 |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: ⚙️ Checkout |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: ⚙️ Install dependencies |
| 25 | + uses: ./.github/workflows/install-pnpm |
| 26 | + |
| 27 | + - name: 📝 Update package.json version with commit SHA |
| 28 | + run: | |
| 29 | + PR_COMMIT_SHA="${{ github.event.pull_request.head.sha }}" |
| 30 | + COMMIT_SHA="${PR_COMMIT_SHA:-${{ github.sha }}}" |
| 31 | + COMMIT_SHA_SHORT=$(echo "${COMMIT_SHA}" | cut -c1-8) |
| 32 | + CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 33 | + NEW_VERSION="${CURRENT_VERSION}-${COMMIT_SHA_SHORT}" |
| 34 | + node -e "const fs = require('fs'); const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); pkg.version = '${NEW_VERSION}'; fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');" |
| 35 | + echo "Updated version to: ${NEW_VERSION}" |
| 36 | + echo "COMMIT_SHA=${COMMIT_SHA_SHORT}" >> $GITHUB_ENV |
| 37 | +
|
| 38 | + - name: 🏗️ Build renderer (staging) |
| 39 | + env: |
| 40 | + CHAINS_FILE: chains |
| 41 | + TOKENS_FILE: tokens |
| 42 | + run: | |
| 43 | + pnpm clean:build |
| 44 | + pnpm renderer:staging |
| 45 | + pnpm postbuild:staging |
| 46 | +
|
| 47 | + - name: 🚀 Deploy to Cloudflare Pages |
| 48 | + id: deploy |
| 49 | + uses: cloudflare/wrangler-action@v3 |
| 50 | + with: |
| 51 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 52 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 53 | + workingDirectory: . |
| 54 | + command: pages deploy release/build --project-name=nova-spektr-pr --branch=${{ github.head_ref }} |
| 55 | + |
| 56 | + - name: 💬 Comment PR with preview URL |
| 57 | + if: success() |
| 58 | + uses: actions/github-script@v7 |
| 59 | + with: |
| 60 | + script: | |
| 61 | + const prNumber = context.payload.pull_request.number; |
| 62 | + const branchName = context.payload.pull_request.head.ref; |
| 63 | + const previewUrl = '${{ steps.deploy.outputs.pages-deployment-alias-url }}'; |
| 64 | + const fullCommitSha = '${{ github.event.pull_request.head.sha }}'; |
| 65 | + |
| 66 | + const body = `## 🚀 Preview Deployment Ready! |
| 67 | + |
| 68 | + Your PR preview has been deployed to Cloudflare Pages! |
| 69 | + |
| 70 | + **Preview URL:** ${previewUrl} |
| 71 | + |
| 72 | + **Branch:** \`${branchName}\` |
| 73 | + **Deployed Commit:** ${fullCommitSha} |
| 74 | + **PR:** #${prNumber} |
| 75 | + |
| 76 | + > ⚠️ Note: The deployment may take a few minutes to propagate. If the URL doesn't work immediately, wait a moment and try again. |
| 77 | + |
| 78 | + --- |
| 79 | + *This preview will be automatically updated when you push new commits to this PR.*`; |
| 80 | +
|
| 81 | + // Find existing comment from github-actions bot |
| 82 | + const comments = await github.rest.issues.listComments({ |
| 83 | + issue_number: prNumber, |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + }); |
| 87 | +
|
| 88 | + const existingComment = comments.data.find( |
| 89 | + comment => |
| 90 | + comment.user.type === 'Bot' && |
| 91 | + comment.user.login === 'github-actions[bot]' && |
| 92 | + comment.body.includes('🚀 Preview Deployment Ready!') |
| 93 | + ); |
| 94 | +
|
| 95 | + if (existingComment) { |
| 96 | + // Update existing comment |
| 97 | + await github.rest.issues.updateComment({ |
| 98 | + comment_id: existingComment.id, |
| 99 | + owner: context.repo.owner, |
| 100 | + repo: context.repo.repo, |
| 101 | + body: body |
| 102 | + }); |
| 103 | + } else { |
| 104 | + // Create new comment if none exists |
| 105 | + await github.rest.issues.createComment({ |
| 106 | + issue_number: prNumber, |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + body: body |
| 110 | + }); |
| 111 | + } |
| 112 | +
|
| 113 | + - name: ❌ Comment PR on failure |
| 114 | + if: failure() |
| 115 | + uses: actions/github-script@v7 |
| 116 | + with: |
| 117 | + script: | |
| 118 | + const prNumber = context.payload.pull_request.number; |
| 119 | + const body = `## ❌ Preview Deployment Failed |
| 120 | + |
| 121 | + The preview deployment failed. Please check the workflow logs for details. |
| 122 | + |
| 123 | + **PR:** #${prNumber} |
| 124 | + |
| 125 | + Common issues: |
| 126 | + - Build errors in the renderer |
| 127 | + - Network issues during deployment`; |
| 128 | +
|
| 129 | + github.rest.issues.createComment({ |
| 130 | + issue_number: prNumber, |
| 131 | + owner: context.repo.owner, |
| 132 | + repo: context.repo.repo, |
| 133 | + body: body |
| 134 | + }); |
0 commit comments