Using environment variables with codemagic.yaml
How to use environment variables in scripts in codemagic.yaml
To access a variable during build time, add the $ symbol in front of its name. For example, you can access the value of API_TOKEN variable by referencing it as $API_TOKEN.
Setting environment variables at build time and accessing them across shell scripts
By default, if you define an environment variable inside your script, you can only use it within that particular script itself. However, you can make an environment variable available to any subsequent step of your workflow by writing it to the CM_ENV environment file.
Specifically, you can do this by writing a "KEY=value" pair to the CM_ENV environment file. CM_ENV can contain multiple environment variables separated by newlines.
The following commands show how to write variables to the CM_ENV file, depending on the instance type of the build machine you are using:
scripts:
- name: Save variable to ENV file
script: |
echo "KEY=value" >> $CM_ENV$KEY. scripts:
- name: Save variable to ENV file
script: |
echo "KEY=value" >> $CM_ENV$KEY. scripts:
- name: Save variable to ENV file
script: |
Add-Content -Path $env:CM_ENV -Value "KEY=value"$env:KEY.Setting a multiline environment variable
To add a multiline environment variable, you need to use << instead of an = to mark the end of the key in the key-value pair. In addition, set a delimiter to mark the start and the end of the variable.
In the following example, the DELIMITER keyword can be replaced by any word of your choice, however, make sure that the delimiter at the beginning and at the end match.
scripts:
- name: Save variable to ENV file
script: |
echo 'MULTILINE_VAR<<DELIMITER' >> $CM_ENV
echo 'line_one\nline_two' >> $CM_ENV
echo 'DELIMITER' >> $CM_ENVNote that the example is specific to Linux and macOS machines but the same principles apply when building on Windows.
Accessing environment variables from apps
Environment variables can also be accessed from within your apps. One of the great advantages is that you are able to use sensitive data such as API keys without having to store them in your repository.
The following examples show how to place your Google Maps API key into an Android or iOS application from an environment variable.
- Add your key as an environment variable with the name
MAPS_API_KEY - Reference the environment variable in the
build.gradle
defaultConfig {
// Other values set here
resValue "string", "maps_api_key", "$System.env.MAPS_API_KEY"
}<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/maps_api_key"
/>- Add your key as an environment variable with the name
MAPS_API_KEY - In the build step, add
--dart-defineto your build script
scripts:
- name: Flutter build ipa
script: |
flutter build ipa --release \
--dart-define=MAPS_API_KEY=$MAPS_API_KEY- Within your Flutter Application, use
String.fromEnvironmentto retrieve these variables in your Dart Code.
void main() {
final secret = String.fromEnvironment('MAPS_API_KEY');
print(secret);
}- Add your key as an environment variable with the name
MAPS_API_KEY - Reference the environment variable in the
Info.plist
<key>MAPS_API_KEY</key>
<string>$(MAPS_API_KEY)</string>- Reference the value from
Info.plistin theAppDelegate.swift
GMSServices.provideAPIKey(Bundle.main.object(forInfoDictionaryKey: "MAPS_API_KEY") as? String ?? "")- Add your key as an environment variable with the name
MAPS_API_KEY - Reference the environment variable in the
AppDelegate.mas in the following example.
[GMSServices provideAPIKey:[[NSProcessInfo processInfo] environment][@"MAPS_API_KEY"]];