> ## Documentation Index
> Fetch the complete documentation index at: https://e2e.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Expo Workflows

> 使用 Expo Build 构建 Expo 应用，并在 Expo 工作流中对其运行 iOS 和 Android 测试。

export const setupPrompt = `添加一个 Expo 工作流，构建这个 Expo 应用并在 iOS 和 Android 上运行它的 e2e 测试。文档：\`https://e2e.com/docs/ci/eas-workflows.md\` 和 \`https://e2e.com/docs/ci.md\`

1. 阅读这两个页面。
2. 向 \`eas.json\` 添加 \`e2e\` 构建配置。
3. 添加 \`.eas/workflows/e2e.yml\`，并根据这个项目的测试目标和 Node 版本进行调整。
4. 提醒我将模型密钥添加为机密的 Expo 环境变量，并在每个测试作业上设置匹配的 \`environment:\`。
5. 我启动工作流后，读取作业日志并修复错误，直到两个作业都通过。`;

在 Expo 工作流中，`build` 作业会生成用于模拟器的 `.app` 或 `.apk`。随后，自定义作业下载构建产物、启动设备并运行测试套件。这些作业遵循[移动端作业](https://e2e.com/docs/zh/ci#mobile-jobs)中的规则。

<Note>
本页介绍 CI 服务 Expo Workflows。要在 Expo 托管的 iOS 模拟器和 Android 模拟器上运行测试，请参阅 [Expo Simulators](https://e2e.com/docs/zh/integrations/eas)。
</Note>

<SetupPrompt>

<Prompt description={setupPrompt} children={setupPrompt} actions={["copy", "cursor"]} />

</SetupPrompt>

## 构建配置

添加一个无需凭据的构建配置：

```json title="eas.json"
{
  "build": {
    "e2e": {
      "withoutCredentials": true,
      "ios": { "simulator": true },
      "android": { "buildType": "apk" }
    }
  }
}
```

## 工作流

Android 作业需要使用 `nested-virtualization` Linux 运行器，这是唯一能运行模拟器的 Expo 机器。使用如下所示的自定义作业。内置的 `maestro` 作业类型运行的是 Maestro 流程，而非 e2e。

```yaml title=".eas/workflows/e2e.yml" expandable
name: e2e

on:
  pull_request:
    branches: ['*']
  push:
    branches: [main]

defaults:
  tools:
    node: '24'

jobs:
  build_ios:
    type: build
    params:
      platform: ios
      profile: e2e

  build_android:
    type: build
    params:
      platform: android
      profile: e2e

  e2e_ios:
    needs: [build_ios]
    runs_on: macos-medium
    environment: preview
    env:
      CI: '1'
    steps:
      - uses: eas/checkout
      - uses: eas/install_node_modules
      - uses: eas/download_build
        id: download_build
        with:
          build_id: ${{ needs.build_ios.outputs.build_id }}
          extensions: [app]
      - name: Boot a simulator and install the app
        run: |
          UDID=$(xcrun simctl create e2e "iPhone 17" com.apple.CoreSimulator.SimRuntime.iOS-26-5)
          xcrun simctl bootstatus "$UDID" -b
          xcrun simctl install "$UDID" "${{ steps.download_build.outputs.artifact_path }}"
      - name: Run e2e
        run: npx e2e run --target ios --reporter list,junit
      - uses: eas/upload_artifact
        if: ${{ always() }}
        with:
          type: other
          name: e2e-ios
          path: .e2e

  e2e_android:
    needs: [build_android]
    runs_on: linux-medium-nested-virtualization
    environment: preview
    env:
      CI: '1'
    steps:
      - uses: eas/checkout
      - uses: eas/install_node_modules
      - uses: eas/download_build
        id: download_build
        with:
          build_id: ${{ needs.build_android.outputs.build_id }}
          extensions: [apk]
      - uses: eas/start_android_emulator
        with:
          system_image_package: system-images;android-35;google_apis;x86_64
      - name: Install the app
        run: adb install -r "${{ steps.download_build.outputs.artifact_path }}"
      - name: Run e2e
        run: npx e2e run --target android --reporter list,junit
      - uses: eas/upload_artifact
        if: ${{ always() }}
        with:
          type: other
          name: e2e-android
          path: .e2e
```

`CI: '1'` 会在未设置 `CI` 的运行器上启用 [CI 默认值](https://e2e.com/docs/zh/ci#what-ci-changes)。`eas/start_android_emulator` 是 Expo Build 函数，文档位于[自定义构建](https://docs.expo.dev/custom-builds/schema/)中，而非 Workflows 语法页面。它使用 `sdkmanager` 下载你指定的系统镜像。

## 模型密钥

将模型密钥（例如 `AI_GATEWAY_API_KEY`）保存在可见性为 secret 的 [Expo 环境变量](https://docs.expo.dev/eas/environment-variables/)中。在每个作业上设置 `environment:`，因为自定义作业默认读取 `production`。

每个读取该环境的作业都能读取密钥，包括运行不可信拉取请求（PR）的作业。参阅[模型凭据](https://e2e.com/docs/zh/ci#model-credentials)。

## 减少构建次数

结合 `get-build` 和 `repack` 的 `fingerprint` 作业会复用上一次原生构建，只打包 JavaScript。[agent-device Expo Workflows 模板](https://github.com/callstackincubator/eas-agent-device/blob/main/.eas/workflows/agent-qa-mobile.yml)在两个平台上都采用了这种方式。

## 下一步

<CardGroup cols={2}>
  <Card title="持续集成" icon="rotate" href="/ci">
    CI 默认值、分片、重新运行和退出码。
  </Card>
  <Card title="Expo Simulators" icon="/images/integrations/expo.svg" href="/integrations/eas">
    在 Expo 托管设备上运行测试套件，无需 Mac 或 Android SDK。
  </Card>
</CardGroup>
