Environment Variables

You might have API keys and other secrets that your app needs to consume — either at run time or during a build. However, you might not want to check these secrets into your source code to make them available to your app.

In order to securely propagate your credentials to your build and app, buddybuild provides two sets of variables that you can define:

  1. Environment Variables (made available to your build time scripts, more details at Custom Build Steps)

  2. Device Variables (available your App at run time via the buddybuild SDK)

  3. Secure Files (files made available to your build time scripts)

You can create default Environment Variables, which are available to all branches, or branch-specific environment variables which are only available for a specific branch.

Important

Any environment variables set by buddybuild are available to all phases of a build.

However, environment variables set by scripts invoked by your build tool, tests, or other build-related operations, are only available while those scripts are executing.

For example, the environment variable BUILT_PRODUCTS_DIR is only available while Xcode is running, specifically when a Run Script is executing.

If you need to persist environment variables across script executions, you have a couple of options:

  • specify the environment variables in buddybuild — buddybuild takes care of making those environment variables for you.

  • write the definitions of the environment variables to a file, and read those definitions every time your scripts execute.

Creating default environment variables

Start by clicking on App Settings.

Builds   Settings.png

In the left navigation, select Build settings, then the Environment tab.

Settings   Environment variables   menu.png

In the Environment variables row, click Configure.

Settings   Environment variables   configure.png

Enter a name and value to create a default environment variable. Your environment variable is now ready to be consumed by your app at build time.

The Environment Variables screen

Create environment variables for a specific branch

You can also create environment variables for a specific branch by creating a branch-specific override.

Under Override build configuration, click Add a branch and select the branch you would like to create an environment variable for.

Settings   Variants   Branch specific 1.png

Select Environment variables from the dropdown and click Configure.

Settings   Environment variables   Branch   configure.png

Enter a name and value to create your branch-specific environment variable.

The Environment Variables screen for a specific branch

Consume the variable in your build

Your environment variables will be automatically consumed by tools that are expecting them.

2a. Consume in your custom build scripts.

If you would like to access them in your custom build scripts, use the bash variable expansion syntax.

./Example.framework/run $Crashlytics_Token

2b. Consume in Android build.gradle file.

If you are building an Android app with Gradle (Android Studio), you can also access them 2 ways inside your build.gradle file.

You can access them via System.getenv(), like this:

buildscript {
  repositories {
    jcenter()
    maven {
      credentials {
        username System.getenv("ARTIFACTORY_USERNAME")
        password System.getenv("ARTIFACTORY_PASSWORD")
      }
      url System.getenv("ARTIFACTORY_URL")
    }
  }
  ...
}

We also put these variables inside gradle.properties file, so you can access them directly like:

buildscript {
  repositories {
    jcenter()
    maven {
      credentials {
        username ARTIFACTORY_USERNAME
        password ARTIFACTORY_PASSWORD
      }
      url ARTIFACTORY_URL
    }
  }
  ...
}

That’s it! If you want to consume your variables from within your app, follow the Device Variables guide.

results matching ""

    No results matching ""