I have an app deployed in appcenter.ms that has some "secret" fields. I added them in the "Environment variables" section like MYSTRING_STR value
and I'm trying to read it in a Xamarin.Forms project like var myStr = Environment.GetEnvironmentVariable("MYSTRING_STR");
but no value is retured. What's the proper way to read them in a release build?
I was quite stupid. Of course it's not going to work because the app doesn't run on the system it's compiled so the Environment Variables are not going to get copied. I'm leaving this here for anyone who might need this in the future. Tl;Dr, I have my app on github and App Center just compiles from there. This is how I managed to get "secret" fields on there without leaking information on github: You'll need the Mobile.BuildTools
nuget package to be installed in your project. Afterwards, you create a file called secrets.json
in the root of your project with the information you need. I created one locally with placeholder values like:
{
"String1": "%VALUE1%",
"String2": "%VALUE2%"
}
Because it's a json, it won't read the values but it's ok. We only want it tracked on github for the next part. After commiting it to the repo I add it to .gitignore and remove the cache for it so it won't get tracked when we put real values in it. When you hit compile, Mobile.BuildTools will automatically generate a class called Secrets
. You can access the variables in code with Secrets.String1
etc, so you just use those where you need them. After that, you set the Environment Variables as usual in App Center, then you'll need a build script to write them to the json file. In the folder <MyProject>.Android
(or iOS, or both if you're supporting multiple platforms), I created the script called appcenter-post-clone.sh
with the following content:
#!/usr/bin/env bash
npm install node-jq --save
jq -n \
--arg str1 "$STRING1" \
--arg str2 "$STRING2" \
'{String1: $str1, String2: $str2}' > $APPCENTER_SOURCE_DIRECTORY/<Project>/secrets.json
After that you're done. Sorry for the long post but I couldn't really find any source explaining this so I'm just leaving it here in case anyone else is as lost as I was.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.