简体   繁体   English

Expo 用于开发、UAT 和生产的应用环境

[英]Expo App environments for Dev, UAT and Production

I have a React Native app built in Expo that connects to a Rest API.我有一个内置于 Expo 的 React Native 应用程序,它连接到 Rest API。 There are three environments for the rest api - dev, uat and production as below (example). rest api 有三种环境 - 开发、uat 和生产,如下所示(示例)。

dev = https://dev.myapi.com/api
uat = https://uat.myapi.com/api
prod = https://prod.myapi.com/api

Depending on where the app is being used it needs to connect to the correct environment.根据应用程序的使用位置,它需要连接到正确的环境。

Running in the Expo Client = Dev API
Running in TestFlight or Internal Testing for the Play Store = UAT API
Running in the App Store or Play Store = Production API

What is the simplest way to achieve this?实现这一目标的最简单方法是什么?

This can be done using different Release Channel names, lets say you have created 3 release channels this way:这可以使用不同的发布通道名称来完成,假设您以这种方式创建了 3 个发布通道:

expo publish --release-channel prod
expo publish --release-channel staging
expo publish --release-channel dev

then you can have a function to set environment vars accordingly:那么你可以有一个 function 来相应地设置环境变量:

import * as Updates from 'expo-updates';

function getEnvironment() {
  if (Updates.releaseChannel.startsWith('prod')) {
    // matches prod*
    return { envName: 'PRODUCTION', dbUrl: 'ccc', apiKey: 'ddd' }; // prod env settings
  } else if (Updates.releaseChannel.startsWith('staging')) {
    // matches staging*
    return { envName: 'STAGING', dbUrl: 'eee', apiKey: 'fff' }; // stage env settings
  } else {
    // assume any other release channel is development
    return { envName: 'DEVELOPMENT', dbUrl: 'aaa', apiKey: 'bbb' }; // dev env settings
  }
}

Refer to expo documentation for more info!有关详细信息,请参阅expo 文档

Follow below Steps按照以下步骤

  1. Install expo-constants package.安装expo-constants package。 To install package run below command.要安装 package,请运行以下命令。

    npm i expo-constants npm i expo-constants

  2. Add environment.js file and paste below code.添加environment.js文件并粘贴以下代码。

import Constants from "expo-constants";
import { Platform } from "react-native";

const localhost =
Platform.OS === "ios" ? "localhost:8080" : "10.0.2.2:8080";

const ENV = {
dev: {
  apiUrl: "https://dev.myapi.com/api",
  amplitudeApiKey: null,
},
staging: {
  apiUrl: "https://uat.myapi.com/api",
  amplitudeApiKey: "[Enter your key here]",
  // Add other keys you want here
},
prod: {
  apiUrl: "https://prod.myapi.com/api",
  amplitudeApiKey: "[Enter your key here]",
  // Add other keys you want here
}
};

const getEnvVars = (env = Constants.manifest.releaseChannel) => {
// What is __DEV__ ?
// This variable is set to true when react-native is running in Dev mode.
// __DEV__ is true when run locally, but false when published.
if (__DEV__) {
  return ENV.dev;
} else if (env === 'staging') {
  return ENV.staging;
} else if (env === 'prod') {
  return ENV.prod;
}
};

export default getEnvVars;
  1. Accessing Environment Variables访问环境变量
// Import getEnvVars() from environment.js
import getEnvVars from '../environment';
const { apiUrl } = getEnvVars();

/******* SESSIONS::LOG IN *******/
// LOG IN
// credentials should be an object containing phone number:
// {
//   "phone" : "9876342222"
// }
export const logIn = (credentials, jsonWebToken) => (
 fetch(`${apiUrl}/phone`, {
   method: 'POST',
   headers: {
     'Authorization': 'Bearer ' + jsonWebToken,
     'Content-Type': 'application/json',
   },
   body: JSON.stringify(credentials)
 })
);
  1. To create the builds use the below commands.要创建构建,请使用以下命令。

Dev - expo build:ios --release-channel dev开发 - expo 构建:ios --release-channel dev

Staging - expo build:ios --release-channel staging暂存 - expo 构建:ios --release-channel 暂存

Production - expo build:ios --release-channel prod生产 - expo build:ios --release-channel prod

Now that Expo supports config file as app.config.js or app.config.ts, we can use the dotenv.现在 Expo 支持 app.config.js 或 app.config.ts 等配置文件,我们可以使用 dotenv。 Check this: https://docs.expo.io/guides/environment-variables/#using-a-dotenv-file检查这个: https://docs.expo.io/guides/environment-variables/#using-a-dotenv-file

For those who are using Expo sdk 46(or any newer version), you can do the following way对于正在使用 Expo sdk 46(或任何更新版本)的用户,您可以执行以下方式

  1. Rename the app.json to app.config.js将 app.json 重命名为 app.config.js
  2. Add API URL under extra property在额外属性下添加 API URL
export default () => ({
  expo: {
    name: '',
    slug: ''
    extra: {
      API_URL: process.env.API_URL || null,
    },
    // ...
  },
});

We can access this API using expo constants like this(wherever we want).我们可以使用像这样的 expo 常量访问这个 API(我们想要的任何地方)。 Don't forget to import constants from Expo.不要忘记从 Expo 导入常量。

const myApi = Constants.expoConfig.extra.API_URL 
axios.get(myApi).... // using API END POINT

For Local development to access API you can do it in two ways对于本地开发访问 API 您可以通过两种方式进行

  1. API_URL="http:// localhost:3000" expo start API_URL="http://localhost:3000" expo 启动
  2. Just comment the Contants.expoConfig..... and directly paste local URL like const myApi = "http:// localhost:3000"只需注释 Contants.expoConfig..... 并直接粘贴本地 URL 就像 const myApi = "http://localhost:3000"

And in eas.json并在 eas.json

{
  "production": {
    "env": {
      "API_URL": "https://prod.example.com"
    }
  },
  "staging": {
    "env": {
      "API_URL": "https://staging.example.com"
    }
  }
}

Once we run eas build the appropriate API endpoint will be set.一旦我们运行 eas build,就会设置适当的 API 端点。 Refer to the same in Expo documentation https://docs.expo.dev/eas-update/environment-variables/参考 Expo 文档https://docs.expo.dev/eas-update/environment-variables/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM