t3-code-android-nightly/.github/workflows/mobile-eas-preview-fork.yml
Julius Marminge ceb20555b3
ci(mobile): fork previews wait for an approval that names the commit (#42503)
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-24 16:25:01 -08:00

500 lines
21 KiB
YAML

name: Mobile EAS Preview (Fork)
run-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) || '' }}"
# EAS previews for fork PRs. This runs the PR's code with an Expo token: the
# app config and plugins run on a runner; both clients build on Blacksmith
# with preview signing credentials. So every job that does waits for an
# approval in the `mobile-preview-fork` environment on every run.
#
# The label only requests a build, and counts only from someone with write
# access. The `request` job consumes it, pins the PR's head commit from the
# label event, and names that commit in the run title, the job summary and a
# PR comment. It reads no secrets and checks out nothing. The approval is the
# sign-off that the named commit was reviewed: the jobs after it build that
# commit and no other. A later push deploys nothing until the label is applied
# again and that run is approved.
#
# Same-repo PRs deploy on every push while labeled, via mobile-eas-preview.yml.
on:
pull_request_target:
types: [labeled]
permissions:
contents: read
jobs:
request:
name: Preview request
if: >-
github.event.pull_request.head.repo.full_name != github.repository &&
github.event.label.name == '🚀 Mobile Continuous Deployment'
runs-on: blacksmith-2vcpu-ubuntu-2404
permissions:
contents: read
pull-requests: write
outputs:
sha: ${{ steps.pin.outputs.sha }}
steps:
# The label counts only from someone with write access, who could push a
# same-repo branch and get this build anyway. If a maintainer removed the
# label first, that removal wins.
- id: consume
name: Consume the deployment label
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
name: context.payload.label.name,
});
} catch (error) {
if (error.status !== 404) throw error;
core.info("The label was removed before this run could consume it. Skipping.");
core.setOutput("consumed", "false");
return;
}
const sender = context.payload.sender.login;
const { data: access } = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: sender,
});
const authorized = ["admin", "maintain", "write"].includes(access.permission);
if (!authorized) {
core.warning(`${sender} has ${access.permission} access; a fork preview needs write access. Label removed, nothing built.`);
}
core.setOutput("consumed", String(authorized));
# The review screen of a pull_request_target run shows the base branch,
# so the commit is named here, where the reviewer reads it before
# approving. The jobs below take it from this job's output. Nothing is
# requested unless the environment exists with required reviewers: a
# workflow that names a missing environment creates it without any.
- id: pin
name: Name the commit to build
if: steps.consume.outputs.consumed == 'true'
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
ENVIRONMENT: mobile-preview-fork
with:
script: |
const name = process.env.ENVIRONMENT;
let rules = [];
try {
const { data } = await github.request("GET /repos/{owner}/{repo}/environments/{environment_name}", {
owner: context.repo.owner,
repo: context.repo.repo,
environment_name: name,
});
rules = data.protection_rules ?? [];
} catch (error) {
if (error.status !== 404) throw error;
}
if (!rules.some((rule) => rule.type === "required_reviewers" && rule.reviewers?.length > 0)) {
core.setFailed(`The ${name} environment has no required reviewers. Nothing built.`);
return;
}
const pull = context.payload.pull_request;
const sha = pull.head.sha;
if (!/^[0-9a-f]{40}$/.test(sha)) {
core.setFailed("The label event carries no head commit. Nothing built.");
return;
}
const repository = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}`;
const body = [
`Mobile preview requested by ${context.payload.sender.login} for commit [\`${sha}\`](${repository}/pull/${pull.number}/commits/${sha}).`,
"",
`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.`,
].join("\n");
await core.summary.addRaw(body, true).write();
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pull.number,
body,
});
core.setOutput("sha", sha);
preview:
name: EAS Preview
needs: request
if: needs.request.outputs.sha != ''
runs-on: blacksmith-8vcpu-ubuntu-2404
environment:
name: mobile-preview-fork
concurrency:
group: mobile-eas-preview-fork-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: read
pull-requests: read
outputs:
ios_group: ${{ steps.ios_update.outputs.group }}
ios_client: ${{ steps.ios_client.outputs.link }}
group: ${{ steps.update.outputs.group }}
client: ${{ steps.client.outputs.link }}
ios_build: ${{ steps.ios_client.outputs.build }}
ios_fingerprint: ${{ steps.ios_client.outputs.fingerprint }}
build: ${{ steps.client.outputs.build }}
fingerprint: ${{ steps.client.outputs.fingerprint }}
env:
APP_VARIANT: preview
NODE_OPTIONS: --max-old-space-size=8192
MOBILE_VERSION_POLICY: fingerprint
steps:
- name: Confirm the PR is still open
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const { data: pull } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});
if (pull.state !== "open") core.setFailed(`PR #${pull.number} is no longer open; not building.`);
- id: expo-token
name: Check for EXPO_PREVIEW_TOKEN
env:
EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
run: |
if [ -n "$EXPO_TOKEN" ]; then
echo "present=true" >> "$GITHUB_OUTPUT"
else
echo "present=false" >> "$GITHUB_OUTPUT"
echo "EXPO_PREVIEW_TOKEN is not set; skipping EAS preview."
fi
# The commit the request job named, not the branch, so a push after the
# label builds nothing. No credentials stay in .git for the PR's scripts
# to read.
- name: Checkout reviewed commit
if: steps.expo-token.outputs.present == 'true'
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
ref: ${{ needs.request.outputs.sha }}
fetch-depth: 0
# No sparse-checkout: see mobile-eas-preview.yml.
# Scripts and actions come from main, not from the PR.
- name: Checkout trusted scripts
if: steps.expo-token.outputs.present == 'true'
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
path: .trusted
sparse-checkout: |
.github/scripts
.github/actions
# The steps below run dependency and build scripts; they get no token.
- name: Drop checkout credentials
if: steps.expo-token.outputs.present == 'true'
uses: ./.trusted/.github/actions/drop-checkout-credentials
# No caches: pull_request_target saves them under main's scope, where
# trusted workflows with other secrets would restore what PR code wrote.
- name: Setup Vite+
if: steps.expo-token.outputs.present == 'true'
uses: voidzero-dev/setup-vp@250f29ce396baf5e8f24498e17c0dfdebabc26eb # v1.15.0
with:
node-version-file: package.json
cache: false
run-install: |
args:
- --filter=@t3tools/mobile...
- name: Expose pnpm
if: steps.expo-token.outputs.present == 'true'
run: |
pnpm_version="$(node --print "require('./package.json').packageManager.split('@').pop()")"
vp_pnpm_bin="$HOME/.vite-plus/package_manager/pnpm/$pnpm_version/pnpm/bin"
echo "$vp_pnpm_bin" >> "$GITHUB_PATH"
"$vp_pnpm_bin/pnpm" --version
- name: Setup EAS
if: steps.expo-token.outputs.present == 'true'
uses: expo/expo-github-action@c7b66a9c327a43a8fa7c0158e7f30d6040d2481e # v8.2.1
with:
eas-version: latest
eas-cache: false
token: ${{ secrets.EXPO_PREVIEW_TOKEN }}
packager: npm
- name: Pull preview environment variables
if: steps.expo-token.outputs.present == 'true'
working-directory: apps/mobile
env:
EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
run: |
eas env:pull preview --non-interactive
# See mobile-eas-preview.yml: keeps Android build and OTA fingerprints matching.
node --input-type=module <<'NODE'
import { appendFileSync, readFileSync } from "node:fs";
import { parseEnv } from "node:util";
const env = parseEnv(readFileSync(".env.local", "utf8"));
const googleServicesFile = env.T3CODE_ANDROID_GOOGLE_SERVICES_FILE;
if (googleServicesFile) {
appendFileSync(process.env.GITHUB_ENV, `T3CODE_ANDROID_GOOGLE_SERVICES_FILE=${googleServicesFile}\n`);
}
NODE
- id: ios_client
name: Find the iOS client for this fingerprint
if: steps.expo-token.outputs.present == 'true'
working-directory: apps/mobile
shell: bash
env:
EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
run: |
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)')"
echo "fingerprint=$hash" >> "$GITHUB_OUTPUT"
tag="mobile-ios-preview-build/$hash"
link=""
if git fetch --quiet origin "refs/tags/$tag:refs/tags/$tag" 2>/dev/null; then
link="$(git tag -l --format='%(contents)' "$tag" | node "$GITHUB_WORKSPACE/.trusted/.github/scripts/mobile-ios-preview-record.cjs" read)"
fi
if [ -z "$link" ]; then
link="$(eas build:list --platform ios --build-profile preview:dev --runtime-version "$hash" --status finished --limit 5 --json --non-interactive \
| sed -n '/^\[/,$p' \
| 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)"')"
fi
echo "link=$link" >> "$GITHUB_OUTPUT"
if [ -z "$link" ]; then echo "build=true" >> "$GITHUB_OUTPUT"; fi
- id: ios_update
name: Publish the iOS update
if: steps.expo-token.outputs.present == 'true'
working-directory: apps/mobile
shell: bash
env:
EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
PR: ${{ github.event.pull_request.number }}
run: |
group="$(eas update --auto --branch "pr-$PR" --platform ios --environment preview --non-interactive --json | sed -n '/^\[/,$p' | jq -er '.[0].group')"
echo "group=$group" >> "$GITHUB_OUTPUT"
# Android, as in mobile-eas-preview.yml: the same fingerprint command the Expo
# action runs. A client built for a same-repo PR (its tag) or by EAS is reused.
- id: client
name: Find the Android client for this fingerprint
if: steps.expo-token.outputs.present == 'true'
working-directory: apps/mobile
shell: bash
env:
EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
run: |
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)')"
echo "fingerprint=$hash" >> "$GITHUB_OUTPUT"
tag="mobile-android-preview-build/$hash"
link=""
if git fetch --quiet origin "refs/tags/$tag:refs/tags/$tag" 2>/dev/null; then
link="$(git tag -l --format='%(contents:subject)' "$tag")"
else
link="$(eas build:list --platform android --build-profile preview:dev --runtime-version "$hash" --status finished --limit 5 --json --non-interactive \
| sed -n '/^\[/,$p' \
| 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)"')"
fi
echo "link=$link" >> "$GITHUB_OUTPUT"
if [ -z "$link" ]; then echo "build=true" >> "$GITHUB_OUTPUT"; fi
- id: update
name: Publish the Android update
if: steps.expo-token.outputs.present == 'true'
working-directory: apps/mobile
shell: bash
env:
EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
PR: ${{ github.event.pull_request.number }}
run: |
group="$(eas update --auto --branch "pr-$PR" --platform android --environment preview --non-interactive --json | sed -n '/^\[/,$p' | jq -er '.[0].group')"
echo "group=$group" >> "$GITHUB_OUTPUT"
# Each client job waits for its own approval in the environment, once the
# preview job has finished. The reviewed commit's Android client, built cold: no sticky disk, because PR
# code could leave Gradle or ccache state that trusted builds would restore, and
# no build record tag, so nobody else is served an APK a fork's code built.
android-build:
name: Android preview client
needs: [request, preview]
if: needs.preview.outputs.build == 'true'
runs-on: blacksmith-32vcpu-ubuntu-2404
timeout-minutes: 60
environment:
name: mobile-preview-fork
permissions:
contents: read
pull-requests: read
outputs:
client: ${{ steps.build.outputs.link }}
env:
APP_VARIANT: preview
NODE_OPTIONS: --max-old-space-size=8192
MOBILE_VERSION_POLICY: fingerprint
FINGERPRINT: ${{ needs.preview.outputs.fingerprint }}
# Phones only: emulators use the development client from the native-client cache.
ORG_GRADLE_PROJECT_reactNativeArchitectures: armeabi-v7a,arm64-v8a
steps:
- name: Confirm the PR is still open
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const { data: pull } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});
if (pull.state !== "open") core.setFailed(`PR #${pull.number} is no longer open; not building.`);
- name: Checkout reviewed commit
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
ref: ${{ needs.request.outputs.sha }}
fetch-depth: 0
# Scripts and actions come from main, not from the PR.
- name: Checkout trusted scripts
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
path: .trusted
sparse-checkout: |
.github/scripts
.github/actions
# The steps below run dependency and build scripts; they get no token.
- name: Drop checkout credentials
uses: ./.trusted/.github/actions/drop-checkout-credentials
# Cold: no sticky disk or EAS cache that trusted builds would restore.
- name: Build
uses: ./.trusted/.github/actions/eas-local-build
with:
platform: android
profile: preview:dev
environment: preview
expo-token: ${{ secrets.EXPO_PREVIEW_TOKEN }}
output: ${{ runner.temp }}/t3code-preview.apk
cache: "false"
- id: build
name: Upload
working-directory: apps/mobile
shell: bash
env:
EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
run: |
eas upload --platform android --build-path "$RUNNER_TEMP/t3code-preview.apk" --fingerprint "$FINGERPRINT" --non-interactive --json | tee "$RUNNER_TEMP/upload.json"
link="$(sed -n '/^{/,$p' "$RUNNER_TEMP/upload.json" | jq -r '.url // empty')"
if [ -z "$link" ]; then
echo "::error::eas upload returned no install link"
exit 1
fi
echo "link=$link" >> "$GITHUB_OUTPUT"
ios-build:
name: iOS preview client
needs: [request, preview]
if: needs.preview.outputs.ios_build == 'true'
runs-on: blacksmith-12vcpu-macos-27
timeout-minutes: 60
environment:
name: mobile-preview-fork
permissions:
contents: read
pull-requests: read
outputs:
client: ${{ steps.build.outputs.link }}
env:
APP_VARIANT: preview
NODE_OPTIONS: --max-old-space-size=8192
MOBILE_VERSION_POLICY: fingerprint
FINGERPRINT: ${{ needs.preview.outputs.ios_fingerprint }}
steps:
- name: Confirm the PR is still open
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const { data: pull } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});
if (pull.state !== "open") core.setFailed(`PR #${pull.number} is no longer open; not building.`);
- name: Checkout reviewed commit
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
ref: ${{ needs.request.outputs.sha }}
fetch-depth: 0
# Scripts and actions come from main, not from the PR.
- name: Checkout trusted scripts
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
path: .trusted
sparse-checkout: |
.github/scripts
.github/actions
# The steps below run dependency and build scripts; they get no token.
- name: Drop checkout credentials
uses: ./.trusted/.github/actions/drop-checkout-credentials
# Cold: no sticky disk or EAS cache that trusted builds would restore.
- name: Build
uses: ./.trusted/.github/actions/eas-local-build
with:
platform: ios
profile: preview:dev
environment: preview
expo-token: ${{ secrets.EXPO_PREVIEW_TOKEN }}
output: ${{ runner.temp }}/t3code-preview.ipa
cache: "false"
- id: build
name: Upload
working-directory: apps/mobile
shell: bash
env:
EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
run: |
eas upload --platform ios --build-path "$RUNNER_TEMP/t3code-preview.ipa" --fingerprint "$FINGERPRINT" --non-interactive --json | tee "$RUNNER_TEMP/upload.json"
link="$(sed -n '/^{/,$p' "$RUNNER_TEMP/upload.json" | jq -r '.url // empty')"
if [ -z "$link" ]; then
echo "::error::eas upload returned no install link"
exit 1
fi
echo "link=$link" >> "$GITHUB_OUTPUT"
comment:
name: Mobile preview comment
needs: [request, preview, android-build, ios-build]
if: always() && !cancelled() && needs.preview.result == 'success'
runs-on: blacksmith-2vcpu-ubuntu-2404
concurrency:
group: mobile-preview-comment-${{ github.event.pull_request.number }}
cancel-in-progress: false
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout comment script
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
sparse-checkout: .github/scripts
- name: Comment
env:
GH_TOKEN: ${{ github.token }}
PR: ${{ github.event.pull_request.number }}
PR_HEAD_SHA: ${{ needs.request.outputs.sha }}
ANDROID_FINGERPRINT: ${{ needs.preview.outputs.fingerprint }}
ANDROID_GROUP: ${{ needs.preview.outputs.group }}
ANDROID_CLIENT: ${{ needs.android-build.outputs.client || needs.preview.outputs.client }}
IOS_FINGERPRINT: ${{ needs.preview.outputs.ios_fingerprint }}
IOS_GROUP: ${{ needs.preview.outputs.ios_group }}
IOS_CLIENT: ${{ needs.ios-build.outputs.client || needs.preview.outputs.ios_client }}
run: node .github/scripts/mobile-preview-comment.cjs