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 工作流程从发布分支为 Android 和 iOS 自动化生成生产构建和空中更新。
每次生产环境有改动都重建应用很浪费,尤其大多数改动只是 TypeScript/JavaScript 的时候。
🌐 Rebuilding the app for every production change is wasteful when most changes are TypeScript/JavaScript only.
学习成果
🌐 Learning outcomes
- 从
release/*分支触发生产部署,而不是每次推送到main - 根据项目指纹,在原生构建和 OTA 更新 之间切换分支
- 在生产工作流中把应用商店提交自动化当作后续步骤
1 要求
1 要求
EAS CLI 会在我们第一次为某个配置触发构建时提示生成凭据。请手动触发 Android 和 iOS 的生产构建,以便在工作流运行前拥有凭据:
使用发布分支
🌐 Using a release branch
以前,在开发版本中,我们看到使用 on.push.branches 触发器会在每次推送到指定的 main 分支时运行我们的工作流。对于生产部署来说,在每次推送到 main 分支时触发工作流意味着每次合并的变更都是一次新发布。团队通常希望有一个明确的发布流程,所以他们会使用发布分支或标签来代替。
🌐 Previously, with development builds, we have seen that using the on.push.branches trigger runs our workflow on every push to the specified main branch. For production deployments, triggering a workflow on every push to the main branch means every change merged is a new release. Teams usually want a deliberate release process, so they use a release branch or tag instead.
在发布分支策略中,我们通常用类似 release/* 的模式来指定分支,其中 * 是功能或版本名称。这就是区分持续集成(CI)和持续交付(CD)的地方。CI 会在每次推送到 main 分支时运行,而 CD 则会在团队准备好部署到生产环境时在发布分支上运行。
🌐 In a release branch strategy, we usually specify the branch with a pattern such as release/*, where * is the feature or version name. This is what separates Continuous Integration (CI) from Continuous Delivery (CD). CI runs on every push to the main branch, while CD runs on a release branch when the team is ready to deploy to production.
下表显示了每个工作流的触发器如何定义其用途:
🌐 The table below shows how each workflow's trigger defines its purpose:
上述文件可以在一个项目的 GitHub 仓库中共存而不会发生冲突。每个文件都使用不同的构建配置和触发器。
🌐 The above files coexist inside a project's GitHub repository without any conflict. Each uses a different build profile and a trigger.
用于生产构建的 build 工作类型
🌐 The build job type for production builds
让我们先创建一个生产工作流程。它使用 EAS 工作流程中的 预打包 build 作业类型,并同时运行于 Android 和 iOS。
🌐 Let's start by creating a production workflow. It uses the pre-packaged build job type from EAS Workflows and runs for both Android and iOS.
1
添加一个 production.yml
🌐 Add a production.yml
在 .eas/workflows/ 里面,创建一个名为 production.yml 的新文件,使用 eas.json 里的 production 构建配置:
🌐 Inside .eas/workflows/, create a new file called production.yml that uses the production build profile from eas.json:
这会在推送到任何符合 release/* 模式的分支时,为 Android 创建一个 .aab 文件,为 iOS 创建一个 .ipa 文件。
🌐 This creates a .aab artifact for Android and an .ipa artifact for iOS on a push to any branch that matches the pattern release/*.
2
在工作流程中添加指纹
🌐 Add fingerprints to the workflow
让我们在工作流中添加一个 fingerprint 任务,以检查本地代码是否有更改,并基于该检查更新现有的 build_android 和 build_ios 任务。我们还添加了 get-build 任务,用于查看已有的 Android 和 iOS 构建。
🌐 Let's add a fingerprint job to the workflow to check if there are changes to native code and update existing build_android and build_ios jobs based on that. We are also adding get-build jobs to look up existing builds for Android and iOS.
用以下代码更新 production.yml 文件:
🌐 Update the production.yml file with the following code:
在上面的工作流程中,fingerprint 任务首先运行,并根据代码库的更改生成一个哈希值。接着,get-build 任务会检查是否有已有的 Android 和 iOS 构建使用相同的指纹哈希。如果没有现有的构建,build 任务就会运行,生成新的生产构建。
🌐 In the above workflow, the fingerprint job runs first and generates a hash based on the changes in the codebase. The get-build jobs then check if there are existing builds for Android and iOS with the same fingerprint hash. If there are no existing builds, the build jobs run to create new builds for production.
fingerprint 任务中的 environment 字段告诉 EAS 在计算指纹时要加载哪些环境变量。我们的生产环境中,像 API_URL 或功能开关这样的变量可能有不同的值。这些值在 EAS 将它们烘焙进构建时可能会影响原生层。设置 environment: production 可以确保指纹反映实际发布的内容。
🌐 The environment field on the fingerprint job tells EAS which environment variables to load when computing the fingerprint. Our production environment may have different values for variables such as API_URL or feature flags. Those values can affect the native layer when EAS bakes them into the build. Setting environment: production ensures the fingerprint reflects what ships.
build_android 和 build_ios 任务上使用的 if 字段是一个布尔表达式。当它的值为 false 时,构建任务会被跳过。! 运算符意味着构建任务只在 get-build 没有找到匹配的构建时才会运行。
🌐 The if field used on the build_android and build_ios jobs is a boolean expression. When it evaluates to false, the build jobs are skipped. The ! operator means the build job runs only when get-build did not find a matching build.
3
添加一个更新任务
🌐 Add a job for updates
目前,我们的工作流程只有在本地代码有更改时才会创建生产版本。如果只是 TypeScript/JavaScript 文件有更改,我们希望触发 OTA 更新,而不是本地构建。
🌐 Currently, our workflow only creates production builds when there are changes to native code. If there are only changes to TypeScript/JavaScript files, we want to trigger an OTA update instead of a native build.
让我们添加两个作业 update_android 和 update_ios,当本地代码没有变化时运行,并使用 update 作业类型来触发 OTA 更新:
🌐 Let's add two jobs update_android and update_ios that run when there are no changes to native code and use update job type to trigger an OTA update:
我们的工作流程只构建面向生产的应用二进制文件或发布 OTA 更新。自动化应用商店提交是接下来常讨论的一个步骤,具体见下文的 自动化应用商店提交 部分。
🌐 Our workflow only builds production-ready app binaries or publishes OTA updates. Automating store submission is a common next step discussed below in the Automate app store submissions section.
4
创建一个发布分支并推送更改
🌐 Create a release branch and push changes
信息 注意: 确保此时工作流文件已经是我们 GitHub 仓库的一部分,并且在
main分支上可用。
为了测试我们的工作流程,让我们创建一个发布分支,然后从终端窗口将其推送到我们的 GitHub 仓库:
🌐 To test our workflow, let's create a release branch and push it to our GitHub repository from a terminal window:
打开 EAS 仪表板并找到工作流运行。由于这个指纹还没有构建,build 任务应该会运行,而 update 任务应该是灰色的。
🌐 Open the EAS dashboard and find the workflow run. Since no build exists yet for this fingerprint, the build jobs should run, and the update jobs should be grayed out.
Android 和 iOS 的构建正在同时进行。一旦每个任务完成,我们就会收到新的 Android 和 iOS 工件。
🌐 Both Android and iOS builds are running in parallel. Once each job completes, we receive a new artifact for Android and iOS.
现在,让我们在相同的发布分支上,在示例项目中仅做一个 TypeScript/JavaScript 的修改,然后使用下面的命令再次推送:
🌐 Now, let's make a TypeScript/JavaScript only change in our example project on the same release branch and push again using the commands below:
工作流运行后,注意在 EAS 仪表板上指纹与上次推送的构建匹配。build 任务完全被跳过,update 任务则发布了 OTA 更新。
🌐 After the workflow runs, notice in the EAS dashboard that the fingerprint matches the build from the last push. The build jobs are skipped entirely, and the update jobs publish an OTA update.
自动化应用商店提交
🌐 Automate app store submissions
我们在本章中构建的工作流程只会生成可用于生产的应用二进制文件,或者发布现有生产应用的 OTA 更新。自动化商店提交是生产发布工作流程中常见的下一步。
🌐 The workflow we have built in this chapter only builds production-ready app binaries or publishes OTA updates to an existing production app. Automating store submission is a common next step in a production release workflow.
EAS Workflows 提供了一个 submit 预打包任务,用于自动化应用商店提交。它要求我们使用 EAS CLI 来管理应用商店的凭证。同时,它还要求我们满足应用商店的要求,比如手动上传第一个 Android 版本(.aab)到 Google Play 商店。iOS 则有自己的 Apple 开发者计划注册和签名凭证需要设置。
🌐 EAS Workflows provides a submit pre-packaged job for automating app store submissions. It requires us to manage our app store credentials with EAS CLI. It also requires us to fulfill app store requirements, such as manually uploading the first Android release (.aab) to the Google Play Store. iOS has its own Apple Developer Program enrollment and signing credentials to set up.
一旦应用商店的要求就绪,我们就可以在 build 任务之后,在工作流程中添加两个新任务 submit_android 和 submit_ios:
🌐 Once the app store requirements are in place, we can add two new jobs submit_android and submit_ios to our workflow that run after the build jobs:
上面的 submit_android 和 submit_ios 任务需要一个 build_id 参数来知道要提交到应用商店的构建版本。它们只有在 build_android 和 build_ios 任务运行时才会执行,这意味着会创建一个新的生产构建。如果只修改了 TypeScript/JavaScript 文件,submit 任务也会被跳过,因为不会创建新的构建。
🌐 The above submit_android and submit_ios jobs require a build_id parameter to know which build to submit to the app stores. They run only when the build_android and build_ios jobs run, which means a new production build is created. If there are only changes to TypeScript/JavaScript files, the submit jobs are skipped as well since no new build is created.
概括
🌐 Summary
Chapter 5: Production deployments
We created a production workflow for Android and iOS triggered by pushes to a release branch, used fingerprinting to choose between a native build and an OTA update, and saw how EAS Submit automates app store submissions.
In the next chapter, learn how to switch production deployments from release branches to version tags.