sailfork / t3code-github / .github/ workflows/ mobile-eas-preview.yml

name: Mobile EAS Preview

on:
  pull_request:
    types: [opened, reopened, synchronize, labeled]

jobs:
  preview:
    name: EAS Preview
    # Fork PRs deploy one reviewed commit per label through mobile-eas-preview-fork.yml.
    if: |
      github.event.pull_request.head.repo.full_name == github.repository &&
      contains(github.event.pull_request.labels.*.name, '🚀 Mobile Continuous Deployment') &&
      (github.event.action != 'labeled' || github.event.label.name == '🚀 Mobile Continuous Deployment')
    runs-on: blacksmith-8vcpu-ubuntu-2404
    concurrency:
      group: mobile-eas-preview-${{ github.event.pull_request.number }}
      cancel-in-progress: true
    permissions:
      contents: read
      pull-requests: write
    env:
      APP_VARIANT: preview
      NODE_OPTIONS: --max-old-space-size=8192
      MOBILE_VERSION_POLICY: fingerprint
    steps:
      - 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 available; skipping EAS preview."
          fi

      - name: Checkout
        if: steps.expo-token.outputs.present == 'true'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          # No sparse-checkout here: it makes actions/checkout fetch with
          # --filter=blob:none, and eas-cli archives the project via
          # `git clone --depth 1 file://<workspace>`, which fails (exit 128)
          # when the partial clone can't serve the unfetched blobs.

      - name: Setup Vite+
        if: steps.expo-token.outputs.present == 'true'
        uses: voidzero-dev/setup-vp@v1
        with:
          node-version-file: package.json
          cache: true
          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@v8
        with:
          # eas-cli 22 renamed `project` to `app` in build JSON, and
          # continuous-deploy-fingerprint still reads `build.project.ownerAccount`.
          # Unpin once https://github.com/expo/expo-github-action/issues/358 is fixed.
          eas-version: 21.8.0
          token: ${{ secrets.EXPO_PREVIEW_TOKEN }}
          # npm, not pnpm: this only installs eas-cli into the action's own
          # tool dir, and pnpm 11 hard-fails that install on dtrace-provider's
          # ignored build script (no allowBuilds config outside the repo).
          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
          # EAS Update disables dotenv loading. Keep the downloaded native config
          # in its process environment so Android build and OTA fingerprints match.
          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

      - name: Deploy with fingerprint check
        if: steps.expo-token.outputs.present == 'true'
        uses: expo/expo-github-action/continuous-deploy-fingerprint@main
        env:
          EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
          # The Expo action otherwise suppresses the EAS command's failure output.
          RUNNER_DEBUG: "1"
        with:
          profile: preview:dev
          branch: pr-${{ github.event.pull_request.number }}
          platform: all
          environment: preview
          working-directory: apps/mobile
          github-token: ${{ secrets.GITHUB_TOKEN }}