Open History to see the last commit
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.
16#
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 # No sparse-checkout: see mobile-eas-preview.yml.
182β
183 # Scripts and actions come from main, not from the PR.
184 - name: Checkout trusted scripts
185 if: steps.expo-token.outputs.present == 'true'
186 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
187 with:
188 path: .trusted
189 sparse-checkout: |
190 .github/scripts
191 .github/actions
192β
193 # The steps below run dependency and build scripts; they get no token.
194 - name: Drop checkout credentials
195 if: steps.expo-token.outputs.present == 'true'
196 uses: ./.trusted/.github/actions/drop-checkout-credentials
197β
198 # No caches: pull_request_target saves them under main's scope, where
199 # trusted workflows with other secrets would restore what PR code wrote.
200 - name: Setup Vite+
201 if: steps.expo-token.outputs.present == 'true'
202 uses: voidzero-dev/setup-vp@250f29ce396baf5e8f24498e17c0dfdebabc26eb # v1.15.0
203 with:
204 node-version-file: package.json
205 cache: false
206 run-install: |
207 args:
208 - --filter=@t3tools/mobile...
209β
210 - name: Expose pnpm
211 if: steps.expo-token.outputs.present == 'true'
212 run: |
213 pnpm_version="$(node --print "require('./package.json').packageManager.split('@').pop()")"
214 vp_pnpm_bin="$HOME/.vite-plus/package_manager/pnpm/$pnpm_version/pnpm/bin"
215 echo "$vp_pnpm_bin" >> "$GITHUB_PATH"
216 "$vp_pnpm_bin/pnpm" --version
217β
218 - name: Setup EAS
219 if: steps.expo-token.outputs.present == 'true'
220 uses: expo/expo-github-action@c7b66a9c327a43a8fa7c0158e7f30d6040d2481e # v8.2.1
221 with:
222 eas-version: latest
223 eas-cache: false
224 token: ${{ secrets.EXPO_PREVIEW_TOKEN }}
225 packager: npm
226β
227 - name: Pull preview environment variables
228 if: steps.expo-token.outputs.present == 'true'
229 working-directory: apps/mobile
230 env:
231 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
232 run: |
233 eas env:pull preview --non-interactive
234 # See mobile-eas-preview.yml: keeps Android build and OTA fingerprints matching.
235 node --input-type=module <<'NODE'
236 import { appendFileSync, readFileSync } from "node:fs";
237 import { parseEnv } from "node:util";
238 const env = parseEnv(readFileSync(".env.local", "utf8"));
239 const googleServicesFile = env.T3CODE_ANDROID_GOOGLE_SERVICES_FILE;
240 if (googleServicesFile) {
241 appendFileSync(process.env.GITHUB_ENV, `T3CODE_ANDROID_GOOGLE_SERVICES_FILE=${googleServicesFile}\n`);
242 }
243 NODE
244β
245 - id: ios_client
246 name: Find the iOS client for this fingerprint
247 if: steps.expo-token.outputs.present == 'true'
248 working-directory: apps/mobile
249 shell: bash
250 env:
251 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
252 run: |
253 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)')"
254 echo "fingerprint=$hash" >> "$GITHUB_OUTPUT"
255 tag="mobile-ios-preview-build/$hash"
256 link=""
257 if git fetch --quiet origin "refs/tags/$tag:refs/tags/$tag" 2>/dev/null; then
258 link="$(git tag -l --format='%(contents)' "$tag" | node "$GITHUB_WORKSPACE/.trusted/.github/scripts/mobile-ios-preview-record.cjs" read)"
259 fi
260 if [ -z "$link" ]; then
261 link="$(eas build:list --platform ios --build-profile preview:dev --runtime-version "$hash" --status finished --limit 5 --json --non-interactive \
262 | sed -n '/^\[/,$p' \
263 | 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)"')"
264 fi
265 echo "link=$link" >> "$GITHUB_OUTPUT"
266 if [ -z "$link" ]; then echo "build=true" >> "$GITHUB_OUTPUT"; fi
267β
268 - id: ios_update
269 name: Publish the iOS update
270 if: steps.expo-token.outputs.present == 'true'
271 working-directory: apps/mobile
272 shell: bash
273 env:
274 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
275 PR: ${{ github.event.pull_request.number }}
276 run: |
277 group="$(eas update --auto --branch "pr-$PR" --platform ios --environment preview --non-interactive --json | sed -n '/^\[/,$p' | jq -er '.[0].group')"
278 echo "group=$group" >> "$GITHUB_OUTPUT"
279β
280 # Android, as in mobile-eas-preview.yml: the same fingerprint command the Expo
281 # action runs. A client built for a same-repo PR (its tag) or by EAS is reused.
282 - id: client
283 name: Find the Android client for this fingerprint
284 if: steps.expo-token.outputs.present == 'true'
285 working-directory: apps/mobile
286 shell: bash
287 env:
288 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
289 run: |
290 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)')"
291 echo "fingerprint=$hash" >> "$GITHUB_OUTPUT"
292 tag="mobile-android-preview-build/$hash"
293 link=""
294 if git fetch --quiet origin "refs/tags/$tag:refs/tags/$tag" 2>/dev/null; then
295 link="$(git tag -l --format='%(contents:subject)' "$tag")"
296 else
297 link="$(eas build:list --platform android --build-profile preview:dev --runtime-version "$hash" --status finished --limit 5 --json --non-interactive \
298 | sed -n '/^\[/,$p' \
299 | 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)"')"
300 fi
301 echo "link=$link" >> "$GITHUB_OUTPUT"
302 if [ -z "$link" ]; then echo "build=true" >> "$GITHUB_OUTPUT"; fi
303β
304 - id: update
305 name: Publish the Android update
306 if: steps.expo-token.outputs.present == 'true'
307 working-directory: apps/mobile
308 shell: bash
309 env:
310 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
311 PR: ${{ github.event.pull_request.number }}
312 run: |
313 group="$(eas update --auto --branch "pr-$PR" --platform android --environment preview --non-interactive --json | sed -n '/^\[/,$p' | jq -er '.[0].group')"
314 echo "group=$group" >> "$GITHUB_OUTPUT"
315β
316 # Each client job waits for its own approval in the environment, once the
317 # preview job has finished. The reviewed commit's Android client, built cold: no sticky disk, because PR
318 # code could leave Gradle or ccache state that trusted builds would restore, and
319 # no build record tag, so nobody else is served an APK a fork's code built.
320 android-build:
321 name: Android preview client
322 needs: [request, preview]
323 if: needs.preview.outputs.build == 'true'
324 runs-on: blacksmith-32vcpu-ubuntu-2404
325 timeout-minutes: 60
326 environment:
327 name: mobile-preview-fork
328 permissions:
329 contents: read
330 pull-requests: read
331 outputs:
332 client: ${{ steps.build.outputs.link }}
333 env:
334 APP_VARIANT: preview
335 NODE_OPTIONS: --max-old-space-size=8192
336 MOBILE_VERSION_POLICY: fingerprint
337 FINGERPRINT: ${{ needs.preview.outputs.fingerprint }}
338 # Phones only: emulators use the development client from the native-client cache.
339 ORG_GRADLE_PROJECT_reactNativeArchitectures: armeabi-v7a,arm64-v8a
340 steps:
341 - name: Confirm the PR is still open
342 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
343 with:
344 script: |
345 const { data: pull } = await github.rest.pulls.get({
346 owner: context.repo.owner,
347 repo: context.repo.repo,
348 pull_number: context.payload.pull_request.number,
349 });
350 if (pull.state !== "open") core.setFailed(`PR #${pull.number} is no longer open; not building.`);
351β
352 - name: Checkout reviewed commit
353 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
354 with:
355 ref: ${{ needs.request.outputs.sha }}
356 fetch-depth: 0
357β
358 # Scripts and actions come from main, not from the PR.
359 - name: Checkout trusted scripts
360 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
361 with:
362 path: .trusted
363 sparse-checkout: |
364 .github/scripts
365 .github/actions
366β
367 # The steps below run dependency and build scripts; they get no token.
368 - name: Drop checkout credentials
369 uses: ./.trusted/.github/actions/drop-checkout-credentials
370β
371 # Cold: no sticky disk or EAS cache that trusted builds would restore.
372 - name: Build
373 uses: ./.trusted/.github/actions/eas-local-build
374 with:
375 platform: android
376 profile: preview:dev
377 environment: preview
378 expo-token: ${{ secrets.EXPO_PREVIEW_TOKEN }}
379 output: ${{ runner.temp }}/t3code-preview.apk
380 cache: "false"
381β
382 - id: build
383 name: Upload
384 working-directory: apps/mobile
385 shell: bash
386 env:
387 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
388 run: |
389 eas upload --platform android --build-path "$RUNNER_TEMP/t3code-preview.apk" --fingerprint "$FINGERPRINT" --non-interactive --json | tee "$RUNNER_TEMP/upload.json"
390 link="$(sed -n '/^{/,$p' "$RUNNER_TEMP/upload.json" | jq -r '.url // empty')"
391 if [ -z "$link" ]; then
392 echo "::error::eas upload returned no install link"
393 exit 1
394 fi
395 echo "link=$link" >> "$GITHUB_OUTPUT"
396β
397 ios-build:
398 name: iOS preview client
399 needs: [request, preview]
400 if: needs.preview.outputs.ios_build == 'true'
401 runs-on: blacksmith-12vcpu-macos-27
402 timeout-minutes: 60
403 environment:
404 name: mobile-preview-fork
405 permissions:
406 contents: read
407 pull-requests: read
408 outputs:
409 client: ${{ steps.build.outputs.link }}
410 env:
411 APP_VARIANT: preview
412 NODE_OPTIONS: --max-old-space-size=8192
413 MOBILE_VERSION_POLICY: fingerprint
414 FINGERPRINT: ${{ needs.preview.outputs.ios_fingerprint }}
415 steps:
416 - name: Confirm the PR is still open
417 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
418 with:
419 script: |
420 const { data: pull } = await github.rest.pulls.get({
421 owner: context.repo.owner,
422 repo: context.repo.repo,
423 pull_number: context.payload.pull_request.number,
424 });
425 if (pull.state !== "open") core.setFailed(`PR #${pull.number} is no longer open; not building.`);
426β
427 - name: Checkout reviewed commit
428 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
429 with:
430 ref: ${{ needs.request.outputs.sha }}
431 fetch-depth: 0
432β
433 # Scripts and actions come from main, not from the PR.
434 - name: Checkout trusted scripts
435 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
436 with:
437 path: .trusted
438 sparse-checkout: |
439 .github/scripts
440 .github/actions
441β
442 # The steps below run dependency and build scripts; they get no token.
443 - name: Drop checkout credentials
444 uses: ./.trusted/.github/actions/drop-checkout-credentials
445β
446 # Cold: no sticky disk or EAS cache that trusted builds would restore.
447 - name: Build
448 uses: ./.trusted/.github/actions/eas-local-build
449 with:
450 platform: ios
451 profile: preview:dev
452 environment: preview
453 expo-token: ${{ secrets.EXPO_PREVIEW_TOKEN }}
454 output: ${{ runner.temp }}/t3code-preview.ipa
455 cache: "false"
456β
457 - id: build
458 name: Upload
459 working-directory: apps/mobile
460 shell: bash
461 env:
462 EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
463 run: |
464 eas upload --platform ios --build-path "$RUNNER_TEMP/t3code-preview.ipa" --fingerprint "$FINGERPRINT" --non-interactive --json | tee "$RUNNER_TEMP/upload.json"
465 link="$(sed -n '/^{/,$p' "$RUNNER_TEMP/upload.json" | jq -r '.url // empty')"
466 if [ -z "$link" ]; then
467 echo "::error::eas upload returned no install link"
468 exit 1
469 fi
470 echo "link=$link" >> "$GITHUB_OUTPUT"
471β
472 comment:
473 name: Mobile preview comment
474 needs: [request, preview, android-build, ios-build]
475 if: always() && !cancelled() && needs.preview.result == 'success'
476 runs-on: blacksmith-2vcpu-ubuntu-2404
477 concurrency:
478 group: mobile-preview-comment-${{ github.event.pull_request.number }}
479 cancel-in-progress: false
480 permissions:
481 contents: read
482 pull-requests: write
483 steps:
484 - name: Checkout comment script
485 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
486 with:
487 sparse-checkout: .github/scripts
488β
489 - name: Comment
490 env:
491 GH_TOKEN: ${{ github.token }}
492 PR: ${{ github.event.pull_request.number }}
493 PR_HEAD_SHA: ${{ needs.request.outputs.sha }}
494 ANDROID_FINGERPRINT: ${{ needs.preview.outputs.fingerprint }}
495 ANDROID_GROUP: ${{ needs.preview.outputs.group }}
496 ANDROID_CLIENT: ${{ needs.android-build.outputs.client || needs.preview.outputs.client }}
497 IOS_FINGERPRINT: ${{ needs.preview.outputs.ios_fingerprint }}
498 IOS_GROUP: ${{ needs.preview.outputs.ios_group }}
499 IOS_CLIENT: ${{ needs.ios-build.outputs.client || needs.preview.outputs.ios_client }}
500 run: node .github/scripts/mobile-preview-comment.cjs