|
| 1 | +name: Deploy Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + deploy: |
| 9 | + name: Vercel preview deployment |
| 10 | + runs-on: ubuntu-latest |
| 11 | + # Only run when Vercel secrets are configured (skips forks / contributors without access) |
| 12 | + if: ${{ vars.VERCEL_PROJECT_ID != '' }} |
| 13 | + |
| 14 | + permissions: |
| 15 | + pull-requests: write # needed to post the preview URL comment |
| 16 | + |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - uses: actions/setup-node@v4 |
| 21 | + with: |
| 22 | + node-version: "20" |
| 23 | + |
| 24 | + - name: Install Vercel CLI |
| 25 | + run: npm install --global vercel@latest |
| 26 | + |
| 27 | + - name: Pull Vercel environment (preview) |
| 28 | + run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }} |
| 29 | + env: |
| 30 | + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} |
| 31 | + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} |
| 32 | + |
| 33 | + - name: Build |
| 34 | + run: vercel build --token=${{ secrets.VERCEL_TOKEN }} |
| 35 | + env: |
| 36 | + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} |
| 37 | + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} |
| 38 | + |
| 39 | + - name: Deploy |
| 40 | + id: deploy |
| 41 | + run: | |
| 42 | + url=$(vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }}) |
| 43 | + echo "url=$url" >> "$GITHUB_OUTPUT" |
| 44 | + env: |
| 45 | + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} |
| 46 | + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} |
| 47 | + |
| 48 | + - name: Post preview URL to PR |
| 49 | + uses: actions/github-script@v7 |
| 50 | + with: |
| 51 | + script: | |
| 52 | + github.rest.issues.createComment({ |
| 53 | + issue_number: context.issue.number, |
| 54 | + owner: context.repo.owner, |
| 55 | + repo: context.repo.repo, |
| 56 | + body: `## Preview deployment\n\n🚀 **${{ steps.deploy.outputs.url }}**\n\nDeployed from ${context.sha.slice(0, 7)}.`, |
| 57 | + }) |
0 commit comments