This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

CI/CD 教程:入门

EAS 工作流教程简介,以及为 Expo 和 React Native 应用设置 CI/CD 流水线的核心概念。


构建、测试和交付 Expo 和 React Native 应用通常不是一步到位的过程。每次代码更改都要经历类似的流程:为 Android 和 iOS 构建,运行单元测试和端到端(E2E)测试,然后交给队友、 QA 或应用商店处理。每次提交都这样做很快就会变得乏味,而这正是 CI/CD 流水线应该为我们处理的工作。

🌐 Building, testing, and delivering an Expo and React Native app is rarely a one-step process. Each code change goes through a similar flow of building for Android and iOS, running unit and end-to-end (E2E) tests, and handing it off to teammates, QA, or the app stores. Doing this on every commit gets tedious fast, and it's exactly the kind of work a CI/CD pipeline should handle for us.

在本教程结束时,我们对 Expo 项目的每一次 push 都会自动构建、测试或发布。实现这一点的服务是 EAS Workflows,它是来自 Expo 应用服务(EAS)的持续集成(CI)/持续交付(CD)服务,专为 Expo 和 React Native 应用设计。

🌐 By the end of this tutorial, every push to our Expo project builds, tests, or ships automatically. The service that makes this possible is EAS Workflows, a Continuous Integration (CI)/Continuous Delivery (CD) service from Expo Application Services (EAS) for Expo and React Native apps.

我们在 Expo 项目根目录下 .eas/workflows/ 中的 YAML 文件里定义一个工作流。这里有一个完整的示例,它会在 GitHub 仓库的 main 分支每次推送时构建一个 Android 开发版本:

🌐 We define a workflow in a YAML file that lives in .eas/workflows/ at the root of our Expo project. Here is a complete example that builds an Android development build on every push to the main branch in a GitHub repository:

.eas/workflows/build.yml
name: Build Android on: push: branches: ['main'] jobs: build: type: build params: platform: android profile: development

这几行代码就是整个工作流程,剩下的都由 EAS 来处理。我们在整个教程中都会沿用这个模式。

🌐 That handful of lines is the entire workflow, and EAS handles the rest. We build on this pattern throughout the tutorial.

为什么要使用 EAS 工作流?

🌐 Why use EAS Workflows?

EAS Workflows 可以运行 Android、iOS 和网页构建,发布 OTA 更新,提交到应用商店,并用 Maestro 运行 E2E 测试,这些都像上面的例子一样在 YAML 文件中定义。因为作业在托管的云环境中运行,所以我们不需要设置或维护构建服务器。

🌐 EAS Workflows runs Android, iOS, and web builds, publishes OTA updates, submits to app stores, and runs E2E tests with Maestro, all defined in YAML files like the example above. Because jobs run in a managed cloud environment, there is no build server for us to set up or maintain.

我们可以通过 GitHub 事件(push、pull request、标签或 label)、定时(cron)或者使用 EAS CLI 手动触发工作流。

🌐 We can trigger workflows from GitHub events (push, pull request, tags, or labels), on a schedule (cron), or manually with EAS CLI.

涵盖的话题

🌐 Topics covered

这个教程分为三部分:

🌐 The tutorial has three parts:

  • 开发。 创建自定义任务,使用指纹识别自动化开发构建,并为相关人员推送拉取请求(PR)预览更新。
  • 测试和发布。 在工作流中用 Maestro 运行 E2E 测试,然后创建一个生产工作流,使用指纹识别在原生构建和 OTA 更新之间进行选择。
  • 扩展。 将生产触发器从分支切换到版本标签,并使用 EAS Hosting 部署网页构建。

值得先了解的概念

🌐 Concepts worth knowing first

在我们创建第一个工作流之前,有几个概念值得了解:

🌐 Before we create our first workflow, here are a few concepts worth knowing:

Expo 应用会被发布到哪里?

一个 Expo 应用可以发布到三个目标。具体是哪个目标,以及哪个 EAS 服务交付它,取决于代码中有哪些变化:

🌐 An Expo app can ship to three targets. Which one, and which EAS service delivers it, depends on what changed in the code:

目标EAS 服务什么时候使用
应用商店EAS 构建 和 EAS 提交新版本发布,原生代码更改
已安装设备EAS 更新仅限 TypeScript/JavaScript 修复,可通过 OTA 秒级推送
WebEAS 托管与原生版本一起发布的 Web 应用
建立档案

EAS Build 是一个用于为我们的 Expo 项目构建应用二进制文件的服务。默认情况下,它支持三种构建配置:development、preview 和 production。每种配置对应开发的不同阶段。在本教程中,我们会使用这三种配置。

版本与更新

像 EAS Build 一样,EAS Update 是一个为使用 expo-updates 库的 Expo 项目提供 OTA 更新的服务。

🌐 Like EAS Build, EAS Update is a service that serves OTA updates for Expo projects using the expo-updates library.

  • EAS Build 会把原生代码编译成完整的应用二进制文件。构建需要更长的时间。当我们项目里的原生代码发生变化时,就需要重新构建。比如,添加新的原生库、修改权限或者升级 Expo SDK。
  • EAS 更新可以将 TypeScript/JavaScript 的改动通过无线方式推送到已经安装了兼容本地构建的设备上。如果改动需要修改本地代码,我们会先创建并安装那个构建,然后再发布更新。这样,每次更新都能保持与目标构建的兼容。发布一次更新本身只需要几秒钟。比如,我们可以发布更新来修复一个 UI 错误或者修改按钮的文字。

根据不同的变更,我们会使用其中一种服务。EAS 工作流把它们连接起来,这样流水线就可以决定什么时候构建,什么时候发布更新。

🌐 Depending on the change, we use one service or the other. EAS Workflows wires them together so the pipeline can decide when to build and when to publish an update.

先决条件

🌐 Prerequisites

本教程是一个动手体验。为了取得进展,我们需要一个已经存在的使用 Continuous Native Generation 的 Expo 项目。在我们的机器上和 EAS 上按以下要求进行设置:

🌐 This tutorial is a hands-on experience. To make progress, we need an existing Expo project that uses Continuous Native Generation. Set it up on our machine and EAS with the following requirements:

先决条件

6 要求

1.

一个Expo账户

我们需要注册一个Expo账户。

🌐 We need to sign up for an Expo account.

2.

安装 EAS CLI 并登录

要安装 EAS CLI,请运行以下命令:

🌐 To install EAS CLI, run the following command:

Terminal
- npm install --global eas-cli
- yarn global add eas-cli
- pnpm add -g eas-cli
- bun add -g eas-cli

然后,运行以下登录命令,用 Expo 账户认证 EAS CLI:

🌐 Then, run the following login command to authenticate EAS CLI with the Expo account:

Terminal
- eas login

3.

创建 Expo 项目

要创建一个新的 Expo 项目,运行以下命令:

🌐 To create a new Expo project, run the following command:

Terminal
- npx create-expo-app@latest
- yarn create expo-app
- pnpm create expo-app
- bun create expo

使用 create-expo-app 创建新项目会生成一个没有提交本地 android 和 ios 目录的项目。这种模式叫做连续本地生成(Continuous Native Generation,CNG),EAS Build 会自动为我们创建这些本地项目目录。

🌐 Creating a new project with create-expo-app sets up a project without native android and ios directories committed. This pattern is called Continuous Native Generation (CNG) and EAS Build creates those native project directories for us automatically.

4.

在项目中初始化 EAS

要在我们的项目中初始化 EAS,运行以下命令:

🌐 To initialize EAS in our project, run the following command:

Terminal
- eas build:configure

这会在我们 Expo 项目的根目录下生成一个 eas.json 文件,并创建一个与我们本地项目关联的 EAS 项目。

🌐 This generates an eas.json file in the root of our Expo project and creates an EAS project linked to our local project.

5.

与 EAS 仪表板关联的 GitHub 仓库

我们需要为这个项目准备一个 GitHub 仓库。我们可以创建一个新的仓库或使用已有的仓库。然后,将 GitHub 仓库连接到 EAS:

🌐 We need a GitHub repository for the project. We can create a new repository or use an existing one. Then, connect the GitHub repository to EAS:

  1. 在 EAS 仪表板上导航到项目的 GitHub 设置。
  2. 按照仪表板上的界面操作来安装 Expo GitHub 应用。
  3. 选择与 Expo 项目匹配的 GitHub 存储库并连接它。

6.

存储 EAS 提交的凭证(可选)

只有在我们在第5章之后将自动化应用商店提交作为适配时才需要。查看 Google Play 商店 和 Apple App Store 指南了解如何为 EAS Submit 配置凭证。

🌐 Only needed if we add automated app store submission as an adaptation after Chapter 5. See the Google Play Store and Apple App Store guides for instructions on how to configure credentials for EAS Submit.

来自 GitHub Actions 吗?

🌐 Coming from GitHub Actions?

EAS 工作流文件存放在 Expo 项目的根目录下的 .eas/workflows/ 文件夹里,就像 GitHub Actions 的工作流文件存放在 .github/workflows/ 一样。触发语法(on.push、on.pull_request 等)看起来也差不多。

🌐 EAS Workflows files live in the .eas/workflows/ directory at the root of an Expo project, similar to how GitHub Actions workflows live in .github/workflows/. The trigger syntax (on.push, on.pull_request, and so on) also looks much the same.

主要的区别在于预打包的作业:设置 type: build 让 EAS 处理环境和构建产物,不需要编写 runs-on 或设置步骤。我们会在学习过程中讲到这些。

🌐 The main difference is pre-packaged jobs: setting type: build lets EAS handle the environment and build artifacts, with no runs-on or setup step to write. We cover these as we go.

下一步

🌐 Next step

项目设置好之后,我们可以进入第一章,写我们的第一个工作流程了。

🌐 With our project set up, we can move to the first chapter and write our first workflow.

开始

使用 EAS 工作流创建并运行我们的第一个工作流。