GitHub Action 用于 PR 预览

编辑页面

了解如何使用 GitHub Actions 自动发布带有 EAS Update 的更新。


For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.

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

本指南将带你了解如何设置 GitHub Actions,以便在 pull request 上发布预览。

在 pull request 上发布预览

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

1

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

2

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

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 "你必须在此仓库的 secrets 中提供一个与该项目的 Expo 账号关联的 EXPO_TOKEN secret。了解更多:https://docs.expo.dev/eas-update/github-actions" exit 1 fi - name: Checkout repository uses: actions/checkout@v5 - name: Setup Node uses: actions/setup-node@v6 with: node-version: 22 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

在上面的脚本中:

  • 你使用了工作流事件 on,以便在每次打开或更新 pull request 时运行。
  • update 作业中,Node.js 版本、Expo 的 GitHub Action 以及依赖项都使用了 GitHub Action 内置缓存进行设置。
  • eas update --autopreview 子 action 执行。它会在 pull request 中添加一条评论,包含有关该更新的基本信息以及用于扫描该更新的二维码。

不要忘记在该作业中添加 permissions 部分。这将使作业能够向 pull request 添加评论。

3

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

如果还没有设置,你需要通过提供一个 EXPO_TOKEN 环境变量来授予上面的脚本运行权限。

现在你的 GitHub Action 应该已经设置好了。每当开发者创建一个 pull request 时,这个 action 都会构建一个更新并发布它,使所有能够访问 EAS 分支的构建审阅者都可以使用它。

某些仓库或组织可能需要显式启用 GitHub Workflows,并允许第三方 Actions。

使用 Bun 替代 Yarn

如果想使用 Bun 作为包管理器而不是 Yarn,请按照以下步骤分别在 push 时发布更新和在 pull request 上发布预览:

1

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

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

2

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

update.yml/preview.yml
- name: Install dependencies run: bun install