Environment Variables are set (processed) in a specific order. An environment variable processed before another one can’t override the second one, but the second one can use the first one.
If you use the Build Trigger API and you specify one-off environment variables as parameters of the build, those will be processed after the Secret Env Vars, but before App Env Vars.
This means that simply specifying the same environment variable this way which is also defined in the build configuration (bitrise.yml
), e.g. as an App Env Var, you won’t be able to override it, the value of the App Env Var will be used as it will override the value you specify through the API.
For more technical information about the environment variable processing order see this DevCenter article.
The solution
So, what if you want to override a variable which is defined in the build configuration? The solution is simple: you have to override it at a later stage!
Probably the easiest solution is to do it with a build step (e.g. with a Script
step). Steps and step outputs are processed after App Env Vars and Workflow Env Vars, so with this solution you can reliably override any other environment variable.
The key is to specify an environment variable through the API which is not defined in your build configuration, and then “copy” the value of this env var to the main one, overriding it. For example, if you want to override the environment variable BITRISE_SCHEME
, you should specify e.g. an API_BITRISE_SCHEME
env var, and then copy the value of API_BITRISE_SCHEME
to BITRISE_SCHEME
.
Using a Script step
Any step or script can expose environment variables for subsequent steps, using the Bitrise CLI built in envman utility.
If you want to override BITRISE_SCHEME
with the value of API_BITRISE_SCHEME
(which is not defined in your build configuration, but might be specified through the Bitrise Build Trigger API), the script can be as simple as:
#!/bin/bash
set -ex
if [ ! -z "$API_BITRISE_SCHEME" ] ; then
envman add --key BITRISE_SCHEME --value "$API_BITRISE_SCHEME"
fi
This of course have to run before you’d use the value of BITRISE_SCHEME
. Also please note that BITRISE_SCHEME
environment variable’s new value would only be available in the next steps and not in the step that contains the envman command.
A very minimal bitrise.yml
for demonstration:
format_version: 1.3.1
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
app:
envs:
- BITRISE_SCHEME: SchemeFromAppEnv
workflows:
primary:
steps:
- script:
title: Copy the value of API_BITRISE_SCHEME into BITRISE_SCHEME if defined
inputs:
- content: |
#!/bin/bash
set -ex
if [ ! -z "$API_BITRISE_SCHEME" ] ; then
envman add --key BITRISE_SCHEME --value "$API_BITRISE_SCHEME"
fi
- script:
inputs:
- content: |
#!/bin/bash
echo "BITRISE_SCHEME: ${BITRISE_SCHEME}"