|
24 | 24 | uses: actions/github-script@v8 |
25 | 25 | with: |
26 | 26 | script: | |
27 | | - const workflowRun = context.payload.workflow_run; |
28 | | - const headBranch = workflowRun?.head_branch; |
29 | | - if (typeof headBranch === 'string' && headBranch.startsWith('hotfix/')) { |
30 | | - core.info('hotfix/* branch detected; skipping CI job verification.'); |
31 | | - return; |
32 | | - } |
33 | | -
|
34 | | - const runId = workflowRun.id; |
| 27 | + const runId = context.payload.workflow_run.id; |
35 | 28 | const requiredJobs = ['Lint', 'Unit Tests']; |
36 | 29 | const { data } = await github.rest.actions.listJobsForWorkflowRun({ |
37 | 30 | owner: context.repo.owner, |
@@ -60,133 +53,24 @@ jobs: |
60 | 53 | github.event.workflow_run.event == 'pull_request' |
61 | 54 |
|
62 | 55 | steps: |
63 | | - - name: Hotfix branch — skip preview deploy |
64 | | - if: ${{ startsWith(github.event.workflow_run.head_branch, 'hotfix/') }} |
65 | | - run: | |
66 | | - echo "hotfix/* branch detected; skipping preview deployment (treat as success)." |
67 | | -
|
68 | 56 | - name: Checkout repository |
69 | | - if: ${{ !startsWith(github.event.workflow_run.head_branch, 'hotfix/') }} |
70 | 57 | uses: actions/checkout@v6 |
71 | 58 | with: |
72 | 59 | ref: ${{ github.event.workflow_run.head_sha }} |
73 | 60 |
|
74 | | - - name: Create GitHub deployment (Preview) |
75 | | - if: ${{ !startsWith(github.event.workflow_run.head_branch, 'hotfix/') }} |
76 | | - id: github-deployment-preview |
77 | | - uses: actions/github-script@v8 |
78 | | - with: |
79 | | - script: | |
80 | | - const workflowRun = context.payload.workflow_run; |
81 | | - const ref = workflowRun?.head_sha; |
82 | | - if (!ref) { |
83 | | - core.setFailed('Missing workflow_run.head_sha; cannot create GitHub deployment.'); |
84 | | - return; |
85 | | - } |
86 | | -
|
87 | | - const { data } = await github.rest.repos.createDeployment({ |
88 | | - owner: context.repo.owner, |
89 | | - repo: context.repo.repo, |
90 | | - ref, |
91 | | - auto_merge: false, |
92 | | - required_contexts: [], |
93 | | - environment: 'vercel-preview', |
94 | | - transient_environment: true, |
95 | | - production_environment: false, |
96 | | - description: 'Vercel preview deployment' |
97 | | - }); |
98 | | -
|
99 | | - core.setOutput('deployment_id', String(data.id)); |
100 | | -
|
101 | 61 | - name: Deploy to Vercel (Preview) |
102 | | - if: ${{ !startsWith(github.event.workflow_run.head_branch, 'hotfix/') }} |
103 | 62 | uses: amondnet/vercel-action@v41.1.4 |
104 | 63 | id: vercel-preview |
105 | 64 | with: |
106 | 65 | vercel-token: ${{ secrets.VERCEL_TOKEN }} |
107 | | - github-token: ${{ secrets.GITHUB_TOKEN }} |
108 | 66 | vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }} |
109 | 67 | vercel-org-id: ${{ secrets.VERCEL_ORG_ID }} |
110 | 68 | github-comment: false |
111 | 69 | env: |
112 | 70 | VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} |
113 | | - GITHUB_SHA: ${{ github.event.workflow_run.head_sha }} |
114 | | - GITHUB_REF: refs/heads/${{ github.event.workflow_run.head_branch }} |
115 | | - |
116 | | - - name: Update GitHub deployment status (Preview) |
117 | | - if: ${{ !startsWith(github.event.workflow_run.head_branch, 'hotfix/') && always() }} |
118 | | - uses: actions/github-script@v8 |
119 | | - env: |
120 | | - VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} |
121 | | - VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} |
122 | | - VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} |
123 | | - with: |
124 | | - script: | |
125 | | - const deploymentIdRaw = '${{ steps.github-deployment-preview.outputs.deployment_id }}'; |
126 | | - const deploymentId = Number(deploymentIdRaw); |
127 | | - if (!deploymentIdRaw || Number.isNaN(deploymentId)) { |
128 | | - core.warning('Missing deployment id; skipping GitHub deployment status update.'); |
129 | | - return; |
130 | | - } |
131 | | -
|
132 | | - const workflowRun = context.payload.workflow_run; |
133 | | - const rawPreviewUrl = '${{ steps.vercel-preview.outputs.preview-url }}'.trim(); |
134 | | - const isVercelUrl = (url) => typeof url === 'string' && /vercel\.(app|com)/.test(url); |
135 | | - const fallbackRunUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
136 | | -
|
137 | | - const fetchDeploymentUrl = async () => { |
138 | | - if (!process.env.VERCEL_TOKEN || !process.env.VERCEL_PROJECT_ID) { |
139 | | - return null; |
140 | | - } |
141 | | - if (typeof fetch !== 'function') { |
142 | | - return null; |
143 | | - } |
144 | | - const query = new URLSearchParams({ |
145 | | - projectId: process.env.VERCEL_PROJECT_ID, |
146 | | - 'meta-githubCommitSha': workflowRun?.head_sha ?? '', |
147 | | - limit: '1' |
148 | | - }); |
149 | | - if (process.env.VERCEL_ORG_ID) { |
150 | | - query.set('teamId', process.env.VERCEL_ORG_ID); |
151 | | - } |
152 | | - const response = await fetch(`https://api.vercel.com/v6/deployments?${query.toString()}`, { |
153 | | - headers: { |
154 | | - Authorization: `Bearer ${process.env.VERCEL_TOKEN}` |
155 | | - } |
156 | | - }); |
157 | | - if (!response.ok) { |
158 | | - return null; |
159 | | - } |
160 | | - const data = await response.json(); |
161 | | - const deployment = data?.deployments?.[0]; |
162 | | - if (deployment?.url) { |
163 | | - return `https://${deployment.url}`; |
164 | | - } |
165 | | - if (deployment?.inspectorUrl) { |
166 | | - return deployment.inspectorUrl.startsWith('http') |
167 | | - ? deployment.inspectorUrl |
168 | | - : `https://${deployment.inspectorUrl}`; |
169 | | - } |
170 | | - return null; |
171 | | - }; |
172 | | -
|
173 | | - const previewUrl = isVercelUrl(rawPreviewUrl) ? rawPreviewUrl : await fetchDeploymentUrl(); |
174 | | - const state = '${{ steps.vercel-preview.outcome }}' === 'success' ? 'success' : 'failure'; |
175 | | - const targetUrl = isVercelUrl(previewUrl) ? previewUrl : fallbackRunUrl; |
176 | | -
|
177 | | - await github.rest.repos.createDeploymentStatus({ |
178 | | - owner: context.repo.owner, |
179 | | - repo: context.repo.repo, |
180 | | - deployment_id: deploymentId, |
181 | | - state, |
182 | | - environment: 'vercel-preview', |
183 | | - environment_url: targetUrl, |
184 | | - log_url: fallbackRunUrl, |
185 | | - description: state === 'success' ? 'Vercel preview is ready' : 'Vercel preview failed' |
186 | | - }); |
187 | 71 |
|
188 | 72 | - name: Comment preview URL on PR |
189 | | - if: ${{ !startsWith(github.event.workflow_run.head_branch, 'hotfix/') && always() && steps.vercel-preview.outcome == 'success' }} |
| 73 | + if: always() && steps.vercel-preview.outcome == 'success' |
190 | 74 | uses: actions/github-script@v8 |
191 | 75 | env: |
192 | 76 | VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} |
@@ -301,7 +185,7 @@ jobs: |
301 | 185 | } |
302 | 186 |
|
303 | 187 | - name: Comment preview failure on PR |
304 | | - if: ${{ !startsWith(github.event.workflow_run.head_branch, 'hotfix/') && always() && steps.vercel-preview.outcome != 'success' }} |
| 188 | + if: always() && steps.vercel-preview.outcome != 'success' |
305 | 189 | uses: actions/github-script@v8 |
306 | 190 | env: |
307 | 191 | VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} |
@@ -389,73 +273,16 @@ jobs: |
389 | 273 | with: |
390 | 274 | ref: ${{ github.event.workflow_run.head_sha }} |
391 | 275 |
|
392 | | - - name: Create GitHub deployment (Production) |
393 | | - id: github-deployment-production |
394 | | - uses: actions/github-script@v8 |
395 | | - with: |
396 | | - script: | |
397 | | - const workflowRun = context.payload.workflow_run; |
398 | | - const ref = workflowRun?.head_sha; |
399 | | - if (!ref) { |
400 | | - core.setFailed('Missing workflow_run.head_sha; cannot create GitHub deployment.'); |
401 | | - return; |
402 | | - } |
403 | | -
|
404 | | - const { data } = await github.rest.repos.createDeployment({ |
405 | | - owner: context.repo.owner, |
406 | | - repo: context.repo.repo, |
407 | | - ref, |
408 | | - auto_merge: false, |
409 | | - required_contexts: [], |
410 | | - environment: 'production', |
411 | | - transient_environment: false, |
412 | | - production_environment: true, |
413 | | - description: 'Vercel production deployment' |
414 | | - }); |
415 | | -
|
416 | | - core.setOutput('deployment_id', String(data.id)); |
417 | | -
|
418 | 276 | - name: Deploy to Vercel (Production) |
419 | 277 | uses: amondnet/vercel-action@v41.1.4 |
420 | 278 | id: vercel-production |
421 | 279 | with: |
422 | 280 | vercel-token: ${{ secrets.VERCEL_TOKEN }} |
423 | | - github-token: ${{ secrets.GITHUB_TOKEN }} |
424 | 281 | vercel-args: '--prod' |
425 | 282 | vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }} |
426 | 283 | vercel-org-id: ${{ secrets.VERCEL_ORG_ID }} |
427 | 284 | env: |
428 | 285 | VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} |
429 | | - GITHUB_SHA: ${{ github.event.workflow_run.head_sha }} |
430 | | - GITHUB_REF: refs/heads/${{ github.event.workflow_run.head_branch }} |
431 | | - |
432 | | - - name: Update GitHub deployment status (Production) |
433 | | - if: ${{ always() }} |
434 | | - uses: actions/github-script@v8 |
435 | | - with: |
436 | | - script: | |
437 | | - const deploymentIdRaw = '${{ steps.github-deployment-production.outputs.deployment_id }}'; |
438 | | - const deploymentId = Number(deploymentIdRaw); |
439 | | - if (!deploymentIdRaw || Number.isNaN(deploymentId)) { |
440 | | - core.warning('Missing deployment id; skipping GitHub deployment status update.'); |
441 | | - return; |
442 | | - } |
443 | | -
|
444 | | - const state = '${{ steps.vercel-production.outcome }}' === 'success' ? 'success' : 'failure'; |
445 | | - const rawUrl = '${{ steps.vercel-production.outputs.preview-url }}'.trim(); |
446 | | - const environmentUrl = rawUrl ? rawUrl : undefined; |
447 | | - const logUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
448 | | -
|
449 | | - await github.rest.repos.createDeploymentStatus({ |
450 | | - owner: context.repo.owner, |
451 | | - repo: context.repo.repo, |
452 | | - deployment_id: deploymentId, |
453 | | - state, |
454 | | - environment: 'production', |
455 | | - environment_url: environmentUrl, |
456 | | - log_url: logUrl, |
457 | | - description: state === 'success' ? 'Production deploy completed' : 'Production deploy failed' |
458 | | - }); |
459 | 286 |
|
460 | 287 | - name: Log production deployment |
461 | 288 | if: always() && steps.vercel-production.outcome == 'success' |
|
0 commit comments