用于 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@v4
- name: Setup Node
uses: actions/setup-node@v4
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:
- 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