|
| 1 | +name: Vercel Preview Deployment |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [labeled, unlabeled, synchronize, closed] |
| 6 | + |
| 7 | +jobs: |
| 8 | + deploy-or-remove: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + steps: |
| 12 | + - name: Checkout |
| 13 | + uses: actions/checkout@v4 |
| 14 | + |
| 15 | + - name: Setup Node.js |
| 16 | + uses: actions/setup-node@v4 |
| 17 | + with: |
| 18 | + node-version: 18 |
| 19 | + |
| 20 | + - name: Setup pnpm |
| 21 | + uses: pnpm/action-setup@v4 |
| 22 | + with: |
| 23 | + version: 9 |
| 24 | + |
| 25 | + - name: Install Vercel CLI |
| 26 | + run: npm install -g vercel@latest |
| 27 | + |
| 28 | + - name: Handle PR Events |
| 29 | + env: |
| 30 | + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} |
| 31 | + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} |
| 32 | + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} |
| 33 | + run: | |
| 34 | + PR_NUMBER=${{ github.event.pull_request.number }} |
| 35 | + BRANCH_NAME=${{ github.event.pull_request.head.ref }} |
| 36 | + |
| 37 | + # Check for 'preview' label using contains |
| 38 | + HAS_PREVIEW_LABEL=false |
| 39 | + if echo '${{ toJson(github.event.pull_request.labels) }}' | jq -r '.[].name' | grep -q "^preview$"; then |
| 40 | + HAS_PREVIEW_LABEL=true |
| 41 | + fi |
| 42 | +
|
| 43 | + if [ "${{ github.event.action }}" == "closed" ]; then |
| 44 | + echo "PR closed. Attempting to remove deployment..." |
| 45 | + # List deployments and remove those matching the branch |
| 46 | + DEPLOYMENTS=$(vercel ls --token=$VERCEL_TOKEN --meta gitBranch=$BRANCH_NAME --json | jq -r '.[].url' 2>/dev/null || echo "") |
| 47 | + if [ -n "$DEPLOYMENTS" ]; then |
| 48 | + echo "$DEPLOYMENTS" | while read -r deployment; do |
| 49 | + if [ -n "$deployment" ]; then |
| 50 | + echo "Removing deployment: $deployment" |
| 51 | + vercel remove $deployment --yes --token=$VERCEL_TOKEN || echo "Failed to remove $deployment" |
| 52 | + fi |
| 53 | + done |
| 54 | + else |
| 55 | + echo "No deployments found for branch: $BRANCH_NAME" |
| 56 | + fi |
| 57 | + elif [ "$HAS_PREVIEW_LABEL" == "true" ]; then |
| 58 | + echo "Preview label found. Deploying preview..." |
| 59 | + |
| 60 | + # Pull Vercel configuration |
| 61 | + vercel pull --yes --environment=preview --token=$VERCEL_TOKEN |
| 62 | + |
| 63 | + # Build the project |
| 64 | + echo "Building project..." |
| 65 | + vercel build --token=$VERCEL_TOKEN |
| 66 | + |
| 67 | + # Deploy the built project |
| 68 | + echo "Deploying to Vercel..." |
| 69 | + DEPLOYMENT_URL=$(vercel deploy --prebuilt --token=$VERCEL_TOKEN) |
| 70 | + |
| 71 | + if [ $? -eq 0 ] && [ -n "$DEPLOYMENT_URL" ]; then |
| 72 | + echo "✅ Deployment successful!" |
| 73 | + echo "🔗 Preview URL: $DEPLOYMENT_URL" |
| 74 | + |
| 75 | + # Add deployment URL as a comment to PR |
| 76 | + echo "DEPLOYMENT_URL=$DEPLOYMENT_URL" >> $GITHUB_ENV |
| 77 | + else |
| 78 | + echo "❌ Deployment failed" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + else |
| 82 | + echo "No 'preview' label. Skipping deployment." |
| 83 | + fi |
| 84 | +
|
| 85 | + - name: Comment PR with deployment URL |
| 86 | + if: env.DEPLOYMENT_URL |
| 87 | + uses: actions/github-script@v7 |
| 88 | + with: |
| 89 | + script: | |
| 90 | + github.rest.issues.createComment({ |
| 91 | + issue_number: context.issue.number, |
| 92 | + owner: context.repo.owner, |
| 93 | + repo: context.repo.repo, |
| 94 | + body: `🚀 **Preview deployment ready!**\n\n📎 **Preview URL:** ${process.env.DEPLOYMENT_URL}\n\n*This preview will be automatically updated when you push new commits to this PR.*` |
| 95 | + }) |
0 commit comments