appcenter-sdk-react-native icon indicating copy to clipboard operation
appcenter-sdk-react-native copied to clipboard

Setting AppSecret to be different with build variants

Open henrikra opened this issue 7 years ago • 15 comments

I have a situation where I am having separate apps on AppCenter but all of the apps use same repo. So these different apps are just white label versions of the same app.

Now I am facing problem where I would like to change AppSecret per ios build config. But since AppCenter-Config.plist is custom plist file I cant use user defined variables with them. (https://stackoverflow.com/questions/34067120/use-user-defined-build-settings-in-custom-plist-file)

If however this SDK would read the app secret from Info.plist I would be able to use user defined variables. Is there any reason why app secret has to be in own plist file? Is there drawback if it would be in Info.plist?

henrikra avatar Jan 16 '18 14:01 henrikra

@henrikra for a current work around you could add a pre-build script that replaces the AppCenter-Config.plist depending on the build. It will copy you old config for backup, then replace the current config with the one for your white label. I haven't tried this with AppCenter but used similar setups for white label apps or different stages (dev/prod).

Go to your project > select target > select build phases > New Run Script Phase (plus button) > Move it to before compiles (pre-build)

# Backing up original plist
cp AppCenter-Config.plist AppCenter-Config.plist.bak

if [ ${TARGET_NAME} = "WhiteLabel A" ]; then
echo "Setting AppCenter for WhiteLabel A"
cp AppCenter-Config-WhiteLabelA AppCenter-Config.plist

elif [ ${TARGET_NAME} = "WhiteLabel B" ]; then
echo "Setting AppCenter for WhiteLabel B"
cp AppCenter-Config-WhiteLabelB AppCenter-Config.plist

else
echo "Not specified"
fi

Even better you could save this as config_appcenter.sh and just call it in the prebuild for each target.

hammadzz avatar Apr 08 '18 18:04 hammadzz

@hammadzz Yeah we did something similar. Only down side is that you will get unwanted changes to git because you are basically modifying file that is tracked by git

henrikra avatar Apr 20 '18 08:04 henrikra

@henrikra you can make sure to undo the changes in the script by doing a post compile script that cleans up.

hammadzz avatar Apr 23 '18 19:04 hammadzz

Thanks for the above, works great for iOS. Also trying to set it up on Android, but it looks like here that Android looks for the first appcenter-config.json file it finds or always looks in the main folder, but I have a debug folder and a beta folder for the other variants of our Android app. Any ideas how to either update the appcenter-config.json on build like above, or to tell AppCenter where to find those specific variant files?

codybrouwers avatar Aug 31 '18 17:08 codybrouwers

Hi @CodyBrouwers,

App Center doesn't support specifying different build variants (we're tracking this as as feature request). Maybe you can add a script that copies the right appcenter-config.json into the main folder based on what variant you are building? You should be able to do this as a gradle task (check out the gradle docs for file operations), like this:

task copyAppCenterConfig(type: Copy) {
    // maybe also delete the appcenter-config.json in main first?
    from file("${buildDir}/path-to-your-beta-folder/appcenter-config.json")
    into file("${buildDir}/path-to-main-folder/appcenter-config.json")
}

and run this before build?

ElektrojungeAtWork avatar Sep 01 '18 00:09 ElektrojungeAtWork

@TroubleMakerBen that is a great idea, thanks so much for the quick response! I ended up doing this earlier today:

    buildTypes {
        debug {
            if (getCurrentBuildType() == "debug") {
                println "=========== Configuring appcenter-config.json for Development ==========="
                def appCenterSecret = "..."
                new File("$projectDir/src/main/assets/appcenter-config.json").text = "{ \"app_secret\": \"${appCenterSecret}\" }"
            }
        }
        ... // Same in the rest of my build types
    }

Using the getCurrentBuildType method from this gist.

Using a task like you suggested might be a better approach though so thanks a lot for the recommendation!

codybrouwers avatar Sep 01 '18 05:09 codybrouwers

this is our example:

if [ "$APPCENTER_BRANCH" == "master" ]
then
  echo "Building with prod config"
  cp "environments/config.prod.js" "src/services/config/index.js"
  cp "environments/AppCenter-Config.prod.plist" "ios/NordstromON/AppCenter-Config.plist"
  cp "environments/appcenter-config.prod.json" "android/app/src/main/assets/appcenter-config.json"
else
  echo "Building with nonprod config"
  cp "environments/config.nonprod.js" "src/services/config/index.js"
  cp "environments/AppCenter-Config.nonprod.plist" "ios/NordstromON/AppCenter-Config.plist"
  cp "environments/appcenter-config.nonprod.json" "android/app/src/main/assets/appcenter-config.json"
fi

jdnichollsc avatar Dec 26 '18 22:12 jdnichollsc

@CodyBrouwers and @ElektrojungeMS I was able to get around this issue without any code in the gradle file that might be helpful

we have several product flavors (client1, client2) and build types (qa, release), so I was able to create a directory for each build variant that needed a different AppCenter config like this...

/android/app/src/client1Qa/assets/appcenter-config.json /android/app/src/client1Release/assets/appcenter-config.json /android/app/src/client2Qa/assets/appcenter-config.json /android/app/src/client2Release/assets/appcenter-config.json

this allows you to have a default /android/app/src/main/assets/appcenter-config.json for any other build variant that doesn't need a specific api key.

tkiefhaber avatar Jan 08 '19 14:01 tkiefhaber

@tkiefhaber getting this error with your suggested solution - `Found more than one appcenter-config.json. How did you fixed this?

ashish-oyo avatar Sep 01 '19 12:09 ashish-oyo

You can have a appcenter-pre-build.sh in the root folder

#!/usr/bin/env bash

echo $CODE_PUSH_ANDROID_SECRET | base64 --decode > "$APPCENTER_SOURCE_DIRECTORY/android/app/src/main/assets/appcenter-config.json"
echo $CODE_PUSH_IOS_SECRET | base64 --decode > "$APPCENTER_SOURCE_DIRECTORY/ios/Shyn/AppCenter-Config.plist"

And for each environment, you can use a different file and base64 -w 0 appcenter-config.json and add as the env var.

dougg0k avatar Jul 09 '21 13:07 dougg0k

@ElektrojungeAtWork hi...just curious: did you guys implement the feature by now? the documentation doesnt mention anything.

sammysium avatar Sep 27 '21 20:09 sammysium

Hi @sammysium , could you please confirm that you're asking about migrating app secret from AppCenter-Config.plist to the default Info.plist file? It is not yet implemented if such, but thanks for bumping this thread, we will discuss it within the team.

DmitriyKirakosyan avatar Sep 28 '21 07:09 DmitriyKirakosyan

@DmitriyKirakosyan that would be nice, I am also not getting why you need a separate Plist file for storing just one key-value.

pavshr avatar Nov 04 '22 11:11 pavshr

on our side, for ios projects, we handled such problem with using a pre-built script, as @hammadzz mentioned, however, our approach for this sh is a little bit different. below you can find our approach.

first, we have 3 build schemes for both android and ios named as 'Release', 'DevTest', 'ProdTest'

on iOS project folder, we have created AppCenter folder along with Release, DevTest and ProdTest folders. place each appcenter-info.plist file to designated folders.

add APPCENTER_CONFIG user-defined string in your xcode -> build settings for each your schemes. we have 4 schemes, 3 build settings. debug, devtest, prodtest and release. set these fields in the way you want to configure your build schemes.

then add a build script from xcode build phases, with the following code piece:

#!/bin/sh
APPCENTER_INFO=${PROJECT_DIR}/AppCenter/${APPCENTER_CONFIG}/AppCenter-Config.plist

# Make sure  of AppCenter-Config.plist exists
echo "Looking for ${APPCENTER_INFO}"
if [ ! -f $APPCENTER_INFO ]
then
echo "No AppCenter-Config.plist found. Please ensure it's in the proper directory."
exit 1
fi

# Get a reference to the destination location for the AppCenter-Config.plist
PLIST_DESTINATION=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/AppCenter-Config.plist
echo "Will copy ${APPCENTER_INFO} to final destination: ${PLIST_DESTINATION}"

# Copy over the prod AppCenter-Config.plist for Release builds
echo "Using ${APPCENTER_INFO}"
cp "${APPCENTER_INFO}" "${PLIST_DESTINATION}"

AlkanV avatar Dec 04 '22 14:12 AlkanV

@DmitriyKirakosyan that would be nice, I am also not getting why you need a separate Plist file for storing just one key-value.

I also wonder the same thing, why the extra file for ios and android. If can be done in plist file and string.xml, i think it'd look a lot cleaner with more flexibility. Or give the ability to define on the fly like codepush would be nice too.

alan-la-chen-478 avatar Apr 04 '23 21:04 alan-la-chen-478