|
| 1 | +name: Deploy Preview (Vercel) |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +concurrency: |
| 8 | + group: preview-${{ github.ref }} |
| 9 | + cancel-in-progress: true |
| 10 | + |
| 11 | +jobs: |
| 12 | + deploy: |
| 13 | + name: Vercel preview |
| 14 | + runs-on: ubuntu-latest |
| 15 | + permissions: |
| 16 | + contents: read |
| 17 | + pull-requests: write |
| 18 | + env: |
| 19 | + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} |
| 20 | + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} |
| 21 | + steps: |
| 22 | + - name: Checkout |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Setup pnpm |
| 26 | + uses: pnpm/action-setup@v4 |
| 27 | + with: |
| 28 | + version: 10 |
| 29 | + |
| 30 | + - name: Setup Node 22 |
| 31 | + uses: actions/setup-node@v4 |
| 32 | + with: |
| 33 | + node-version: 22 |
| 34 | + cache: pnpm |
| 35 | + |
| 36 | + - name: Install Vercel CLI |
| 37 | + run: pnpm add -g vercel@latest |
| 38 | + |
| 39 | + - name: Pull Vercel environment |
| 40 | + run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }} |
| 41 | + |
| 42 | + - name: Build |
| 43 | + run: vercel build --token=${{ secrets.VERCEL_TOKEN }} |
| 44 | + |
| 45 | + - name: Deploy preview |
| 46 | + id: deploy |
| 47 | + run: | |
| 48 | + url=$(vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }}) |
| 49 | + echo "url=$url" >> "$GITHUB_OUTPUT" |
| 50 | +
|
| 51 | + - name: Comment preview URL on PR |
| 52 | + uses: actions/github-script@v7 |
| 53 | + with: |
| 54 | + script: | |
| 55 | + const url = '${{ steps.deploy.outputs.url }}'; |
| 56 | + const body = `🚀 **Preview deployed:** ${url}`; |
| 57 | + const { data: comments } = await github.rest.issues.listComments({ |
| 58 | + owner: context.repo.owner, |
| 59 | + repo: context.repo.repo, |
| 60 | + issue_number: context.issue.number, |
| 61 | + }); |
| 62 | + const existing = comments.find(c => c.user.type === 'Bot' && c.body.startsWith('🚀 **Preview deployed:**')); |
| 63 | + if (existing) { |
| 64 | + await github.rest.issues.updateComment({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + comment_id: existing.id, |
| 68 | + body, |
| 69 | + }); |
| 70 | + } else { |
| 71 | + await github.rest.issues.createComment({ |
| 72 | + owner: context.repo.owner, |
| 73 | + repo: context.repo.repo, |
| 74 | + issue_number: context.issue.number, |
| 75 | + body, |
| 76 | + }); |
| 77 | + } |
0 commit comments