EAS 中的环境变量
如何在构建、更新和工作流中使用 Expo 应用服务 (EAS) 环境变量的概述。
本指南解释了如何在 Expo 应用服务(EAS):构建、更新、工作流和托管中使用环境变量。有关环境变量在 Expo 框架中如何工作的常规信息,请参阅 Expo 中的环境变量。
🌐 This guide explains how to use Environment Variables in Expo Application Services (EAS): Build, Updates, Workflows, and Hosting. For general information on how environment variables work with the Expo framework, refer to Environment variables in Expo.
在本地开发期间,环境变量会从本地的 .env 或 .env.local 文件中加载。这些文件通常被排除在项目的版本控制之外(也就是说,如果它们被列在 .gitignore 文件中或未提交),因此在远程服务器上运行的任务(例如 EAS Build 和 EAS Workflows)无法使用它们。此外,大多数项目有多个应用变体,并且需要多套环境变量(例如开发环境和生产环境)。
🌐 During a local development, environment variables are loaded from your local .env or .env.local files. These files are generally excluded from the project's version control (that is, if they are listed in the .gitignore file or not committed) so they are not available for jobs that run on a remote server, for example, EAS Build and EAS Workflows. Additionally, most projects have multiple app variants and require multiple sets of environment variables (for instance Development and Production).
为什么使用 EAS 环境变量
🌐 Why use EAS environment variables
如果你需要,你可能想使用 EAS 环境变量:
🌐 You might want to use EAS Environment Variable if you need to:
- 将配置集中管理,用于云构建、更新和工作流程,而无需提交 .env 文件。
- 在不同环境(
development、preview、production)中分开值,同时重复使用名称。 - 控制可见性(普通文本、敏感、密钥),确保只有合适的界面可以读取每个值。
- 在本地使用
eas env:pull或在 CI/CD 中应用相同的设置。
这些是 EAS 环境变量旨在解决的问题。使用 EAS 环境变量,这些变量可以通过 EAS CLI 或直接在 expo.dev 仪表板上进行配置,并且可以被 EAS Build、EAS 工作流以及通过 EAS CLI 的本地计算机访问。
🌐 These are the problems that EAS Environment variables is built to solve. With EAS Environment variables, the variables are configured in EAS via the EAS CLI or directly on the expo.dev dashboard, and can be accessed by EAS Build and EAS Workflows as well as your local machine via the EAS CLI.
快速开始
🌐 Quick start
1
要创建一个新的环境变量,请使用 EAS CLI 并在你的项目目录中运行以下命令。以下命令将在 production 环境中创建一个名为 EXPO_PUBLIC_API_URL、值为 https://api.example.com 的新环境变量。
🌐 To create a new environment variable, use the EAS CLI and run the following command inside your project directory. The following command creates a new environment variable with the name EXPO_PUBLIC_API_URL and the value https://api.example.com for the production environment.
- eas env:create --name EXPO_PUBLIC_API_URL --value https://api.example.com --environment production --visibility plainText2
通过确认它在项目设置的环境变量中显示为生产标志,来验证环境变量是否创建成功。环境变量也可以直接在expo.dev上管理。
🌐 Verify if the environment variable is created successfully by confirming it appears with the production badge in the Environment variables in your project settings. Environment variables can also be managed directly on expo.dev.
3
要在 EAS Build 中使用环境变量,请在 prodcuction 构建配置中添加 environment 字段:
🌐 To use the environment variable in EAS Build, add environment field to the prodcuction build profile:
{ "build": { "production": { "environment": "production" } } }
现在,为 production 构建配置创建的任何环境变量将在构建过程中可用。
🌐 Now, any environment variables created for the production build profile will be available during the build process.
4
要在 EAS 更新中使用环境变量,请运行指定了 --environment 标志的 eas update 命令:
🌐 To use the environment variable with EAS Update, run the eas update command with the --environment flag specified:
- eas update --environment production--environment 标志用于指定更新任务所使用的环境。在更新过程中,只会使用来自指定环境的环境变量。
🌐 The --environment flag is used to specify the environment to use for the update job. Only the environment variables from the specified environment will be used during the update process.
5
要在 EAS Hosting 中使用环境变量,请运行带有指定 --environment 标志的 eas deploy 命令。如果你需要同时使用客户端和服务器端环境变量,请按所列顺序运行以下命令(客户端变量必须是明文或敏感的,而非密钥),详情请参见 客户端环境变量。
🌐 To use the environment variable with EAS Hosting, run the eas deploy command with the --environment flag specified. If you need both client and server side environment variables, run the commands below in the order they are listed (client-side variables must be plain text or sensitive, not secret) and see client-side environment variables for details information.
- eas env:pull --environment production- npx expo export --platform web- eas deploy --environment production--environment 标志用于指定部署任务所使用的环境。在部署过程中,只会使用来自指定环境的环境变量。
🌐 The --environment flag is used to specify the environment to use for the deploy job. Only the environment variables from the specified environment will be used during the deploy process.
关键概念
🌐 Key concepts
可用环境
🌐 Available environments
默认情况下,EAS 支持三种环境变量环境:development、preview 和 production 环境。企业版和生产版计划支持自定义环境名称。
🌐 By default, EAS supports three environments for environment variables: development, preview, and production environments. Custom environment names are available on Enterprise and Production plans.
每个环境都是一组独立的变量,可以用来在不同的上下文中定制你的应用。例如,你可以在开发和生产环境中使用不同的 API 密钥,或在应用商店发布时使用不同的应用包标识符。
🌐 Each environment is an independent set of variables that can be used to customize your app in different contexts. For example, you can use different API keys for development and production, or use different bundle identifiers for app store releases.
每个 EAS 构建和工作流任务都会使用来自可用环境之一的环境变量。你也可以在更新时使用环境,这样就可以为你的构建任务使用同一组环境变量。在发布更新时,你可以通过提供 --environment 标志来实现这一点。
🌐 Every EAS Build and Workflows job runs using environment variables from one of the available environments. You can also use environments for updates, allowing you to use the same set of environment variables for your build jobs. You can do this when publishing an update by providing the --environment flag.
环境变量可以分配给多个环境,并在所有环境中具有相同的值,或者仅为单个环境创建。
🌐 Environment variables can be assigned to multiple environments and have the same value across all of them, or be created for a single environment.
范围
🌐 Scope
- 跨项目:特定于单个 EAS 项目。你可以通过在项目仪表板中导航到 环境变量 页面来创建、查看和管理它们。这些环境变量可以在任何运行在 EAS 服务器上的作业中使用,并可用于该项目的更新。如果其可见性设置允许,它们也可以在本地拉取以进行开发。
- 账户范围:在你 EAS 账户的所有项目中均可使用。你可以通过导航至账户仪表板中的 环境变量 页面来创建、查看和管理它们。这些环境变量可在 EAS 服务器上运行的作业中使用,并会与项目范围内的变量一起更新。若其 可见性设置 允许,你也可以在本地提取或在 EAS 服务器之外读取它们。
变量类型
🌐 Variable types
- 字符串:可在构建、更新、工作流和托管中使用的标准键/值对。
- 文件:作为文件上传的值(例如,
google-services.json或证书),这些文件会作为文件路径提供给构建运行器上的作业使用。
环境变量的可见性设置
🌐 Visibility settings for environment variables
每个环境变量都有三种不同的可见性设置可用:
🌐 There are three different visibility settings available for each environment variable:
| 可见性 | 描述 |
|---|---|
| 普通文本 | 在网站、EAS CLI 和日志中可见。 |
| 敏感 | 在 EAS Build 和 Workflows 作业日志中被模糊处理。你可以切换选项在网站上显示它们。在 EAS CLI 中也可以读取。 |
| 秘密 | 在 EAS 服务器之外无法读取,包括在网站和 EAS CLI 中。在 EAS Build 和 Workflows 作业日志中会被模糊处理。 |
警告 请注意,任何包含在客户端代码中的内容都应被视为公开的,并且可以被运行你应用的任何人读取。
警告 秘密类型环境变量旨在为 EAS 构建或工作流任务提供值,以便可以用来改变任务的运行方式。例如,设置
NPM_TOKEN以从 npm 安装私有包,或者设置 Sentry API 密钥以创建发布并上传你的源映射文件。秘密并不会为你最终嵌入到应用中的值提供额外的安全性。
下一步去哪
🌐 Where to go next
了解如何使用 EAS 控制台和 EAS CLI 创建、作用域和使用环境变量。
了解如何在 EAS 构建、更新、托管和工作流程任务中使用环境变量。
了解在 Expo 和 React Native 项目中管理环境变量的非 EAS 方法。
关于 EAS 中环境变量的常见问题解答。