开始定制构建

了解如何使用自定义构建扩展 EAS Build。


自定义构建允许通过在构建过程的前、中或后执行命令来定制项目的构建流程。自定义构建可以通过 EAS CLI 运行,也可以在 React Native CI/CD 管道中运行构建时使用,例如通过 EAS 工作流

🌐 Custom builds allow customizing the build process for your project by running commands before, during, or after the build process. Customized builds can run from EAS CLI or when running builds in a React Native CI/CD pipeline, like with EAS Workflows.

1

创建自定义构建配置

🌐 Create a custom build config

要开始,请在与 eas.json 同级的位置创建目录和一个名为 .eas/build/hello-world.yml 的文件。目录的位置和名称对于 EAS Build 来说很重要,以便识别项目包含自定义构建配置。

🌐 To get started, create directories and a file named .eas/build/hello-world.yml at the same level as eas.json. The location and name of both directories are important for EAS Build to identify that a project contains a custom build config.

hello-world.yml 文件中,你将编写自定义的构建配置。文件名并不重要,你可以随意命名。唯一的要求是文件扩展名必须使用 .yml

🌐 Inside the hello-world.yml, you'll write your custom build config. The filename is unimportant; you can name it whatever you want. The only requirement is that the file extension uses .yml.

在文件中添加以下自定义构建配置步骤:

🌐 Add the following custom build config steps in the file:

.eas/build/hello-world.yml
build: name: Hello World! steps: - run: echo "Hello, world!" # A built-in function (optional)

在现实世界的场景中,你将调用一个内置函数来触发构建。

🌐 In a real world scenario, you will call a built-in function to trigger the build.

2

在 eas.json 中添加 config 属性

🌐 Add config property in eas.json

要使用自定义构建配置,请在 eas.json 的构建配置文件下添加 config 属性。

🌐 To use the custom build config, add the config property in eas.json under a build profile.

让我们在 build 下创建一个名为 test 的新构建配置,以运行 test.yml 文件中的自定义配置:

🌐 Let's create a new build profile called test under build to run the custom config from the test.yml file:

eas.json
{ "build": { %%placeholder-start%%... %%placeholder-end%% "test": { "config": "test.yml", }, }

如果你希望为每个平台使用独立的配置,可以为 Android 和 iOS 创建单独的 YAML 配置文件。例如:

🌐 If you wish to use separate configs for each platform, you can create separate YAML config files for Android and iOS. For example:

eas.json
{ "build": { %%placeholder-start%%... %%placeholder-end%% "test": { "ios": { "config": "hello-ios.yml", }, "android": { "config": "hello-android.yml", } }, }

3

运行构建以测试自定义构建配置

🌐 Run a build to test the custom build config

要测试自定义构建配置,请运行以下命令:

🌐 To test the custom build config, run the following command:

Terminal
eas build -p android -e test

构建完成后,你可以通过检查构建详情页面上的日志来验证 echo "Hello World!" 脚本是否已执行。

🌐 After the build is complete, you can verify that the echo "Hello World!" script was executed by checking the logs on the build's detail page.

了解更多

🌐 Learn more

查看示例存储库以获取更详细的示例:

🌐 Check out the example repository for more detailed examples:

自定义构建示例存储库

一个自定义 EAS 构建示例,其中包含自定义构建的示例,如设置函数、使用环境变量、上传工件等。