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.

Note: It is required to use quotation marks with multi-line variables when you are referencing them in custom scripts.

Setting environment variables at build time

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
You can then reference the variable in subsequent parts of your workflow by using $KEY.
  scripts:
    - name: Save variable to ENV file
      script: | 
        echo "KEY=value" >> $CM_ENV
You can then reference the variable in subsequent parts of your workflow by using $KEY.
  scripts:
    - name: Save variable to ENV file
      script: | 
        Add-Content -Path $env:CM_ENV -Value "KEY=value"
You can then reference the variable in subsequent parts of your workflow by using $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_ENV

Note 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.

  1. Add your key as an environment variable with the name MAPS_API_KEY
  2. Reference the environment variable in the build.gradle
defaultConfig {
    // Other values set here
    resValue "string", "maps_api_key", "$System.env.MAPS_API_KEY"
}
3. Reference the value from `build.gradle` in the `AndroidManifest.xml`
<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/maps_api_key"
/>
  1. Add your key as an environment variable with the name MAPS_API_KEY
  2. Reference the environment variable in the Info.plist
<key>MAPS_API_KEY</key>
<string>$(MAPS_API_KEY)</string>
  1. Reference the value from Info.plist in the AppDelegate.swift
GMSServices.provideAPIKey(Bundle.main.object(forInfoDictionaryKey: "MAPS_API_KEY") as? String ?? "")
  1. Add your key as an environment variable with the name MAPS_API_KEY
  2. Reference the environment variable in the AppDelegate.m as in the following example.
[GMSServices provideAPIKey:[[NSProcessInfo processInfo] environment][@"MAPS_API_KEY"]];