Warm tip: This article is reproduced from serverfault.com, please click

react native-使用GitLab进行构建时Expo中的环境变量

(react native - environment variables in Expo when building with GitLab)

发布于 2020-11-13 12:53:05

我正在尝试用Expo构建一个React Native APK(不弹出)。当我expo build:android在本地计算机上处​​理所有项目文件时,可以设法从.env文件本地获取环境变量

当我推送到我的GitLab存储库时,我有这个.gitlab-ci.yml文件

---
image: node:alpine
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - ~/.npm
    - .jest
stages:
  - test
  - deploy
before_script:
  - npm ci
jest-tests:
  stage: test
  script:
    - npx jest --ci --passWithNoTests
expo-deployments:
  stage: deploy
  script:
    - apk add --no-cache bash
    - npx expo login -u $EXPO_USERNAME -p $EXPO_PASSWORD
    - npx expo build:android --EXPO_ANDROID_GOOGLE_API_KEY $EXPO_ANDROID_GOOGLE_API_KEY --EXPO_IOS_GOOGLE_API_KEY $EXPO_IOS_GOOGLE_API_KEY --release-channel staging --non-interactive
    - EXPO_ANDROID_GOOGLE_API_KEY=$EXPO_ANDROID_GOOGLE_API_KEY; EXPO_IOS_GOOGLE_API_KEY=$EXPO_IOS_GOOGLE_API_KEY; expo build:android --release-channel staging --non-interactive

由于安全性,我在存储库中没有.env文件。

所有这些变量都存储在GitLab中的每个环境中的位置:(完美运行)

  1. EXPO_USERNAME =我用于访问Expo的开发帐户的用户名。
  2. EXPO_PASSWORD =用于访问Expo的帐户密码。

(尝试构建时根本不起作用)

  1. EXPO_IOS_GOOGLE_API_KEY =“ abcdefghijklmnopqrstuvwxyz”
  2. EXPO_ANDROID_GOOGLE_API_KEY =“ abcdefghijklmnopqrstuvwxz”

我想知道如何通过GitLab CI管道运行expo build:android命令时如何在应用程序中设置Google Maps环境变量:

- npx expo build:android

GitLab CI / CD管道在expo build:android失败

Questioner
Gonxis
Viewed
11
Gonxis 2020-11-30 15:34:02

最后,我可以设法通过以下方式进行这项工作:

---
image: node:alpine
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - ~/.npm
    - .jest
stages:
  - test
  - deploy
before_script:
  - npm ci
  - ANDROID_GOOGLE_MAPS_API_KEY=${CI_COMMIT_BRANCH}_EXPO_ANDROID_GOOGLE_API_KEY
  - IOS_GOOGLE_MAPS_API_KEY=${CI_COMMIT_BRANCH}_EXPO_IOS_GOOGLE_API_KEY
  - export android=$( eval echo \$$ANDROID_GOOGLE_MAPS_API_KEY )
  - export ios=$( eval echo \$$IOS_GOOGLE_MAPS_API_KEY )
jest-tests:
  stage: test
  script:
    - npx jest --ci --passWithNoTests
expo-deployments:
  stage: deploy
  script:
    - echo "EXPO_ANDROID_GOOGLE_API_KEY=$android" >> .env
    - echo "EXPO_IOS_GOOGLE_API_KEY=$ios" >> .env
    - apk add --no-cache bash
    - npx expo login -u $EXPO_USERNAME -p $EXPO_PASSWORD
    - npx expo build:android --release-channel staging --non-interactive

在哪里...

 ...CI_COMMIT_BRANCH is development|staging|production depending which branch in gitlab I am using.
 ...development_EXPO_ANDROID_GOOGLE_API_KEY | staging_EXPO_ANDROID_GOOGLE_API_KEY | production_EXPO_ANDROID_GOOGLE_API_KEY are variables that are stored at the Gitlab project.
 ... I am generating the .env file every time I run the script and saving there my variables with the respective value with: echo "EXPO_ANDROID_GOOGLE_API_KEY=$android" >> .env.

这样,我不需要将.env文件推送到Gitlab中。我只需要在Gitlab的项目设置中定义变量。

希望对别人有帮助!