从 CI 触发构建

了解如何从 CI 环境(例如 GitHub Action 等)触发应用的 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 构建初始化步骤,并从本地终端为你想要在 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 上初始化项目。

    ¥Initialize the project on EAS by generating a projectId.

  • 添加定义构建配置文件的 eas.json 文件。

    ¥Add an eas.json file defining your build profiles.

  • 填充原生版本的关键应用配置属性,例如 android.packageNameios.bundleIdentifier

    ¥Populates critical app config properties for native builds, such as android.packageName and ios.bundleIdentifier.

  • 确保创建构建凭据,包括 Android 密钥库和 iOS 分发证书和配置文件。

    ¥Ensure build credentials are created, including Android keystores and iOS distribution certs and provisioning profiles.

运行 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 工作流程 是 Expo 提供的一项服务,允许你在 EAS 上运行构建和许多其他类型的作业。你可以使用 EAS 工作流来自动化你的开发和发布流程,例如创建开发版本或自动构建并提交到应用商店。

¥EAS Workflows is a service from Expo that allows you to run builds, and many other types of jobs, on EAS. You can use EAS Workflows to automate your development and release processes, like creating development builds or automatically building and submitting to the app stores.

要使用 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

提供个人访问令牌以通过 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 密钥。接下来,你需要收集有关你的 苹果团队 的信息。

¥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_API_KEY_PATH: The path to your ASC API Key .p8 file. For example, /path/to/key/AuthKey_SFB993FB5F.p8.

  • EXPO_ASC_KEY_ID:你的 ASC API 密钥的密钥 ID。例如,SFB993FB5F

    ¥EXPO_ASC_KEY_ID: The key ID of your ASC API Key. For example, SFB993FB5F.

  • EXPO_ASC_ISSUER_ID:你的 ASC API 密钥的颁发者 ID。例如,f9675cff-f45d-4116-bd2c-2372142cee09

    ¥EXPO_ASC_ISSUER_ID: The issuer ID of your ASC API Key. For example, f9675cff-f45d-4116-bd2c-2372142cee09.

  • EXPO_APPLE_TEAM_ID:你的 Apple 团队 ID。例如,77KQ969CHE

    ¥EXPO_APPLE_TEAM_ID: Your Apple Team ID. For example, 77KQ969CHE.

  • EXPO_APPLE_TEAM_TYPE:你的 Apple 团队类型。有效类型为 IN_HOUSECOMPANY_OR_ORGANIZATIONINDIVIDUAL

    ¥EXPO_APPLE_TEAM_TYPE: Your Apple Team Type. Valid types are IN_HOUSE, COMPANY_OR_ORGANIZATION, or INDIVIDUAL.

触发新构建

¥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 执行时间的费用。但是,仅当触发 EAS 构建成功时,你的 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 CI

在项目存储库根目录的 .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 Pipelines

在项目存储库根目录的 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 Actions

在项目存储库根目录的 .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@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 18.x
          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