首页指南参考教程

使用 GitHub 操作

了解如何使用 GitHub Actions 通过 EAS 更新自动发布更新。


GitHub Action 是一种云函数,每次 GitHub 上发生事件时都会运行。你可以配置 GitHub Actions,以便在你或你的团队成员合并到分支(例如 "production")时自动构建和发布更新。这使得部署过程一致且快速,让你有更多时间来开发应用。

¥A GitHub Action is a cloud function that runs every time an event on GitHub occurs. You can configure GitHub Actions to automate building and publishing updates when you or members of your team merge to a branch, like "production". This makes the process of deploying consistent and fast, leaving you more time to develop your app.

本指南将引导你了解如何设置 GitHub Actions 以发布推送更新和拉取请求预览。

¥This guide will walk you through how to set up GitHub Actions to publish updates on push and previews on pull requests.

通过推送发布更新

¥Publish updates on push

最常见的用例之一是在推送代码时发布更新。以下是每次推送更新时发布更新的步骤:

¥One of the most common use cases is to publish an update when code is pushed. Below are the steps to publish an update every time an update is pushed:

1

在项目的根目录下创建一个名为 .github/workflows/update.yml 的文件路径。

¥Create a file path named .github/workflows/update.yml at the root of your project.

2

在 update.yml 中,复制并粘贴以下代码片段:

¥Inside update.yml, copy and paste the following snippet:

update.yml
name: update
on: push

jobs:
  update:
    name: EAS Update
    runs-on: ubuntu-latest
    steps:
      - name: Check for EXPO_TOKEN
        run: |
          if [ -z "${{ secrets.EXPO_TOKEN }}" ]; then
            echo "You must provide an EXPO_TOKEN secret linked to this project's Expo account in this repo's secrets. Learn more: https://expo.nodejs.cn/eas-update/github-actions"
            exit 1
          fi

      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 18.x
          cache: yarn

      - name: Setup EAS
        uses: expo/expo-github-action@v8
        with:
          eas-version: latest
          token: ${{ secrets.EXPO_TOKEN }}

      - name: Install dependencies
        run: yarn install

      - name: Publish update
        run: eas update --auto

在上面的脚本中:

¥In the above script:

  • 你正在设置一个操作,以便每次将代码推送到任何分支时运行。

    ¥You are setting an action to run every time the code is pushed to any branch.

  • update 作业用于设置 Node.js 版本和 Expo 的 GitHub Action (expo-github-action)。cache 选项用于缓存上次运行时安装的所有依赖,以加快此脚本在后续运行中的速度。

    ¥The update job is used to set up a Node.js version and Expo's GitHub Action (expo-github-action). The cache option is used to cache any dependencies installed from the last run to speed this script up on subsequent runs.

  • yarn install 用于安装依赖。

    ¥yarn install is used to install dependencies.

  • 最后一步用 eas update --auto 发布更新。由于它使用 --auto 标志,因此 EAS 分支将以 GitHub 分支命名。更新的消息将与提交的消息匹配。

    ¥The last step publishes the update with eas update --auto. Since it uses the --auto flag, the EAS branch will be named after the GitHub branch. The message for the update will match the commit's message.

3

你需要通过提供 EXPO_TOKEN 环境变量来授予上述脚本运行权限。

¥You need to give the script above permission to run by providing an EXPO_TOKEN environment variable.

  • 导航至 https://expo.dev/settings/access-tokens

    ¥Navigate to https://expo.dev/settings/access-tokens.

  • 单击“创建令牌”以创建新的个人访问令牌。

    ¥Click Create token to create a new personal access token.

  • 复制生成的令牌。

    ¥Copy the token generated.

  • 将 "your-username" 和 "your-repo-name" 替换为你的项目信息,导航到 https://github.com/your-username/your-repo-name/settings/secrets/actions

    ¥Navigate to https://github.com/your-username/your-repo-name/settings/secrets/actions by replacing "your-username" and "your-repo-name" with your project's info.

  • 在存储库密钥下,单击新建存储库密钥。

    ¥Under Repository secrets, click New repository secret.

  • 创建一个名为 EXPO_TOKEN 的密钥,并将复制的访问令牌粘贴为值。

    ¥Create a secret with the name EXPO_TOKEN, and paste the copied access token in as the value.

你的 GitHub Action 现在应该已设置完毕。每当开发者将代码合并到存储库中时,此操作都会构建更新并发布它,使其可用于具有有权访问 EAS 分支的构建的所有设备。

¥Your GitHub Action should be set up now. Whenever a developer merges code into the repo, this action will build an update and publish it, making it available to all of your devices with builds that have access to the EAS branch.

某些存储库或组织可能需要显式启用 GitHub 工作流并允许第三方操作。

¥Some repositories or organizations might need to explicitly enable GitHub Workflows and allow third-party Actions.

发布拉取请求预览

¥Publish previews on pull requests

另一个常见的用例是为每个拉取请求创建新的更新。这允许你在合并代码之前在设备上测试拉取请求中的更改,而无需在本地启动项目。以下是每次打开拉取请求时发布更新的步骤:

¥Another common use case is to create a new update for every pull request. This allows you to test the changes in the pull request on a device before merging the code, and without having to start the project locally. Below are the steps to publish an update every time a pull request is opened:

1

在项目的根目录下创建一个名为 .github/workflows/preview.yml 的文件路径。

¥Create a file path named .github/workflows/preview.yml at the root of your project.

2

在 Preview.yml 中,复制并粘贴以下代码片段:

¥Inside preview.yml, copy and paste the following snippet:

preview.yml
name: preview
on: pull_request

jobs:
  update:
    name: EAS Update
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      - name: Check for EXPO_TOKEN
        run: |
          if [ -z "${{ secrets.EXPO_TOKEN }}" ]; then
            echo "You must provide an EXPO_TOKEN secret linked to this project's Expo account in this repo's secrets. Learn more: https://expo.nodejs.cn/eas-update/github-actions"
            exit 1
          fi

      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 18.x
          cache: yarn

      - name: Setup EAS
        uses: expo/expo-github-action@v8
        with:
          eas-version: latest
          token: ${{ secrets.EXPO_TOKEN }}

      - name: Install dependencies
        run: yarn install

      - name: Create preview
        uses: expo/expo-github-action/preview@v8
        with:
          command: eas update --auto

在上面的脚本中:

¥In the above script:

  • 你正在使用工作流事件 on 在每次打开或更新拉取请求时运行。

    ¥You are using the workflow event on to run every time a pull request is opened or updated.

  • update 作业中,Node.js 版本、Expo 的 GitHub Action 和依赖是使用 GitHub Action 的内置缓存设置的。

    ¥In the update job, the Node.js version, Expo's GitHub Action and the dependencies are set up using GitHub Action's built-in cache.

  • eas update --auto预览子操作 运行。它向拉取请求添加一条注释,其中包含有关更新的基本信息以及用于扫描更新的二维码。

    ¥The eas update --auto is run by the preview subaction. It adds a comment to the pull request with basic information about the update and a QR code to scan the update.

不要忘记将 permissions 部分添加到作业中。这使得作业能够向拉取请求添加评论。

¥Don't forget to add the permissions section to the job. This enables the job to add comments to the pull request.

3

如果你已经在上一节中设置了 EXPO_TOKEN,则可以跳过此步骤。只需一个有效的 EXPO_TOKEN 即可使用你的 Expo 账户对 GitHub Actions 进行身份验证。

¥You can skip this step if you have already set up EXPO_TOKEN in the previous section. Only one valid EXPO_TOKEN is required to authenticate GitHub Actions with your Expo account.

如果没有,你需要通过提供 EXPO_TOKEN 环境变量来授予上述脚本运行权限。

¥If you haven't, you need to give the script above permission to run by providing an EXPO_TOKEN environment variable.

  • 导航至 https://expo.dev/settings/access-tokens

    ¥Navigate to https://expo.dev/settings/access-tokens.

  • 单击“创建令牌”以创建新的个人访问令牌。

    ¥Click Create token to create a new personal access token.

  • 复制生成的令牌。

    ¥Copy the token generated.

  • 将 "your-username" 和 "your-repo-name" 替换为你的项目信息,导航到 https://github.com/your-username/your-repo-name/settings/secrets/actions

    ¥Navigate to https://github.com/your-username/your-repo-name/settings/secrets/actions by replacing "your-username" and "your-repo-name" with your project's info.

  • 在存储库密钥下,单击新建存储库密钥。

    ¥Under Repository secrets, click New repository secret.

  • 创建一个名为 EXPO_TOKEN 的密钥,并将复制的访问令牌粘贴为值。

    ¥Create a secret with the name EXPO_TOKEN, and paste the copied access token in as the value.

你的 GitHub Action 现在应该已设置完毕。每当开发者创建拉取请求时,此操作都会构建更新并发布它,使其可供所有有权访问 EAS 分支的构建的审阅者使用。

¥Your GitHub Action should be set up now. Whenever a developer creates a pull request, this action will build an update and publish it, making it available to all reviewers with builds that have access to the EAS branch.

某些存储库或组织可能需要显式启用 GitHub 工作流并允许第三方操作。

¥Some repositories or organizations might need to explicitly enable GitHub Workflows and allow third-party Actions.

使用 Bun 代替 Yarn

¥Using Bun instead of Yarn

要使用 Bun 而不是 Yarn 作为包管理器,请按照以下步骤发布推送更新和拉取请求预览:

¥To use Bun as the package manager instead of Yarn, follow the steps below for both publishing updates on push and previews on pull requests:

1

将 update.yml 或 Preview.yml 中的 Setup Node 步骤替换为以下代码片段:

¥Replace the Setup Node step in update.yml or preview.yml with the following snippet:

update.yml/preview.yml
- name: Setup Bun
  uses: oven-sh/setup-bun@v1
  with:
    bun-version: latest

2

要使用 Bun 安装依赖,请将安装依赖步骤替换为以下代码片段:

¥To install dependencies using Bun, replace the Install dependencies step with the following snippet:

update.yml/preview.yml
- name: Install dependencies
  run: bun install
Expo 中文网 - 粤ICP备13048890号