首页指南参考教程

将环境变量与 EAS 更新结合使用

了解如何通过 EAS 更新设置和使用环境变量。


将 .env 文件与 EAS 更新结合使用

¥Using .env files with EAS Update

Expo 中的环境变量 描述了如何使用 .env 文件在 JavaScript 代码中设置和使用环境变量。Expo CLI 会将代码中正确添加前缀的变量(例如,process.env.EXPO_PUBLIC_VARNAME)替换为开发计算机上存在的 .env 文件中的相应环境变量值。

¥Environment variables in Expo describes how to use .env files to set and use environment variables within your JavaScript code. The Expo CLI will substitute properly prefixed variables in your code (for example,process.env.EXPO_PUBLIC_VARNAME) with the corresponding environment variable values in .env files present on your development machine.

当你运行 eas update 时,将在打包 JavaScript 时评估存在的任何 .env 文件。应用代码中的任何 EXPO_PUBLIC_ 变量都将替换为发布更新的计算机上存在的 .env 文件中的相应值,无论该计算机是本地计算机还是 CI/CD 服务器。

¥When you run eas update, any .env files present will be evaluated when your JavaScript is bundled. Any EXPO_PUBLIC_ variables in your application code will be replaced inline with the corresponding values from your .env files that are present on the machine from which the update is published, whether that is your local machine or your CI/ CD server.

使用 EAS 更新发布时,.env 文件中的 EXPO_PUBLIC_ 变量仅在打包应用的 JavaScript 时使用。在评估 app.config.js 时它们将不可用。

¥When publishing with EAS Update, EXPO_PUBLIC_ variables in .env files will only be used when bundling your app's JavaScript. They will not be available when evaluating app.config.js.

在本地开发、EAS 更新和 EAS 构建之间共享环境变量

¥Sharing environment variables between local development, EAS Update, and EAS Build

你的本地开发环境和 EAS 更新可以使用 .env 文件将 EXPO_PUBLIC_ 变量内联到你的应用源代码中,只要它们存在于正在运行 eas update 命令的计算机上。同时,EAS Build 还支持定义环境变量,这些变量将在 EAS Build 服务器上的 eas.json 中可用。

¥Your local development environment and EAS Update can use .env files to inline EXPO_PUBLIC_ variables into your app source code as long as they are present on the machine the eas update command is being run. Meanwhile, EAS Build also supports defining environment variables that will be available on the EAS Build server in eas.json.

确保在所有情况下定义正确变量的一种方法是在 .env 文件中定义变量以进行本地开发和 EAS 更新,同时在 eas.json 中的第二个位置设置相同的变量以进行 EAS 构建。然而,可以通过将通用环境配置合并到代码中并基于 EAS 更新通道进行切换来避免这种重复。

¥One approach to ensure the correct variables are defined in all cases would be to define your variables in .env files for local development and EAS Update, while setting the same variables in a second place in eas.json for EAS Build. However, this duplication can be avoided by consolidating common environment configurations into code and switching based on EAS Update channel.

例如,你可能在 eas.json 中定义了单独的 stagingproduction 通道用于 EAS 更新:

¥For example, you might have separate staging and production channels for EAS Update defined in your eas.json:

eas.json
{
  "build": {
    "production": {
      "channel": "production"
    },
    "staging": {
      "channel": "staging"
    }
  }
}

每个通道使用不同的 apiUrlenableHiddenFeatures 启用实验性功能标志,并且这些值很少改变。不要在 eas.json 内的 env 中定义这些值,而是在项目中创建一个 Config.js 文件,根据通道导出正确的值:

¥Each channel uses a different apiUrl, and enableHiddenFeatures enables an experimental feature flag, and these values rarely change. Instead of defining these values in the env inside eas.json, create a Config.js file in your project that exports the correct values based on the channel:

Config.js
import * as Updates from 'expo-updates';

let Config = {
  apiUrl: 'https://localhost:3000',
  enableHiddenFeatures: true,
};

if (Updates.channel === 'production') {
  Config.apiUrl = 'https://api.production.com';
  Config.enableHiddenFeatures = false;
} else if (Updates.channel === 'staging') {
  Config.apiUrl = 'https://api.staging.com';
  Config.enableHiddenFeatures = true;
}

export default Config;

由于使用 EAS Build 时为构建设置了 channel,并且为 EAS Update 配置的构建仅从该通道下载更新,因此使用通道设置环境可确保为所有独立构建使用正确的值,无论是运行原始嵌入式 JavaScript 还是运行 更新。

¥Since channel is set for a build when using EAS Build, and builds configured for EAS Update only download updates from that channel, using channel to set your environment ensures that the correct values are used for all standalone builds, whether running the original embedded JavaScript or an update.

设置自定义本地环境

¥Setting a custom local environment

请注意,上述解决方案仅允许一种本地配置。这可能不够灵活,因为有时将本地开发环境指向不同的服务器是很常见的。我们可以更新 Config.js 以查看我们的 .env 文件:

¥Note the above solution only allows for one local configuration. That may not be flexible enough, as it is common to sometimes point your local development environment to different servers. We can update Config.js to also look at our .env files:

Config.js
let Config = {
  apiUrl: process.env.EXPO_PUBLIC_API_URL || 'https://localhost:3000',
  enableHiddenFeatures: process.env.EXPO_PUBLIC_ENABLE_HIDDEN_FEATURES || true,
};

// set variables based on channel...

export default Config;

现在你可以在未提交的 .env.local 文件中设置 EXPO_PUBLIC_API_URLEXPO_PUBLIC_ENABLE_HIDDEN_FEATURES,并且在运行 npx expo start 时将使用它们而不是默认值。

¥Now you can set EXPO_PUBLIC_API_URL and EXPO_PUBLIC_ENABLE_HIDDEN_FEATURES in an uncommitted .env.local file, and they will be used instead of the default when running npx expo start.

这也可用于从 开发构建 运行的 EAS 更新。例如,你可以通过提交设置了 EXPO_PUBLIC_API_URL 的 .env 文件,将为 PR 预览 创建的更新指向特定 API 服务器。

¥This can also be used for EAS Updates run from your development build. For example, you can point updates created for PR previews to a specific API server by committing an .env file with EXPO_PUBLIC_API_URL set.

在 app.config.js 中使用变量

¥Using variables in app.config.js

Expo 环境变量 仅在 SDK 49 或更高版本中可用。在以前的版本中,通常在 expo.extra 属性下的 app.config.js 中设置要在更新中使用的变量:

¥Expo environment variables are only available in SDK 49 or higher. In previous versions, it was common to set variables to be used in updates in app.config.js under the expo.extra property:

app.config.js
export default () => ({
  expo: {
    extra: {
      API_URL: process.env.API_URL || null,
    },
    // ...
  },
});

然后,要在开发过程中设置 API_URL 环境变量,你可以在运行 npx expo start 之前添加变量,如下所示:

¥Then, to set the API_URL environment variable during development, you can prepend the variables before running npx expo start as shown below:

Terminal
API_URL="http://localhost:3000" expo start

该命令将 API_URL 设置为 "http://localhost:3000"。然后,expo-constants 库提供 Constants.expoConfig.extra.API_URL 属性以在项目内访问它。

¥The command sets the API_URL to "http://localhost:3000". Then, the expo-constants library provides the Constants.expoConfig.extra.API_URL property to access it inside a project.

在 SDK 49 及更高版本中仍然可以访问变量 expo.extra,但建议使用 EXPO_PUBLIC_ 前缀和 .env 文件直接在应用代码中引用变量。

¥Variables expo.extra are still accessible in SDK 49 and higher, but it is recommended to use the EXPO_PUBLIC_ prefix and .env files instead to reference variables directly in your application code.

Expo 中文网 - 粤ICP备13048890号