11name : Preview Deployment
22
33on :
4+ workflow_run :
5+ workflows : ["CI"]
6+ types : [completed]
47 pull_request :
5- types : [opened, reopened, synchronize, closed]
8+ types : [closed]
69
710env :
811 FLY_API_TOKEN : ${{ secrets.FLY_PREVIEW_API_TOKEN }}
@@ -16,10 +19,13 @@ permissions:
1619jobs :
1720 preview :
1821 runs-on : ubuntu-latest
22+ if : >
23+ (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') ||
24+ (github.event_name == 'pull_request' && github.event.action == 'closed')
1925 outputs :
2026 url : ${{ steps.deploy.outputs.url }}
2127 concurrency :
22- group : pr-${{ github.event.number }}
28+ group : pr-${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0]. number }}
2329
2430 environment :
2531 name : preview
2935 - name : Checkout
3036 uses : actions/checkout@v4
3137 with :
38+ ref : ${{ github.event.workflow_run.head_sha || github.event.pull_request.head.sha }}
3239 fetch-depth : 0
3340
3441 - name : Deploy preview app
@@ -40,20 +47,25 @@ jobs:
4047 memory : 256
4148
4249 - name : Comment on PR
43- if : github.event.action != 'closed '
50+ if : github.event_name == 'workflow_run '
4451 continue-on-error : true
4552 uses : actions/github-script@v7
4653 with :
4754 script : |
4855 const url = '${{ steps.deploy.outputs.url }}';
49- const sha = context.sha.substring(0, 7);
56+ const prNumber = ${{ github.event.workflow_run.pull_requests[0].number || 0 }};
57+ if (!prNumber) {
58+ console.log('No PR number found, skipping comment');
59+ return;
60+ }
61+ const sha = '${{ github.event.workflow_run.head_sha }}'.substring(0, 7);
5062 const body = `### Preview Deployment\n\n| Name | URL |\n|------|-----|\n| Preview | ${url} |\n\nCommit: \`${sha}\``;
5163
5264 // Find existing comment
5365 const { data: comments } = await github.rest.issues.listComments({
5466 owner: context.repo.owner,
5567 repo: context.repo.repo,
56- issue_number: context.issue.number ,
68+ issue_number: prNumber ,
5769 });
5870 const existing = comments.find(c => c.body.includes('### Preview Deployment'));
5971
6880 await github.rest.issues.createComment({
6981 owner: context.repo.owner,
7082 repo: context.repo.repo,
71- issue_number: context.issue.number ,
83+ issue_number: prNumber ,
7284 body,
7385 });
7486 }
7789 name : E2E Tests
7890 runs-on : ubuntu-latest
7991 needs : preview
80- if : github.event.action != 'closed '
92+ if : github.event_name == 'workflow_run '
8193 continue-on-error : true
8294
8395 steps :
@@ -144,14 +156,19 @@ jobs:
144156 uses : actions/github-script@v7
145157 with :
146158 script : |
159+ const prNumber = ${{ github.event.workflow_run.pull_requests[0].number || 0 }};
160+ if (!prNumber) {
161+ console.log('No PR number found, skipping comment');
162+ return;
163+ }
147164 const status = '${{ job.status }}';
148165 const emoji = status === 'success' ? '✅' : '❌';
149166 const body = `### E2E Test Results\n\n${emoji} Tests ${status}\n\nRan against: ${{ needs.preview.outputs.url }}`;
150167
151168 const { data: comments } = await github.rest.issues.listComments({
152169 owner: context.repo.owner,
153170 repo: context.repo.repo,
154- issue_number: context.issue.number ,
171+ issue_number: prNumber ,
155172 });
156173 const existing = comments.find(c => c.body.includes('### E2E Test Results'));
157174
@@ -166,7 +183,7 @@ jobs:
166183 await github.rest.issues.createComment({
167184 owner: context.repo.owner,
168185 repo: context.repo.repo,
169- issue_number: context.issue.number ,
186+ issue_number: prNumber ,
170187 body,
171188 });
172189 }
@@ -175,7 +192,7 @@ jobs:
175192 name : Lighthouse CI
176193 runs-on : ubuntu-latest
177194 needs : preview
178- if : github.event.action != 'closed '
195+ if : github.event_name == 'workflow_run '
179196 continue-on-error : true
180197
181198 steps :
@@ -201,10 +218,15 @@ jobs:
201218 uses : actions/github-script@v7
202219 with :
203220 script : |
221+ const prNumber = ${{ github.event.workflow_run.pull_requests[0].number || 0 }};
222+ if (!prNumber) {
223+ core.setOutput('urls', '${{ needs.preview.outputs.url }}');
224+ return;
225+ }
204226 const { data: files } = await github.rest.pulls.listFiles({
205227 owner: context.repo.owner,
206228 repo: context.repo.repo,
207- pull_number: context.issue.number ,
229+ pull_number: prNumber ,
208230 });
209231
210232 const baseUrl = '${{ needs.preview.outputs.url }}';
@@ -243,6 +265,11 @@ jobs:
243265 uses : actions/github-script@v7
244266 with :
245267 script : |
268+ const prNumber = ${{ github.event.workflow_run.pull_requests[0].number || 0 }};
269+ if (!prNumber) {
270+ console.log('No PR number found, skipping comment');
271+ return;
272+ }
246273 const manifest = ${{ steps.lighthouse.outputs.manifest }};
247274 const links = ${{ steps.lighthouse.outputs.links }};
248275
@@ -269,7 +296,7 @@ jobs:
269296 const { data: comments } = await github.rest.issues.listComments({
270297 owner: context.repo.owner,
271298 repo: context.repo.repo,
272- issue_number: context.issue.number ,
299+ issue_number: prNumber ,
273300 });
274301 const existing = comments.find(c => c.body.includes('### Lighthouse Results'));
275302
@@ -284,7 +311,7 @@ jobs:
284311 await github.rest.issues.createComment({
285312 owner: context.repo.owner,
286313 repo: context.repo.repo,
287- issue_number: context.issue.number ,
314+ issue_number: prNumber ,
288315 body,
289316 });
290317 }
0 commit comments