Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use .env variables to set up appcenter on a react-native app

I started using MS appcenter in my bare react-native app, and during the set up they ask me to add two new files to the project:

  1. For IOS: AppCenter-Config.plist with
    <dict>
    <key>AppSecret</key>
    <string><MY_SECRET_KEY></string>
    </dict>
  1. And for Android I need to add a JSON file with the key:
 {
    "app_secret": "<MY_SECRET_KEY>"
 }

I'm already using a package in my app to handle .env files: https://github.com/luggit/react-native-config

Is there any way to use this package or another one, in order to get the APP_SECRET for the appcenter config files from a ENV variable?

I just don't want to keep these keys under version control.

like image 852
Victor Avatar asked Jan 06 '20 22:01

Victor


People also ask

Can I use variables in .env file?

The . env file lets you customize your individual working environment variables.

How do you use variables from .env file in react JS?

All you have to do is write or define your variable in your root folder. Then, you can import the variable in your component before calling it. While env variables are very useful in React Native, it is not advisable to use them to store sensitive information like API keys, authentication keys, or passwords.

How do I import a .ENV variable?

On the Projects page, select the project that you want to import environment variables to, and click Properties to open the Project Properties window. On the General page, click Environment. In the Environment Variables dialog, click Import from File.


1 Answers

Appcenter allows us to use build scripts, you can see more details here: https://docs.microsoft.com/en-us/appcenter/build/custom/scripts

The workaround I found to fix this was using a post-clone script. You need to create a bash script on the root folder of your app that will write the .env file using the environment variables.

First, create a new file called appcenter-post-clone.sh.

And after you can write your .env file with something like this:

#!/usr/bin/env bash

echo "Creating .env file"
cat > ./.env <<EOL
API_URL=${API_URL}
API_KEY=${API_KEY}
EOL
like image 137
Victor Avatar answered Oct 26 '22 18:10

Victor