- Home /
Is there any way to set Environment Variables to Cloud Build?
Hi, folks.
I use Cloud Build to build my own project. And I want to load some confidential informations such like Access Token for external services from build scripts. To avoid committing them into source code, I'm looking for a way to refer Env Variables during build but I haven't got yet.
There doesn't seem to be any UIs where I can set them in Cloud Build web console.
Do anybody know any solutions for this? Thanks in advance.
I’ve actually been trying to figure out how to do this myself. Unity does support environment variables but only through the cloud build API and not the GUI at this time. You can find the API docs here https://build-api.cloud.unity3d.com/docs/1.0.0/index.html let me know if you figure out how to use the variables at build time. I’m still trying to wrap my head around all this myself.
Answer by DevinT7 · Jan 15, 2019 at 11:25 PM
Hey! So i Just went through this process myself.
Environment variables are supported by Unity Cloud Build but not through their GUI. Using them isn't the most straight forward process. To set and environment variable for a specific build config you need to use the Unity Cloud Build API . You can setup your variables directly through this UI, set your API key in the top left you can find your key Here.
Then just CTRL-F "Environment Variable".
You can find your ORGID, PROJECTID, and BUILDTARGETID as follows: If you have your config selected in the unity cloud build dashboard all your info will be in the URL https://developer.cloud.unity3d.com/build/orgs/{ORGID}/projects/{PROJECTID}/setup/buildtarget/{BUILDTARGETID}
Example: https://developer.cloud.unity3d.com/build/orgs/my-fake-org/projects/my-cool-project/setup/buildtarget/android-example-config/ Here my ORGID would be my-fake-org my PROJECTID would be my-cool-project and build target would be android-example-config
After setting your Environment Variables you can access them at build time via
var myVar = Environment.GetEnvironmentVariable("MY_ENV_VARIABLE");
In your pre-export method.
As a side note, if you have any methods you only want cloud build ever to look at and use then wrap them in
#if UNITY_CLOUD_BUILD
The best method I've found to use an environment variable or save data to a build is to just record it as a scriptable object which is created at build time and saved to Resources.
Instrument.AppDataContainer asset = ScriptableObject.CreateInstance<Instrument.AppDataContainer>();
asset.envVariable = myVar;
AssetDatabase.CreateAsset(asset, "Assets/Resources/ApplicationConfigData.asset");
AssetDatabase.SaveAssets();
Where AppDataContainer is a scriptable asset that just receives strings of data
Hopefully this helps, I went through a lot of rounds where I was close to pulling my hair out looking for documentation on this.
Your answer
Follow this Question
Related Questions
[Unity Cloud Build] Fatal Error! The file 'none' is corrupted! 0 Answers
iOS Unity Cloud Build failed when 'Target minimum iOS version' is set to 9 0 Answers
UnityCloudBuildProjectId not set in info.plist (iOS) 0 Answers
Unity Cloud Build bug at Visual Studio Team Services 1 Answer
Get Unity Cloud Build Index 0 Answers