18

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.

Victor
  • 5,043
  • 3
  • 41
  • 55
  • Is this what you want to do https://github.com/luggit/react-native-config#native-usage Config variables are available in java class via `BuildConfig` – Totoro Jan 07 '20 at 00:01
  • But the config file for Android is a JSON file, not a Java class. And the same for IOS, the file is a .plist. How can I apply the ENV variable there? – Victor Jan 07 '20 at 01:58
  • @Victor have you found a solution? – Jacek Rojek Jan 22 '21 at 03:03
  • @JacekRojek yes, I just posted an answer, hope it helps you – Victor Jan 22 '21 at 12:21

3 Answers3

8

Appcenter allows us to use build scripts, you can see more details here: https://learn.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
Victor
  • 5,043
  • 3
  • 41
  • 55
  • 2
    We have used a similar [solution](https://blog.usejournal.com/react-native-config-and-appcenter-environment-variables-a1a3492ca6a0) in our projects. But this seems very good to me too. Deserves more upvotes imo. – Kevin Amiranoff Feb 18 '21 at 21:29
  • Aren't you appending the same lines over and over in your .env file each time you run the bash file? – chenop Jun 14 '21 at 13:25
  • Worked for Android - didnt worked for IOS --> I've opened a ticket to App Center support – chenop Nov 29 '21 at 06:50
3

Found this - The app secret (name is a bit confusing) is only for checking in-app updates - its seems you cannot trigger release with it.
So I think it is unnecessary to move the secret to the .env file.

chenop
  • 4,743
  • 4
  • 41
  • 65
  • Thanks for the link very useful, I was actually having security concerns but the answer in the link cleared it. Thanks again. – abdou-tech Feb 16 '23 at 13:51
1

as mention

https://learn.microsoft.com/en-us/appcenter/sdk/getting-started/react-native

Delete Appcenter-config.json both android and ios and

follow

iOS :

3.1.1.2 Setting AppSecret Option 2: Configuring in code

AppDelegate.mm

#import "RNCConfig.h"
#import <AppCenterReactNativeShared/AppCenterReactNativeShared.h>
#import <AppCenter/MSACAppCenter.h>
#import <AppCenterReactNative.h>
#import <AppCenterReactNativeAnalytics.h>
#import <AppCenterReactNativeCrashes.h>

   
NSString *appSecret = [RNCConfig envFor:@"APPCENTER_IOS_KEY"][AppCenterReactNativeShared setStartAutomatically:YES];
[AppCenterReactNativeShared setAppSecret: appSecret];
[AppCenterReactNative register];
[AppCenterReactNativeAnalytics registerWithInitiallyEnabled:true];
[AppCenterReactNativeCrashes registerWithAutomaticProcessing];

Android:

3.1.2.2 Setting AppSecret Option 2: Configuring in code

  @Override
  public void onCreate() {
    AppCenter.start(this, BuildConfig.APPCENTER_ANDROID_KEY, Analytics.class, Crashes.class);
  }
Zafer ATLI
  • 41
  • 2