构建生命周期钩子

了解如何将 EAS Build 生命周期钩子与 npm 结合使用来自定义构建过程。


EAS Build 生命周期 npm hooks 允许你通过在构建过程之前或之后运行脚本来自定义构建过程。

🌐 EAS Build lifecycle npm hooks allows you to customize your build process by running scripts before or after the build process.

为了更好地理解,请参阅 Android 构建流程iOS 构建流程

警告 生命周期钩子在 自定义构建 中不会被构建过程执行。它们需要在构建过程中由构建步骤手动提取并调用。

EAS Build 生命周期钩子

🌐 EAS Build lifecycle hooks

有六个 EAS Build 生命周期的 npm 钩子可用。要使用它们,你可以在 package.json 中进行设置。

🌐 There are six EAS Build lifecycle npm hooks available. To use, them, you can set them in your package.json.

Build Lifecycle npm hookDescription
eas-build-pre-installExecuted before EAS Build runs npm install.
eas-build-post-installThe behavior depends on the platform and project type.

For Android, runs once after the following commands have all completed: npm install and npx expo prebuild (if needed).

For iOS, runs once after the following commands have all completed: npm install, npx expo prebuild (if needed), and pod install.
eas-build-on-successThis hook is triggered at the end of the build process if the build was successful.
eas-build-on-errorThis hook is triggered at the end of the build process if the build failed.
eas-build-on-completeThis hook is triggered at the end of the build process. You can check the build's status with the EAS_BUILD_STATUS environment variable. It's either finished or errored.
eas-build-on-cancelThis hook is triggered if the build is canceled.

使用一个或多个生命周期钩子时,package.json 的示例:

🌐 An example of how a package.json can look when using one or more lifecycle hooks:

package.json
{ "name": "my-app", "scripts": { "eas-build-pre-install": "echo 123", "eas-build-post-install": "echo 456", "eas-build-on-success": "echo 789", "eas-build-on-error": "echo 012", "eas-build-on-cancel": "echo 345", "start": "expo start", "test": "jest" }, "dependencies": { "expo": "54.0.0" %%placeholder-start%%... %%placeholder-end%% } }

特定于平台的钩子行为

🌐 Platform-specific hook behavior

要仅针对 Android 或 iOS 构建运行脚本(或脚本的某些部分),你可以在脚本中根据平台分支执行不同的操作。请参阅以下常见示例,通过 shell 脚本或 Node 脚本来实现此操作。

🌐 To run a script (or some part of a script) only for Android or iOS builds, you can fork the behavior depending on the platform within the script. See the following common examples to do this through a shell script or a Node script.

示例

🌐 Examples

package.json 和 shell 脚本

🌐 package.json and shell script

package.json
{ "name": "my-app", "scripts": { "eas-build-pre-install": "./pre-install", "start": "expo start" %%placeholder-start%%... %%placeholder-end%% }, "dependencies": { %%placeholder-start%%... %%placeholder-end%% } }
pre-install
#!/bin/bash # This is a file called "pre-install" in the root of the project if [[ "$EAS_BUILD_PLATFORM" == "android" ]]; then echo "Run commands for Android builds here" elif [[ "$EAS_BUILD_PLATFORM" == "ios" ]]; then echo "Run commands for iOS builds here" fi
示例:在 macOS 工作节点上安装 git-lfs 的预安装脚本

以下脚本会在未安装 git-lfs 的情况下进行安装。这在某些需要 git-lfs 才能安装特定 CocoaPods 的情况下很有用。

🌐 The following script installs git-lfs if it is not yet installed. This is useful in some cases where git-lfs is required to install certain CocoaPods.

pre-install
if [[ "$EAS_BUILD_PLATFORM" == "ios" ]]; then if brew list git-lfs > /dev/null 2>&1; then echo "=====> git-lfs is already installed." else echo "=====> Installing git-lfs" HOMEBREW_NO_AUTO_UPDATE=1 brew install git-lfs git lfs install fi fi

package.json 和 Node 脚本

🌐 package.json and Node script

package.json
{ "name": "my-app", "scripts": { "eas-build-pre-install": "node pre-install.js", "start": "expo start" // ... }, "dependencies": { // ... } }
pre-install.js
// Create a file called "pre-install.js" at the root of the project if (process.env.EAS_BUILD_PLATFORM === 'android') { console.log('Run commands for Android builds here'); } else if (process.env.EAS_BUILD_PLATFORM === 'ios') { console.log('Run commands for iOS builds here'); }