Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

mobile-eas-preview-fork.yml

506 lines21.8 KB

1name: Mobile EAS Preview (Fork)
2run-name: "${{ github.event.label.name == 'πŸš€ Mobile Continuous Deployment' && github.event.pull_request.head.repo.full_name != github.repository && format('Mobile preview of #{0} at {1}', github.event.pull_request.number, github.event.pull_request.head.sha) || '' }}"
3​
4# EAS previews for fork PRs. This runs the PR's code with an Expo token: the
5# app config and plugins run on a runner; both clients build on Blacksmith
6# with preview signing credentials. So every job that does waits for an
7# approval in the `mobile-preview-fork` environment on every run.
8#
9# The label only requests a build, and counts only from someone with write
10# access. The `request` job consumes it, pins the PR's head commit from the
11# label event, and names that commit in the run title, the job summary and a
12# PR comment. It reads no secrets and checks out nothing. The approval is the
13# sign-off that the named commit was reviewed: the jobs after it build that
14# commit and no other. A later push deploys nothing until the label is applied
15# again and that run is approved.
17# Same-repo PRs deploy on every push while labeled, via mobile-eas-preview.yml.
18​
19on:
20 pull_request_target:
21 types: [labeled]
22​
23permissions:
24 contents: read
25​
26jobs:
27 request:
28 name: Preview request
29 if: >-
30 github.event.pull_request.head.repo.full_name != github.repository &&
31 github.event.label.name == 'πŸš€ Mobile Continuous Deployment'
32 runs-on: blacksmith-2vcpu-ubuntu-2404
33 permissions:
34 contents: read
35 pull-requests: write
36 outputs:
37 sha: ${{ steps.pin.outputs.sha }}
38 steps:
39 # The label counts only from someone with write access, who could push a
40 # same-repo branch and get this build anyway. If a maintainer removed the
41 # label first, that removal wins.
42 - id: consume
43 name: Consume the deployment label
44 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
45 with:
46 script: |
47 try {
48 await github.rest.issues.removeLabel({
49 owner: context.repo.owner,
50 repo: context.repo.repo,
51 issue_number: context.payload.pull_request.number,
52 name: context.payload.label.name,
53 });
54 } catch (error) {
55 if (error.status !== 404) throw error;
56 core.info("The label was removed before this run could consume it. Skipping.");
57 core.setOutput("consumed", "false");
58 return;
59 }
60 const sender = context.payload.sender.login;
61 const { data: access } = await github.rest.repos.getCollaboratorPermissionLevel({
62 owner: context.repo.owner,
63 repo: context.repo.repo,
64 username: sender,
65 });
66 const authorized = ["admin", "maintain", "write"].includes(access.permission);
67 if (!authorized) {
68 core.warning(`${sender} has ${access.permission} access; a fork preview needs write access. Label removed, nothing built.`);
69 }
70 core.setOutput("consumed", String(authorized));
71​
72 # The review screen of a pull_request_target run shows the base branch,
73 # so the commit is named here, where the reviewer reads it before
74 # approving. The jobs below take it from this job's output. Nothing is
75 # requested unless the environment exists with required reviewers: a
76 # workflow that names a missing environment creates it without any.
77 - id: pin
78 name: Name the commit to build
79 if: steps.consume.outputs.consumed == 'true'
80 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
81 env:
82 ENVIRONMENT: mobile-preview-fork
83 with:
84 script: |
85 const name = process.env.ENVIRONMENT;
86 let rules = [];
87 try {
88 const { data } = await github.request("GET /repos/{owner}/{repo}/environments/{environment_name}", {
89 owner: context.repo.owner,
90 repo: context.repo.repo,
91 environment_name: name,
92 });
93 rules = data.protection_rules ?? [];
94 } catch (error) {
95 if (error.status !== 404) throw error;
96 }
97 if (!rules.some((rule) => rule.type === "required_reviewers" && rule.reviewers?.length > 0)) {
98 core.setFailed(`The ${name} environment has no required reviewers. Nothing built.`);
99 return;
100 }
101 const pull = context.payload.pull_request;
102 const sha = pull.head.sha;
103 if (!/^[0-9a-f]{40}$/.test(sha)) {
104 core.setFailed("The label event carries no head commit. Nothing built.");
105 return;
106 }
107 const repository = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}`;
108 const body = [
109 `Mobile preview requested by ${context.payload.sender.login} for commit [\`${sha}\`](${repository}/pull/${pull.number}/commits/${sha}).`,
110 "",
111 `It builds after an approval in the \`${name}\` environment: [review this run](${repository}/actions/runs/${context.runId}). Approving builds exactly this commit; a later push needs the label again.`,
112 ].join("\n");
113 await core.summary.addRaw(body, true).write();
114 await github.rest.issues.createComment({
115 owner: context.repo.owner,
116 repo: context.repo.repo,
117 issue_number: pull.number,
118 body,
119 });
120 core.setOutput("sha", sha);
121​
122 preview:
123 name: EAS Preview
124 needs: request
125 if: needs.request.outputs.sha != ''
126 runs-on: blacksmith-8vcpu-ubuntu-2404
127 environment:
128 name: mobile-preview-fork
129 concurrency:
130 group: mobile-eas-preview-fork-${{ github.event.pull_request.number }}
131 cancel-in-progress: true
132 permissions:
133 contents: read
134 pull-requests: read
135 outputs:
136 ios_group: ${{ steps.ios_update.outputs.group }}
137 ios_client: ${{ steps.ios_client.outputs.link }}
138 group: ${{ steps.update.outputs.group }}
139 client: ${{ steps.client.outputs.link }}
140 ios_build: ${{ steps.ios_client.outputs.build }}
141 ios_fingerprint: ${{ steps.ios_client.outputs.fingerprint }}
142 build: ${{ steps.client.outputs.build }}
143 fingerprint: ${{ steps.client.outputs.fingerprint }}
144 env:
145 APP_VARIANT: preview
146 NODE_OPTIONS: --max-old-space-size=8192
147 MOBILE_VERSION_POLICY: fingerprint
148 steps:
149 - name: Confirm the PR is still open
150 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
151 with:
152 script: |
153 const { data: pull } = await github.rest.pulls.get({
154 owner: context.repo.owner,
155 repo: context.repo.repo,
156 pull_number: context.payload.pull_request.number,
157 });
158 if (pull.state !== "open") core.setFailed(`PR #${pull.number} is no longer open; not building.`);
159​
160 - id: expo-token
161 name: Check for EXPO_PREVIEW_TOKEN
162 env:
163 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
164 run: |
165 if [ -n "$EXPO_TOKEN" ]; then
166 echo "present=true" >> "$GITHUB_OUTPUT"
167 else
168 echo "present=false" >> "$GITHUB_OUTPUT"
169 echo "EXPO_PREVIEW_TOKEN is not set; skipping EAS preview."
170 fi
171​
172 # The commit the request job named, not the branch, so a push after the
173 # label builds nothing. No credentials stay in .git for the PR's scripts
174 # to read.
175 - name: Checkout reviewed commit
176 if: steps.expo-token.outputs.present == 'true'
177 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
178 with:
179 ref: ${{ needs.request.outputs.sha }}
180 fetch-depth: 0
181 # Fork code by design; the environment approval above is the gate.
182 allow-unsafe-pr-checkout: true
183 # No sparse-checkout: see mobile-eas-preview.yml.
184​
185 # Scripts and actions come from main, not from the PR.
186 - name: Checkout trusted scripts
187 if: steps.expo-token.outputs.present == 'true'
188 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
189 with:
190 path: .trusted
191 sparse-checkout: |
192 .github/scripts
193 .github/actions
194​
195 # The steps below run dependency and build scripts; they get no token.
196 - name: Drop checkout credentials
197 if: steps.expo-token.outputs.present == 'true'
198 uses: ./.trusted/.github/actions/drop-checkout-credentials
199​
200 # No caches: pull_request_target saves them under main's scope, where
201 # trusted workflows with other secrets would restore what PR code wrote.
202 - name: Setup Vite+
203 if: steps.expo-token.outputs.present == 'true'
204 uses: voidzero-dev/setup-vp@250f29ce396baf5e8f24498e17c0dfdebabc26eb # v1.15.0
205 with:
206 node-version-file: package.json
207 cache: false
208 run-install: |
209 args:
210 - --filter=@t3tools/mobile...
211​
212 - name: Expose pnpm
213 if: steps.expo-token.outputs.present == 'true'
214 run: |
215 pnpm_version="$(node --print "require('./package.json').packageManager.split('@').pop()")"
216 vp_pnpm_bin="$HOME/.vite-plus/package_manager/pnpm/$pnpm_version/pnpm/bin"
217 echo "$vp_pnpm_bin" >> "$GITHUB_PATH"
218 "$vp_pnpm_bin/pnpm" --version
219​
220 - name: Setup EAS
221 if: steps.expo-token.outputs.present == 'true'
222 uses: expo/expo-github-action@c7b66a9c327a43a8fa7c0158e7f30d6040d2481e # v8.2.1
223 with:
224 eas-version: latest
225 eas-cache: false
226 token: ${{ secrets.EXPO_PREVIEW_TOKEN }}
227 packager: npm
228​
229 - name: Pull preview environment variables
230 if: steps.expo-token.outputs.present == 'true'
231 working-directory: apps/mobile
232 env:
233 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
234 run: |
235 eas env:pull preview --non-interactive
236 # See mobile-eas-preview.yml: keeps Android build and OTA fingerprints matching.
237 node --input-type=module <<'NODE'
238 import { appendFileSync, readFileSync } from "node:fs";
239 import { parseEnv } from "node:util";
240 const env = parseEnv(readFileSync(".env.local", "utf8"));
241 const googleServicesFile = env.T3CODE_ANDROID_GOOGLE_SERVICES_FILE;
242 if (googleServicesFile) {
243 appendFileSync(process.env.GITHUB_ENV, `T3CODE_ANDROID_GOOGLE_SERVICES_FILE=${googleServicesFile}\n`);
244 }
245 NODE
246​
247 - id: ios_client
248 name: Find the iOS client for this fingerprint
249 if: steps.expo-token.outputs.present == 'true'
250 working-directory: apps/mobile
251 shell: bash
252 env:
253 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
254 run: |
255 hash="$(eas env:exec --non-interactive preview "npx expo-updates fingerprint:generate --platform ios" | sed -n '/^{/,$p' | jq -er '.hash | select(type == "string" and length > 0)')"
256 echo "fingerprint=$hash" >> "$GITHUB_OUTPUT"
257 tag="mobile-ios-preview-build/$hash"
258 link=""
259 if git fetch --quiet origin "refs/tags/$tag:refs/tags/$tag" 2>/dev/null; then
260 link="$(git tag -l --format='%(contents)' "$tag" | node "$GITHUB_WORKSPACE/.trusted/.github/scripts/mobile-ios-preview-record.cjs" read)"
261 fi
262 if [ -z "$link" ]; then
263 link="$(eas build:list --platform ios --build-profile preview:dev --runtime-version "$hash" --status finished --limit 5 --json --non-interactive \
264 | sed -n '/^\[/,$p' \
265 | jq -r --arg now "$(date -u +%Y-%m-%dT%H:%M:%SZ)" '[.[] | select((.expirationDate // "9999") > $now)][0] | select(. != null) | "https://expo.dev/accounts/pingdotgg/projects/t3-code/builds/\(.id)"')"
266 fi
267 echo "link=$link" >> "$GITHUB_OUTPUT"
268 if [ -z "$link" ]; then echo "build=true" >> "$GITHUB_OUTPUT"; fi
269​
270 - id: ios_update
271 name: Publish the iOS update
272 if: steps.expo-token.outputs.present == 'true'
273 working-directory: apps/mobile
274 shell: bash
275 env:
276 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
277 PR: ${{ github.event.pull_request.number }}
278 run: |
279 group="$(eas update --auto --branch "pr-$PR" --platform ios --environment preview --non-interactive --json | sed -n '/^\[/,$p' | jq -er '.[0].group')"
280 echo "group=$group" >> "$GITHUB_OUTPUT"
281​
282 # Android, as in mobile-eas-preview.yml: the same fingerprint command the Expo
283 # action runs. A client built for a same-repo PR (its tag) or by EAS is reused.
284 - id: client
285 name: Find the Android client for this fingerprint
286 if: steps.expo-token.outputs.present == 'true'
287 working-directory: apps/mobile
288 shell: bash
289 env:
290 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
291 run: |
292 hash="$(eas env:exec --non-interactive preview "npx expo-updates fingerprint:generate --platform android" | sed -n '/^{/,$p' | jq -er '.hash | select(type == "string" and length > 0)')"
293 echo "fingerprint=$hash" >> "$GITHUB_OUTPUT"
294 tag="mobile-android-preview-build/$hash"
295 link=""
296 if git fetch --quiet origin "refs/tags/$tag:refs/tags/$tag" 2>/dev/null; then
297 link="$(git tag -l --format='%(contents:subject)' "$tag")"
298 else
299 link="$(eas build:list --platform android --build-profile preview:dev --runtime-version "$hash" --status finished --limit 5 --json --non-interactive \
300 | sed -n '/^\[/,$p' \
301 | jq -r --arg now "$(date -u +%Y-%m-%dT%H:%M:%SZ)" '[.[] | select((.expirationDate // "9999") > $now)][0] | select(. != null) | "https://expo.dev/accounts/pingdotgg/projects/t3-code/builds/\(.id)"')"
302 fi
303 echo "link=$link" >> "$GITHUB_OUTPUT"
304 if [ -z "$link" ]; then echo "build=true" >> "$GITHUB_OUTPUT"; fi
305​
306 - id: update
307 name: Publish the Android update
308 if: steps.expo-token.outputs.present == 'true'
309 working-directory: apps/mobile
310 shell: bash
311 env:
312 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
313 PR: ${{ github.event.pull_request.number }}
314 run: |
315 group="$(eas update --auto --branch "pr-$PR" --platform android --environment preview --non-interactive --json | sed -n '/^\[/,$p' | jq -er '.[0].group')"
316 echo "group=$group" >> "$GITHUB_OUTPUT"
317​
318 # Each client job waits for its own approval in the environment, once the
319 # preview job has finished. The reviewed commit's Android client, built cold: no sticky disk, because PR
320 # code could leave Gradle or ccache state that trusted builds would restore, and
321 # no build record tag, so nobody else is served an APK a fork's code built.
322 android-build:
323 name: Android preview client
324 needs: [request, preview]
325 if: needs.preview.outputs.build == 'true'
326 runs-on: blacksmith-32vcpu-ubuntu-2404
327 timeout-minutes: 60
328 environment:
329 name: mobile-preview-fork
330 permissions:
331 contents: read
332 pull-requests: read
333 outputs:
334 client: ${{ steps.build.outputs.link }}
335 env:
336 APP_VARIANT: preview
337 NODE_OPTIONS: --max-old-space-size=8192
338 MOBILE_VERSION_POLICY: fingerprint
339 FINGERPRINT: ${{ needs.preview.outputs.fingerprint }}
340 # Phones only: emulators use the development client from the native-client cache.
341 ORG_GRADLE_PROJECT_reactNativeArchitectures: armeabi-v7a,arm64-v8a
342 steps:
343 - name: Confirm the PR is still open
344 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
345 with:
346 script: |
347 const { data: pull } = await github.rest.pulls.get({
348 owner: context.repo.owner,
349 repo: context.repo.repo,
350 pull_number: context.payload.pull_request.number,
351 });
352 if (pull.state !== "open") core.setFailed(`PR #${pull.number} is no longer open; not building.`);
353​
354 - name: Checkout reviewed commit
355 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
356 with:
357 ref: ${{ needs.request.outputs.sha }}
358 fetch-depth: 0
359 # Fork code by design; the environment approval above is the gate.
360 allow-unsafe-pr-checkout: true
361​
362 # Scripts and actions come from main, not from the PR.
363 - name: Checkout trusted scripts
364 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
365 with:
366 path: .trusted
367 sparse-checkout: |
368 .github/scripts
369 .github/actions
370​
371 # The steps below run dependency and build scripts; they get no token.
372 - name: Drop checkout credentials
373 uses: ./.trusted/.github/actions/drop-checkout-credentials
374​
375 # Cold: no sticky disk or EAS cache that trusted builds would restore.
376 - name: Build
377 uses: ./.trusted/.github/actions/eas-local-build
378 with:
379 platform: android
380 profile: preview:dev
381 environment: preview
382 expo-token: ${{ secrets.EXPO_PREVIEW_TOKEN }}
383 output: ${{ runner.temp }}/t3code-preview.apk
384 cache: "false"
385​
386 - id: build
387 name: Upload
388 working-directory: apps/mobile
389 shell: bash
390 env:
391 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
392 run: |
393 eas upload --platform android --build-path "$RUNNER_TEMP/t3code-preview.apk" --fingerprint "$FINGERPRINT" --non-interactive --json | tee "$RUNNER_TEMP/upload.json"
394 link="$(sed -n '/^{/,$p' "$RUNNER_TEMP/upload.json" | jq -r '.url // empty')"
395 if [ -z "$link" ]; then
396 echo "::error::eas upload returned no install link"
397 exit 1
398 fi
399 echo "link=$link" >> "$GITHUB_OUTPUT"
400​
401 ios-build:
402 name: iOS preview client
403 needs: [request, preview]
404 if: needs.preview.outputs.ios_build == 'true'
405 runs-on: blacksmith-12vcpu-macos-27
406 timeout-minutes: 60
407 environment:
408 name: mobile-preview-fork
409 permissions:
410 contents: read
411 pull-requests: read
412 outputs:
413 client: ${{ steps.build.outputs.link }}
414 env:
415 APP_VARIANT: preview
416 NODE_OPTIONS: --max-old-space-size=8192
417 MOBILE_VERSION_POLICY: fingerprint
418 FINGERPRINT: ${{ needs.preview.outputs.ios_fingerprint }}
419 steps:
420 - name: Confirm the PR is still open
421 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
422 with:
423 script: |
424 const { data: pull } = await github.rest.pulls.get({
425 owner: context.repo.owner,
426 repo: context.repo.repo,
427 pull_number: context.payload.pull_request.number,
428 });
429 if (pull.state !== "open") core.setFailed(`PR #${pull.number} is no longer open; not building.`);
430​
431 - name: Checkout reviewed commit
432 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
433 with:
434 ref: ${{ needs.request.outputs.sha }}
435 fetch-depth: 0
436 # Fork code by design; the environment approval above is the gate.
437 allow-unsafe-pr-checkout: true
438​
439 # Scripts and actions come from main, not from the PR.
440 - name: Checkout trusted scripts
441 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
442 with:
443 path: .trusted
444 sparse-checkout: |
445 .github/scripts
446 .github/actions
447​
448 # The steps below run dependency and build scripts; they get no token.
449 - name: Drop checkout credentials
450 uses: ./.trusted/.github/actions/drop-checkout-credentials
451​
452 # Cold: no sticky disk or EAS cache that trusted builds would restore.
453 - name: Build
454 uses: ./.trusted/.github/actions/eas-local-build
455 with:
456 platform: ios
457 profile: preview:dev
458 environment: preview
459 expo-token: ${{ secrets.EXPO_PREVIEW_TOKEN }}
460 output: ${{ runner.temp }}/t3code-preview.ipa
461 cache: "false"
462​
463 - id: build
464 name: Upload
465 working-directory: apps/mobile
466 shell: bash
467 env:
468 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
469 run: |
470 eas upload --platform ios --build-path "$RUNNER_TEMP/t3code-preview.ipa" --fingerprint "$FINGERPRINT" --non-interactive --json | tee "$RUNNER_TEMP/upload.json"
471 link="$(sed -n '/^{/,$p' "$RUNNER_TEMP/upload.json" | jq -r '.url // empty')"
472 if [ -z "$link" ]; then
473 echo "::error::eas upload returned no install link"
474 exit 1
475 fi
476 echo "link=$link" >> "$GITHUB_OUTPUT"
477​
478 comment:
479 name: Mobile preview comment
480 needs: [request, preview, android-build, ios-build]
481 if: always() && !cancelled() && needs.preview.result == 'success'
482 runs-on: blacksmith-2vcpu-ubuntu-2404
483 concurrency:
484 group: mobile-preview-comment-${{ github.event.pull_request.number }}
485 cancel-in-progress: false
486 permissions:
487 contents: read
488 pull-requests: write
489 steps:
490 - name: Checkout comment script
491 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
492 with:
493 sparse-checkout: .github/scripts
494​
495 - name: Comment
496 env:
497 GH_TOKEN: ${{ github.token }}
498 PR: ${{ github.event.pull_request.number }}
499 PR_HEAD_SHA: ${{ needs.request.outputs.sha }}
500 ANDROID_FINGERPRINT: ${{ needs.preview.outputs.fingerprint }}
501 ANDROID_GROUP: ${{ needs.preview.outputs.group }}
502 ANDROID_CLIENT: ${{ needs.android-build.outputs.client || needs.preview.outputs.client }}
503 IOS_FINGERPRINT: ${{ needs.preview.outputs.ios_fingerprint }}
504 IOS_GROUP: ${{ needs.preview.outputs.ios_group }}
505 IOS_CLIENT: ${{ needs.ios-build.outputs.client || needs.preview.outputs.ios_client }}
506 run: node .github/scripts/mobile-preview-comment.cjs