了解如何使用自定义构建工作流程扩展 EAS Build。
自定义构建允许在云上运行测试、添加原生平台的指令、放弃构建以与你的团队或多个测试设备共享等等。使用 EAS Build,你可以创建构建配置来自定义流程,这样你就不仅限于生成 Android 和 iOS 应用。你还可以在 CI 环境中执行任何你想做的事情。
¥Custom builds allow running tests on the cloud, adding instructions for native platforms, resigning a build to share with your team or multiple test devices, and so on. With EAS Build, you can create build configs to customize the process so that you're not limited to generating Android and iOS applications. You can also do anything that you might want to do in a CI environment.
本指南展示了如何通过 EAS 创建和使用自定义构建配置。它遵循一个场景,你创建自定义工作流程以在 EAS Build 上运行测试。你还可以在其他场景中使用定义的步骤。
¥This guide shows how to create and use a custom build config with EAS. It follows a scenario where you create a custom workflow to run tests on EAS Build. You can also use the defined steps in other scenarios.
1
¥Add a test to the project
要创建运行测试的构建配置,你必须通过安装 jest-expo
和 jest
作为依赖来准备项目。运行以下命令:
¥To create a build config that runs tests, you have to prepare your project by installing jest-expo
and jest
as dependencies. Run the following command:
-
npx expo install -- --save-dev jest-expo jest react-test-renderer
接下来,在 package.json 中添加 test
脚本:
¥Next, add a test
script in package.json:
{
"scripts": {
%%placeholder-start%%... %%placeholder-end%%
"test": "jest"
},
"jest": {
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
]
}
}
为了简洁起见,让我们在项目的根目录下创建一个 App.test.js,然后添加以下代码片段:
¥For brevity, let's create an App.test.js at the root of your project and then add the following snippet:
import renderer from 'react-test-renderer';
import App from './App';
describe('<App />', () => {
it('has 1 child', () => {
const tree = renderer.create(<App />).toJSON();
expect(tree.children.length).toBe(1);
});
});
有关配置
jest-expo
包和其他配置选项(例如transformIgnorePatterns
)的更多信息,请参阅 使用 Jest 进行单元测试。¥See Unit testing with Jest for more information about configuring the
jest-expo
package and additional configuration options such astransformIgnorePatterns
.
2
¥Create a workflow
你必须在项目中添加构建配置文件才能创建工作流程。创建与 eas.json 同级的目录路径.eas/build。例如,如果 eas.json 文件位于项目的根目录,请在该级别创建目录。两个目录的路径和名称对于 EAS Build 识别项目包含自定义构建配置都很重要。
¥You must add a build config file in your project to create a workflow. Create a directory path .eas/build at the same level as eas.json. For example, if the eas.json file is at the root of your project, create the directory at that level. Both path and name of both directories are important for EAS Build to identify that a project contains a custom build config.
在其中,你可以创建一个名为 test.yml 的新文件。该文件包含你要运行的工作流配置。文件名并不重要;你可以随意命名它。唯一的要求是文件扩展名使用 .yml。
¥Inside it, you can create a new file called test.yml. This file contains the workflow config that you want to run. The filename is unimportant; you can name it whatever you want. The only requirement is that the file extension uses .yml.
该文件将包含在 EAS 构建服务上运行测试的步骤。它将从项目的 package.json 文件安装所有必需的依赖并执行 npm test
脚本。
¥This file will contain steps to run tests on the EAS Build service. It'll install all the required dependencies from the package.json file of your project and execute the npm test
script.
在文件中添加以下工作流程步骤:
¥Add the following workflow steps in the file:
build:
name: Run tests
steps:
- eas/checkout
- run:
name: Install dependencies
command: npm install
- run:
name: Run tests
command: |
echo "Running tests…"
npm test
3
config
属性¥Add config
property in eas.json
要使用自定义构建配置,你必须在 eas.json 中添加 config
属性。它包含构建配置文件名作为你想要在 EAS 上运行的值。
¥To use the custom build config, you must add the config
property in eas.json. It contains the build config filename as the value you want to run on the EAS.
让我们在 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:
{
"build": {
%%placeholder-start%%... %%placeholder-end%%
"test": {
"config": "test.yml",
"withoutCredentials": true
},
}
使用 withoutCredentials
选项跳过 CLI 端的凭据设置,因为运行测试不需要它们。
¥Use the withoutCredentials
option to skip the credentials setup on the CLI side, as they are not needed for running tests.
如果你希望为每个平台使用单独的配置,你可以为 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:
{
"build": {
%%placeholder-start%%... %%placeholder-end%%
"test": {
"ios": {
"config": "test-ios.yml",
},
"android": {
"config": "test-android.yml",
},
"withoutCredentials": true
},
}
4
¥Run build to test the workflow
要测试工作流程,请运行以下命令:
¥To test the workflow, run the following command:
-
eas build -p android -e test
构建完成后,你可以通过查看构建详情页面的日志来验证 npm test
脚本是否被执行。
¥After the build is complete, you can verify that the npm test
script was executed by checking the logs on the build details page.
¥Summary
通过添加你想要在 EAS 服务上运行的步骤作为现有常规构建的扩展,可以自定义构建配置。
¥Build configs are customizable by adding steps you want to run on the EAS service as an extension to your existing regular builds.
你可以按照与常规构建相同的方式配置自定义构建的凭据。如果你想运行不需要凭据的自定义构建,你可以使用 withoutCredentials
选项,如上所示。请参阅 应用凭证文档 了解更多信息。
¥You can configure the credentials for custom builds the same way as for regular builds. If you want to run a custom build that doesn't require credentials, you can use the withoutCredentials
option as shown above. See App credentials docs for more information.
如果你的常规构建有 EAS 秘密,它们也可用于自定义构建。
¥If your regular builds have EAS Secrets, they are also available for custom builds.
查看示例存储库以获取更详细的示例:
¥Check out the example repository for more detailed examples:
自定义 EAS 构建示例,其中包括工作流程示例,例如设置函数、使用环境变量、上传工件等。