用于 PR 预览的 GitHub Action
了解如何使用 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 previews on pull requests.
发布拉取请求预览
🌐 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:
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@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
在上述脚本中:
🌐 In the above script:
- 你正在使用工作流事件
on,以便每次打开或更新拉取请求时运行。 - 在
update任务中,Node.js 版本、Expo 的 GitHub Action 以及依赖都是通过 GitHub Action 内置缓存进行设置的。 eas update --auto由 preview subaction 运行。它会在拉取请求中添加一条评论,包含关于更新的基本信息以及用于扫描更新的二维码。
别忘了在任务中添加
permissions部分。这可以让任务向拉取请求添加评论。
3
如果你在上一节中已经设置了 EXPO_TOKEN,可以跳过此步骤。只需要一个有效的 EXPO_TOKEN 就可以验证 GitHub Actions 与你的 Expo 账户的连接。
🌐 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。
- 点击 创建令牌 来创建一个新的个人访问令牌。
- 复制生成的令牌。
- 通过替换“your-username”和“your-repo-name”为你的项目信息,导航到 https://github.com/your-username/your-repo-name/settings/secrets/actions。
- 在 仓库密钥 下,点击 新建仓库密钥。
- 创建一个名为 EXPO_TOKEN 的密钥,并将复制的访问令牌粘贴为其值。
你的 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 工作流并允许第三方操作。
使用 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:
- 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:
- name: Install dependencies run: bun install