|
| 1 | +name: Deploy to Vercel with Redeploy-on-Failure |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +jobs: |
| 9 | + deploy: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + env: |
| 12 | + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} |
| 13 | + VERCEL_ORG_ID: ${{ secrets.VERCEL_TEAM_ID }} |
| 14 | + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Checkout code |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Setup Node.js |
| 21 | + uses: actions/setup-node@v4 |
| 22 | + with: |
| 23 | + node-version: "22.x" |
| 24 | + |
| 25 | + - name: Link Vercel Project |
| 26 | + run: | |
| 27 | + npx vercel link --cwd . --yes \ |
| 28 | + --token "$VERCEL_TOKEN" \ |
| 29 | + --scope "$VERCEL_ORG_ID" \ |
| 30 | + --project "$VERCEL_PROJECT_ID" |
| 31 | +
|
| 32 | + - name: Deploy & Redeploy on Failure |
| 33 | + shell: bash |
| 34 | + run: | |
| 35 | + set +e |
| 36 | +
|
| 37 | + # 1) Initial production deploy (capture logs) |
| 38 | + npx vercel --prod --yes \ |
| 39 | + --token "$VERCEL_TOKEN" \ |
| 40 | + --scope "$VERCEL_ORG_ID" > deploy.log 2>&1 |
| 41 | + EXIT_CODE=$? |
| 42 | + cat deploy.log |
| 43 | +
|
| 44 | + # Extract deployment URL |
| 45 | + DEPLOY_URL=$(grep -Eo 'https?://[A-Za-z0-9.-]+\.vercel\.app' deploy.log | head -n1) |
| 46 | +
|
| 47 | + # 2) If deploy failed, Redeploy using URL |
| 48 | + if [ $EXIT_CODE -ne 0 ]; then |
| 49 | + echo "❗️ Initial deployment failed (exit code=$EXIT_CODE). Redeploying" |
| 50 | + npx vercel redeploy "$DEPLOY_URL" \ |
| 51 | + --token "$VERCEL_TOKEN" \ |
| 52 | + --scope "$VERCEL_ORG_ID" |
| 53 | + REDO_EXIT=$? |
| 54 | + if [ $REDO_EXIT -ne 0 ]; then |
| 55 | + echo "::error::Redeploy failed (exit code=$REDO_EXIT)!" |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | + fi |
0 commit comments