This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

使用 EAS 工作流程为拉取请求创建预览构建

学习如何使用 EAS 工作流程为 Slack 通知和 PR 预览创建预览构建工作流。


与团队成员和拉取请求(PR)审查者分享正在进行的移动端修改时,通常需要他们检出一个分支并在本地运行开发服务器。

🌐 Sharing in-progress mobile changes with teammates and pull request (PR) reviewers often requires them to check out a branch and run a development server locally.

学习成果

🌐 Learning outcomes

  • 用指纹自动化预览构建以便内部分发
  • 构建完成后发送 Slack 通知
  • 在每个拉取请求上发布一个 OTA 更新,并在 PR 上评论附上测试链接

先决条件

3 要求

1.

已安装 expo-updates

安装 expo-updates 库:

🌐 Install the expo-updates library:

Terminal
- npx expo install expo-updates
- yarn dlx expo install expo-updates
- pnpm dlx expo install expo-updates
- bunx expo install expo-updates

2.

已配置 EAS 更新

要配置 EAS 更新,请运行:

🌐 To configure EAS Update, run:

Terminal
- eas update:configure

上述命令会将更新 URL 和运行时版本添加到 app.json,并在 eas.json 中为每个构建配置添加一个 channel 字段。

🌐 The above command adds the update URL and runtime version to app.json, and adds a channel field to each build profile in eas.json.

3.

预览版已创建

要收到 PR 预览更新,审阅者需要在他们的设备上安装带有 expo-updates 和 EAS 更新通道的预览版本。要创建一个:

🌐 To receive PR preview updates, reviewers need a preview build with expo-updates and the EAS Update channel installed on their device. To create one:

Terminal
- eas build --profile preview --platform all

由于 expo-updates 是本地库,这次构建会同时嵌入它的本地代码和 eas.json 中的通道。运行上面的命令也让 EAS CLI 在我们第一次触发预览构建时生成凭证。

🌐 Since expo-updates is a native library, this build embeds both its native code and the channel from eas.json. Running the above command also allows EAS CLI to generate credentials the first time we trigger a preview build.

build 预览版构建的工作类型

🌐 The build job type for preview builds

团队使用预览版本进行利益相关者测试。成员通过内部分发将版本安装到他们的设备/模拟器/模拟器上。他们不需要运行开发服务器或检出分支就能测试更改。

🌐 Teams use preview builds for stakeholder testing. Members install the build through internal distribution onto their device/emulator/simulator. They do not have to run a development server or check out the branch to test changes.

就像开发构建一样,我们添加指纹以避免在本地代码未更改时进行不必要的重建。

🌐 Like development builds, we add fingerprints to avoid unnecessary rebuilds when native code doesn't change.

1

添加一个 preview.yml

🌐 Add a preview.yml

在 .eas/workflows/ 里,我们来添加一个叫 preview.yml 的新文件。这个工作流程文件使用了 preview 配置文件。

🌐 Inside .eas/workflows/, let's add a new file called preview.yml. This workflow file uses the preview profile.

.eas/workflows/preview.yml
name: Preview builds jobs: fingerprint: name: Fingerprint type: fingerprint environment: preview get_android_build: name: Check for existing Android build needs: [fingerprint] type: get-build params: fingerprint_hash: ${{ needs.fingerprint.outputs.android_fingerprint_hash }} profile: preview get_ios_build: name: Check for existing iOS build needs: [fingerprint] type: get-build params: fingerprint_hash: ${{ needs.fingerprint.outputs.ios_fingerprint_hash }} profile: preview build_android: name: Build Android needs: [get_android_build] if: ${{ !needs.get_android_build.outputs.build_id }} type: build params: platform: android profile: preview build_ios: name: Build iOS needs: [get_ios_build] if: ${{ !needs.get_ios_build.outputs.build_id }} type: build params: platform: ios profile: preview

2

运行工作流

🌐 Run the workflow

使用以下命令手动运行工作流:

🌐 Run the workflow manually using the following command:

Terminal
- eas workflow:run .eas/workflows/preview.yml

在 EAS 仪表板中,你会注意到工作流程遵循我们为开发构建实现的相同指纹模式。由于这是我们第一次创建预览构建,它会运行 Android 和 iOS 的构建任务。如果我们向 main 分支推送另一次提交而不更改原生代码,工作流程会跳过构建任务,因为已有兼容的构建存在。

🌐 In the EAS dashboard, notice that the workflow follows the same fingerprint pattern we implemented for development builds. Since we are creating the preview build for the first time, it runs the build jobs for Android and iOS. If we push another commit to the main branch without changing native code, the workflow skips the build jobs since compatible builds already exist.

Slack 通知

🌐 Slack notifications

很多团队使用 Slack 来接收构建通知,这样团队成员就无需查看 EAS 仪表板也能看到构建状态。当构建完成(或跳过)时,slack 预打包的任务会将状态发布到一个通道。

🌐 Many teams use Slack for build notifications so their team sees build status without checking the EAS dashboard. When a build completes (or skips), the slack pre-packaged job posts the status to a channel.

1

创建一个 Slack webhook 链接

🌐 Create a Slack webhook URL

要从 EAS 工作流程向我们的 Slack 通道发送消息,我们需要从 Slack 设置中获取一个 webhook URL:

🌐 To send messages from EAS Workflows to our Slack channel, we need a webhook URL from Slack's settings:

  1. 去 api.slack.com/apps 创建一个新的 Slack 应用(或者用现有的)。
  2. 在 功能 下,选择 传入 Webhook 并将其打开。
  3. 点击 添加新 Webhook 并选择要发布通知的通道。
  4. 复制 webhook URL。它看起来像:https://hooks.slack.com/services/TD000/B000/XXXXXXXXXX
  5. 将 webhook URL 添加为名为 SLACK_WEBHOOK_URL 的 EAS 环境变量。在我们项目的 EAS 仪表板的环境变量页面上,为 preview 环境创建该变量,并将其可见性设置为私密,因为只有 EAS 服务器需要读取它。或者,也可以通过 EAS CLI 创建:
Terminal
- eas env:set --name SLACK_WEBHOOK_URL --value https://hooks.slack.com/services/TD000/B000/XXXXXXXXXX --environment preview --visibility secret

2

添加一个 Slack 通知任务

🌐 Add a Slack notification job

让我们更新 preview.yml,在构建任务之后添加一个 notify 任务。notify 任务使用 webhook_url 和 message 参数,或者使用 payload 参数来通过 Slack Block Kit 进行丰富的格式化。该任务通过它的 environment 字段从 SLACK_WEBHOOK_URL 环境变量,我们之前创建的 中读取 webhook URL。任务默认在 production 环境中运行,所以在这里设置 environment: preview 就可以让变量生效。

🌐 Let's update preview.yml to add a notify job after the build jobs. The notify job uses webhook_url and message parameters, or a payload parameter for rich formatting with Slack Block Kit. The job reads the webhook URL from the SLACK_WEBHOOK_URL environment variable we created earlier through its environment field. Jobs default to the production environment, so setting environment: preview here is what makes the variable available.

.eas/workflows/preview.yml
name: Preview builds jobs: fingerprint: # ... get_android_build: # ... get_ios_build: # ... build_android: # ... build_ios: # ... notify: name: Notify on Slack after: [build_android, build_ios] type: slack environment: preview params: webhook_url: ${{ env.SLACK_WEBHOOK_URL }} message: 'Preview builds are ready - Android: ${{ after.build_android.status }}, iOS: ${{ after.build_ios.status }}'

after 字段确保通知在构建完成后触发,无论结果如何。message 使用 ${{ after.build_android.status }} 和 ${{ after.build_ios.status }} 来包含每个平台的结果(成功、失败或跳过)。团队可以在不离开 Slack 的情况下看到发生了什么。

🌐 The after field ensures the notification fires once the builds finish, regardless of the outcome. The message uses ${{ after.build_android.status }} and ${{ after.build_ios.status }} to include each platform's outcome (success, failure, or skipped). The team sees what happened without leaving Slack.

3

运行工作流

🌐 Run the workflow

使用以下命令手动运行工作流:

🌐 Run the workflow manually using the following command:

Terminal
- eas workflow:run .eas/workflows/preview.yml

在 EAS 仪表板上,我们可以确认 Notify on Slack 任务运行成功。

🌐 On the EAS dashboard, we can verify that the Notify on Slack job runs successfully.

我们也可以在与应用集成的 Slack 通道里验证这个。

🌐 We can also verify this in the Slack channel integrated with the app.

使用 EAS 更新的 PR 预览

🌐 PR previews using EAS Update

PR 审查者可以阅读差异,但无法直观地看到更改,也不能在设备上测试它们。

🌐 A PR reviewer can read the diff, but cannot see the changes visually or test them on a device.

网页团队通过部署预览来解决这个问题。当有人提交拉取请求时,CI/CD 工作流会生成一个预览链接。审查者打开链接并测试更改,而不需要检出分支。

🌐 Web teams solve this with deploy previews. When someone opens a pull request, a CI/CD workflow generates a preview link. Reviewers open the link and test the changes without checking out the branch.

对于移动端,我们可以使用 EAS Update 来设置等效功能。它会向已经安装了兼容版本的设备推送 JavaScript 打包文件,从而跳过本地编译步骤。对于 PR 预览,更新是最合适的选择,因为审阅者可以快速打开和测试,然后在 PR 上留下反馈。

🌐 For mobile, we can set up the equivalent using EAS Update. It delivers a JavaScript bundle to devices that already have a compatible build installed, skipping the native compilation step. For PR previews, an update is the right fit because reviewers can open and test quickly, then leave feedback on the PR.

EAS Workflows 提供了一个 github-comment 预打包任务来处理这个。在更新发布后,它会在拉取请求上发表评论,给审查者一个链接或二维码,让他们在自己的设备上打开更新。

🌐 EAS Workflows provides a github-comment pre-packaged job for this. It posts a comment on the pull request after the update publishes, giving reviewers a link or QR code to open the update on their device.

1

创建一个 pr-preview.yml

🌐 Create a pr-preview.yml

在 .eas/workflows/ 里面,我们来添加一个新文件,叫做 pr-preview.yml。这个工作流会在创建拉取请求时使用 on.pull_request 触发器运行。它只会使用 update 作业类型发布一个更新,然后在 PR 上发布一条带有更新链接的评论。

🌐 Inside .eas/workflows/, let's add a new file called pr-preview.yml. This workflow runs when a pull request is created using the on.pull_request trigger. It only publishes an update using the update job type and then posts a comment on the PR with the link to the update.

.eas/workflows/pr-preview.yml
name: PR Preview on: pull_request: branches: ['*'] jobs: publish_preview: name: Publish PR preview update type: update environment: preview params: channel: preview comment: needs: [publish_preview] type: github-comment

2

用拉取请求测试

🌐 Test with a pull request

让我们通过在我们的 GitHub 仓库中创建一个拉取请求来测试工作流程。拉取请求需要一个与 main 不同的分支。运行以下命令来创建并切换到一个新分支:

🌐 Let's test the workflow by creating a pull request in our GitHub repository. A pull request requires a separate branch from main. Run the following command to create and switch to a new branch:

Terminal
- git checkout -b test-preview

现在,让我们在 Expo 项目中做一个可见的更改(例如,修改文本字符串或背景颜色)。然后,提交并推送分支:

🌐 Now, let's make a visible change in the Expo project (for example, change a text string or background color). Then, commit and push the branch:

Terminal
- git add .
- git commit -m "Test PR preview"
- git push origin test-preview

在 GitHub 上从 test-preview 打开一个到 main 的拉取请求。创建拉取请求后,工作流会自动运行。在 EAS 仪表板上,我们可以看到 publish_preview 任务先运行,comment 任务在它成功后运行。

🌐 Open a pull request from test-preview to main on GitHub. The workflow runs automatically after we have created a pull request. On the EAS dashboard, we can verify that the publish_preview job runs first and the comment job runs after it succeeds.

在 GitHub 的 pull request 上,应该会出现一条评论,里面有一个链接或二维码,可以用来在设备上测试更新。

🌐 On the GitHub pull request, a comment should appear with a link or QR code to test the update on a device.

通过评论中的链接或二维码,我们现在可以在设备或模拟器/模拟器上打开更新并测试更改。

🌐 Using the link or QR code in the comment, we can now open the update on our device or emulator/simulator and test the changes.

概括

🌐 Summary

Chapter 3: Preview builds

We created a preview build workflow with fingerprinting, added Slack notifications for build status, and set up PR previews using EAS Update so reviewers can test changes without checking out a branch.

In the next chapter, learn how to run end-to-end tests with Maestro in an EAS Workflows job.

下一个:Chapter 4: E2E tests