|
| 1 | +name: Vercel Deployments |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main] |
| 6 | + push: |
| 7 | + branches: [main] |
| 8 | + |
| 9 | +concurrency: |
| 10 | + group: vercel-${{ github.ref }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +jobs: |
| 14 | + pre-deploy: |
| 15 | + name: Initialize Deployment Status |
| 16 | + runs-on: ubuntu-latest |
| 17 | + if: github.event_name == 'pull_request' |
| 18 | + permissions: |
| 19 | + pull-requests: write |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Post initial deployment status |
| 23 | + uses: actions/github-script@v7 |
| 24 | + with: |
| 25 | + script: | |
| 26 | + const identifier = '<!-- vercel-deployments-comment -->'; |
| 27 | + const timestamp = new Date().toLocaleString('en-US', { |
| 28 | + timeZone: 'America/Los_Angeles', |
| 29 | + year: 'numeric', |
| 30 | + month: 'short', |
| 31 | + day: '2-digit', |
| 32 | + hour: '2-digit', |
| 33 | + minute: '2-digit', |
| 34 | + second: '2-digit', |
| 35 | + hour12: true |
| 36 | + }); |
| 37 | +
|
| 38 | + const projects = [ |
| 39 | + 'apollo-canvas', |
| 40 | + 'apollo-ui-react', |
| 41 | + 'apollo-vertex', |
| 42 | + 'apollo-wind' |
| 43 | + ]; |
| 44 | +
|
| 45 | + const tableRows = projects.map(projectName => { |
| 46 | + const logsLink = `[Logs](https://github.com/${ context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId})`; |
| 47 | + return `| ${projectName} | 🟡 Deploying... | ${logsLink} | ${timestamp} |`; |
| 48 | + }).join('\n'); |
| 49 | +
|
| 50 | + const comment = [ |
| 51 | + identifier, |
| 52 | + 'The latest updates on your projects. Learn more about [Vercel for GitHub](https://vercel.com/docs/deployments/git).', |
| 53 | + '', |
| 54 | + '| Project | Deployment | Review | Updated (PT) |', |
| 55 | + '|---------|------------|--------|---------------|', |
| 56 | + tableRows |
| 57 | + ].join('\n'); |
| 58 | +
|
| 59 | + // Check for existing comment |
| 60 | + const { data: comments } = await github.rest.issues.listComments({ |
| 61 | + owner: context.repo.owner, |
| 62 | + repo: context.repo.repo, |
| 63 | + issue_number: context.issue.number, |
| 64 | + }); |
| 65 | +
|
| 66 | + const existingComment = comments.find(c => c.body?.includes(identifier)); |
| 67 | +
|
| 68 | + if (existingComment) { |
| 69 | + await github.rest.issues.updateComment({ |
| 70 | + owner: context.repo.owner, |
| 71 | + repo: context.repo.repo, |
| 72 | + comment_id: existingComment.id, |
| 73 | + body: comment |
| 74 | + }); |
| 75 | + } else { |
| 76 | + await github.rest.issues.createComment({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + issue_number: context.issue.number, |
| 80 | + body: comment |
| 81 | + }); |
| 82 | + } |
| 83 | +
|
| 84 | + deploy: |
| 85 | + name: Deploy ${{ matrix.project_name }} |
| 86 | + runs-on: ubuntu-latest |
| 87 | + needs: pre-deploy |
| 88 | + if: ${{ always() && !cancelled() }} |
| 89 | + continue-on-error: true |
| 90 | + permissions: |
| 91 | + contents: read |
| 92 | + pull-requests: write |
| 93 | + strategy: |
| 94 | + matrix: |
| 95 | + include: |
| 96 | + - project_name: apollo-canvas |
| 97 | + vercel_project_id_secret: VERCEL_PROJECT_ID_CANVAS |
| 98 | + - project_name: apollo-ui-react |
| 99 | + vercel_project_id_secret: VERCEL_PROJECT_ID_UI_REACT |
| 100 | + - project_name: apollo-vertex |
| 101 | + vercel_project_id_secret: VERCEL_PROJECT_ID_VERTEX |
| 102 | + - project_name: apollo-wind |
| 103 | + vercel_project_id_secret: VERCEL_PROJECT_ID_WIND |
| 104 | + |
| 105 | + steps: |
| 106 | + - name: Checkout |
| 107 | + uses: actions/checkout@v4 |
| 108 | + |
| 109 | + - name: Setup Node.js |
| 110 | + uses: actions/setup-node@v4 |
| 111 | + with: |
| 112 | + node-version: '22' |
| 113 | + |
| 114 | + - name: Setup pnpm |
| 115 | + uses: pnpm/action-setup@v4 |
| 116 | + |
| 117 | + - name: Get pnpm store directory |
| 118 | + id: pnpm-cache |
| 119 | + run: | |
| 120 | + echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT |
| 121 | +
|
| 122 | + - name: Setup pnpm cache |
| 123 | + uses: actions/cache@v4 |
| 124 | + with: |
| 125 | + path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }} |
| 126 | + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} |
| 127 | + restore-keys: | |
| 128 | + ${{ runner.os }}-pnpm-store- |
| 129 | +
|
| 130 | + - name: Install Vercel CLI |
| 131 | + run: npm install -g vercel@latest |
| 132 | + |
| 133 | + - name: Set deployment variables |
| 134 | + id: vars |
| 135 | + run: | |
| 136 | + if [ "${{ github.event_name }}" == "pull_request" ]; then |
| 137 | + echo "prod_flag=" >> $GITHUB_OUTPUT |
| 138 | + else |
| 139 | + echo "prod_flag=--prod" >> $GITHUB_OUTPUT |
| 140 | + fi |
| 141 | +
|
| 142 | + - name: Deploy to Vercel |
| 143 | + id: deploy |
| 144 | + continue-on-error: true |
| 145 | + run: | |
| 146 | + # Deploy from repo root - Vercel uses Root Directory from dashboard settings |
| 147 | + # Explicitly passing token via --token flag to ensure authentication |
| 148 | + ERROR_MSG="" |
| 149 | + DEPLOY_URL="" |
| 150 | + set +e # Don't exit on error |
| 151 | + DEPLOY_OUTPUT=$(vercel deploy --token $VERCEL_TOKEN --yes ${{ steps.vars.outputs.prod_flag }} 2>&1) |
| 152 | + DEPLOY_EXIT_CODE=$? |
| 153 | + set -e |
| 154 | +
|
| 155 | + if [ $DEPLOY_EXIT_CODE -eq 0 ]; then |
| 156 | + # Extract the actual deployment URL (not the instruction message) |
| 157 | + DEPLOY_URL=$(echo "$DEPLOY_OUTPUT" | grep -oP 'https://[^\s]+\.vercel\.app[^\s]*' | head -n 1) |
| 158 | + echo "url=$DEPLOY_URL" >> $GITHUB_OUTPUT |
| 159 | + echo "project=${{ matrix.project_name }}" >> $GITHUB_OUTPUT |
| 160 | + echo "error_message=" >> $GITHUB_OUTPUT |
| 161 | + echo "✅ Deployed ${{ matrix.project_name }} to $DEPLOY_URL" >> $GITHUB_STEP_SUMMARY |
| 162 | + else |
| 163 | + echo "url=" >> $GITHUB_OUTPUT |
| 164 | + echo "project=${{ matrix.project_name }}" >> $GITHUB_OUTPUT |
| 165 | + ERROR_MSG=$(echo "$DEPLOY_OUTPUT" | tail -n 5 | tr '\n' ' ') |
| 166 | + echo "error_message=$ERROR_MSG" >> $GITHUB_OUTPUT |
| 167 | + echo "❌ Failed to deploy ${{ matrix.project_name }}: $ERROR_MSG" >> $GITHUB_STEP_SUMMARY |
| 168 | + exit 1 |
| 169 | + fi |
| 170 | + env: |
| 171 | + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} |
| 172 | + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} |
| 173 | + VERCEL_PROJECT_ID: ${{ secrets[matrix.vercel_project_id_secret] }} |
| 174 | + CI: true |
| 175 | + NODE_ENV: production |
| 176 | + |
| 177 | + - name: Update PR comment |
| 178 | + if: always() && github.event_name == 'pull_request' |
| 179 | + uses: actions/github-script@v7 |
| 180 | + with: |
| 181 | + script: | |
| 182 | + const identifier = '<!-- vercel-deployments-comment -->'; |
| 183 | + const projectName = '${{ matrix.project_name }}'; |
| 184 | + const outcome = '${{ steps.deploy.outcome }}'; |
| 185 | + const deployUrl = '${{ steps.deploy.outputs.url }}'; |
| 186 | + const errorMsg = '${{ steps.deploy.outputs.error_message }}'; |
| 187 | + const timestamp = new Date().toLocaleString('en-US', { |
| 188 | + timeZone: 'America/Los_Angeles', |
| 189 | + year: 'numeric', |
| 190 | + month: 'short', |
| 191 | + day: '2-digit', |
| 192 | + hour: '2-digit', |
| 193 | + minute: '2-digit', |
| 194 | + second: '2-digit', |
| 195 | + hour12: true |
| 196 | + }); |
| 197 | +
|
| 198 | + // Determine status |
| 199 | + let status = '⚠️ Unknown'; |
| 200 | + if (outcome === 'success') { |
| 201 | + status = '🟢 Ready'; |
| 202 | + } else if (outcome === 'failure') { |
| 203 | + status = errorMsg ? `❌ Failed: ${errorMsg.substring(0, 100)}...` : '❌ Failed'; |
| 204 | + } else { |
| 205 | + status = '⚠️ Skipped'; |
| 206 | + } |
| 207 | +
|
| 208 | + // Build this project's row |
| 209 | + const projectLink = deployUrl ? `[${projectName}](${deployUrl})` : projectName; |
| 210 | + const previewLink = deployUrl ? `[Preview](${deployUrl})` : 'N/A'; |
| 211 | + const logsLink = `[Logs](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId})`; |
| 212 | + const newRow = `| ${projectLink} | ${status} | ${previewLink}, ${logsLink} | ${timestamp} |`; |
| 213 | +
|
| 214 | + // Find existing comment |
| 215 | + const { data: comments } = await github.rest.issues.listComments({ |
| 216 | + owner: context.repo.owner, |
| 217 | + repo: context.repo.repo, |
| 218 | + issue_number: context.issue.number, |
| 219 | + }); |
| 220 | +
|
| 221 | + const existingComment = comments.find(c => c.body?.includes(identifier)); |
| 222 | + if (!existingComment) { |
| 223 | + console.log('No existing comment found, skipping update'); |
| 224 | + return; |
| 225 | + } |
| 226 | +
|
| 227 | + // Parse and update the table - find the row for this project |
| 228 | + const lines = existingComment.body.split('\n'); |
| 229 | + const updatedLines = lines.map(line => { |
| 230 | + // Match either plain project name or linked project name |
| 231 | + if (line.includes(`| ${projectName} |`) || line.includes(`| [${projectName}](`)) { |
| 232 | + return newRow; |
| 233 | + } |
| 234 | + return line; |
| 235 | + }); |
| 236 | +
|
| 237 | + // Update comment |
| 238 | + await github.rest.issues.updateComment({ |
| 239 | + owner: context.repo.owner, |
| 240 | + repo: context.repo.repo, |
| 241 | + comment_id: existingComment.id, |
| 242 | + body: updatedLines.join('\n') |
| 243 | + }); |
0 commit comments