从 CI 触发构建

了解如何从 CI 环境(例如 GitHub Actions 等)触发应用在 EAS 上的构建。


本文档概述了如何从 CI 环境(例如 GitHub Actions、Travis CI 等)触发应用的 EAS 构建。

🌐 This document outlines how to trigger builds on EAS for your app from a CI environment such as GitHub Actions, Travis CI, and more.

先决条件

🌐 Prerequisites

从本地计算机运行成功的构建

🌐 Run a successful build from your local machine

要从 CI 环境触发 EAS 构建,你的应用需要设置为在非交互模式下使用 EAS Build。为此,请完成 EAS Build 的初始化步骤,并在本地终端为你希望在 CI 上支持的每个平台运行一次成功的构建。这样,eas build 命令就可以提示任何额外的配置需求,然后该配置将可用于未来在 CI 上的非交互式运行。

🌐 To trigger EAS builds from a CI environment, your app needs to be set up to use EAS Build in non-interactive mode. To do this, go through the EAS Build initialization steps and run a successful build from your local terminal for each platform you would like to support on CI. This way, the eas build command can prompt for any additional configuration it needs, and then that configuration will be available for future non-interactive runs on CI.

在本地运行构建将完成以下关键配置步骤:

🌐 Running a build locally will accomplish the following critical configuration steps:

  • 通过生成 projectId 在 EAS 上初始化项目。
  • 添加一个 eas.json 文件来定义你的构建配置。
  • 为原生构建填充关键的应用配置属性,例如 android.packageNameios.bundleIdentifier
  • 确保创建构建凭据,包括 Android 密钥库和 iOS 分发证书和配置文件。

运行 eas build -p [all|android|ios] 并验证每个平台的构建是否成功完成。然后,继续执行以下步骤,在 CI 上实现 EAS 构建。

🌐 Run eas build -p [all|android|ios] and verify that your builds for each platform complete successfully. Then, continue with the below steps for implementing EAS Build on CI.

如果你还没有做过此操作,请查看 创建你的第一个构建 指南,准备好后再回来这里。

🌐 If you haven't done this yet, see the Create your first build guide and return here when you're ready.

使用 EAS 工作流

🌐 Using EAS Workflows

EAS Workflows 是 Expo 提供的一个 CI/CD 服务,允许你在 EAS 上运行构建以及许多其他类型的任务。你可以使用 EAS Workflows 来自动化你的开发和发布流程,例如创建开发构建或自动构建并提交到应用商店。

要使用 EAS 工作流创建构建,请首先在 .eas/workflows/build.yml 中添加以下代码:

🌐 To create a build with EAS Workflows, start by adding the following code in .eas/workflows/build.yml:

name: Build on: push: branches: - main jobs: build_android: name: Build Android App type: build params: platform: android build_ios: name: Build iOS App type: build params: platform: ios

当提交推送到主分支时,此工作流将创建 Android 和 iOS 构建。你可以在 EAS 工作流文档 中了解如何修改此工作流并安排其他类型的任务。

🌐 When a commit is pushed to the main branch, this workflow will create Android and iOS builds. You can learn how to modify this workflow and sequence other types of jobs in the EAS Workflows documentation.

为其他 CI 配置应用服务

🌐 Configuring your app for other CI services

/* 待办事项:我们可能可以把这部分省略——用户可以自己决定是否要这样做或使用 npx */

/* 要与 EAS API 交互,我们需要安装 EAS CLI。你可以使用已预装此库的环境,或者将其作为开发依赖添加到项目中。 */

/* 后者是最简单的方法,但可能会增加安装时间。 */

/* 对于按分钟收费的供应商,创建一个预先构建的环境可能是值得的。 */

提供个人访问令牌以通过 CI 上的 Expo 账户进行身份验证

🌐 Provide a personal access token to authenticate with your Expo account on CI

接下来,我们需要确保能够以应用所有者的身份在 CI 上进行身份验证。这可以通过在 CI 设置中将个人访问令牌存储在 EXPO_TOKEN 环境变量中来实现。

🌐 Next, we need to ensure that we can authenticate ourselves on CI as the owner of the app. This is possible by storing a personal access token in the EXPO_TOKEN environment variable in the CI settings.

请参阅 个人访问令牌 了解如何创建访问令牌。

🌐 See personal access tokens to learn how to create access tokens.

(可选)为你的 Apple 团队提供 ASC API 令牌

🌐 (Optional) Provide an ASC API Token for your Apple Team

如果你的 iOS 凭证需要修复,我们将需要一个 ASC API 密钥,以便在 CI 中向 Apple 进行身份验证。一个常见的情况是你的配置描述文件需要重新签名。

🌐 In the event your iOS credentials need to be repaired, we will need an ASC API key to authenticate ourselves to Apple in CI. A common case is when your provisioning profile needs to be re-signed.

你需要创建一个 API 密钥。接下来,你需要收集有关你的 Apple 团队 的信息。

🌐 You will need to create an API Key. Next, you will need to gather information about your Apple Team.

使用你收集的信息,通过环境变量将其传入构建命令。你需要传入以下内容:

🌐 Using the information you've gathered, pass it into the build command through environment variables. You will need to pass the following:

  • EXPO_ASC_API_KEY_PATH:你的 ASC API 密钥 .p8 文件的路径。例如,/path/to/key/AuthKey_SFB993FB5F.p8
  • EXPO_ASC_KEY_ID:你的 ASC API 密钥的关键 ID。例如,SFB993FB5F
  • EXPO_ASC_ISSUER_ID:你的 ASC API 密钥的发行者 ID。例如,f9675cff-f45d-4116-bd2c-2372142cee09
  • EXPO_APPLE_TEAM_ID:你的 Apple 团队 ID。例如,77KQ969CHE
  • EXPO_APPLE_TEAM_TYPE:你的苹果团队类型。有效类型为 IN_HOUSECOMPANY_OR_ORGANIZATIONINDIVIDUAL

触发新构建

🌐 Trigger new builds

现在我们已经通过 Expo CLI 进行了身份验证,我们可以创建构建步骤。

🌐 Now that we're authenticated with Expo CLI, we can create the build step.

为了触发新的构建,我们将此脚本添加到我们的配置中:

🌐 To trigger new builds, we will add this script to our configuration:

Terminal
npx eas-cli build --platform all --non-interactive --no-wait

这将触发 EAS 上的新构建。系统会打印一个 URL,链接到 EAS 仪表板中构建的进度。

🌐 This will trigger a new build on EAS. A URL will be printed, linking to the build's progress in the EAS dashboard.

信息 --no-wait 标志在构建触发后会退出该步骤。当 EAS 执行构建时,你不会被计入 CI 执行时间费用。然而,你的 CI 仅在成功触发 EAS Build 时才会报告构建作业通过。

如果你需要在构建完成后添加另一个 CI 步骤,请移除此标志。

Travis CI

在项目仓库根目录的 .travis.yml 文件中添加以下代码片段。

🌐 Add the following code snippet in .travis.yml at the root of your project repository.

travis.yml
language: node_js node_js: - node - lts/* cache: directories: - ~/.npm before_script: - npm install -g npm@latest jobs: include: - stage: build node_js: lts/* script: - npm ci - npx eas-cli build --platform all --non-interactive --no-wait
GitLab 持续集成

在项目仓库根目录下的 .gitlab-ci.yml 文件中添加以下代码片段。

🌐 Add the following code snippet in .gitlab-ci.yml at the root of your project repository.

.gitlab-ci.yml
image: node:alpine cache: key: ${CI_COMMIT_REF_SLUG} paths: - .npm # or with Yarn: #- .yarn stages: - build before_script: - npm ci --cache .npm # or with Yarn: #- yarn install --cache-folder .yarn eas-build: stage: build script: - apk add --no-cache bash - npx eas-cli build --platform all --non-interactive --no-wait
Bitbucket 管道

在项目仓库根目录下的 bitbucket-pipelines.yml 文件中添加以下代码片段。

🌐 Add the following code snippet in bitbucket-pipelines.yml at the root of your project repository.

bitbucket-pipelines.yml
image: node:alpine definitions: caches: npm: ~/.npm pipelines: default: - step: name: Build app deployment: test caches: - npm script: - apk add --no-cache bash - npm ci - npx eas-cli build --platform all --non-interactive --no-wait
CircleCI

在项目仓库根目录的 circleci/config.yml 中添加以下代码片段。

🌐 Add the following code snippet in circleci/config.yml at the root of your project repository.

.circleci/config.yml
version: 2.1 executors: default: docker: - image: cimg/node:lts working_directory: ~/my-app jobs: eas_build: executor: default steps: - checkout - run: name: Install dependencies command: npm ci - run: name: Trigger build command: npx eas-cli build --platform all --non-interactive --no-wait workflows: build_app: jobs: - eas_build: filters: branches: only: master
GitHub 操作

在项目仓库根目录的 .github/workflows/eas-build.yml 中添加以下代码片段。

🌐 Add the following code snippet in .github/workflows/eas-build.yml at the root of your project repository.

.github/workflows/eas-build.yml
name: EAS Build on: workflow_dispatch: push: branches: - main jobs: build: name: Install and build runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 - uses: actions/setup-node@v6 with: node-version: 22 cache: npm - name: Setup Expo and EAS uses: expo/expo-github-action@v8 with: eas-version: latest token: ${{ secrets.EXPO_TOKEN }} - name: Install dependencies run: npm ci - name: Build on EAS run: eas build --platform all --non-interactive --no-wait