Precedence of environment variables available to all branches vs to specific branch

Hi everyone!

In my build settings I need to have an environment variable set to a value for all branches except for the release branch, which uses a different value. Tried to do this by adding this in the Settings section:

  • MY_ENV_VAR, value1, Only available to the “branch123” branch
  • MY_ENV_VAR, value2, Available to all branches

In the build log for the “branch123” branch, I see:

Setting environment variables from repository settings
export MY_ENV_VAR=value1 export MY_ENV_VAR=value2

It is like the one available to all branches is overwriting the one specific for the branch. This was unexpected and seems not to be consistent.

So the question is: what is the precedence when defining the same variable in the Settings section multiple times for different -or all- branches?

Thanks!

I think the “precedence”, as it were, is to apply the env vars from the Settings page in the order they are defined, regardless of the scope.

So, if you have the “all-branches” MY_ENV_VAR at the top of the list in the Settings page, it would work as you wanted.

However, this is not what should happen. We’ll look into fixing it.

1 Like
2 Likes

Sorry to revive an old thread, but I did a search and I think it’s still the most relevant; I found this when I was dealing with a similar issue a few months ago and @BanzaiMan, the change you made did indeed help make this solvable. I’m wondering if it could be taken a step further though:

Use case:
I’ve got an env var set multiple times, first a value for all branches, then 3 more times for main, testing, and staging. The “all branches” item is at the top of the list because I added it first. But something’s changed and I need to change that variable for PR builds (but not main/testing/staging). I can’t edit the variable, so I have to delete it. When I add it again for all branches, it’s now below the other three. So builds on main/testing/staging will all inherit the new value. I have to then delete those 3 one at a time and re-add them so the order is maintained.

Some possible solutions:

  • Allow editing a value after it’s been set
  • Allow re-ordering of env vars
  • Automatically add “Available to all branches” variables as the first item in the set of a specific key, so that any branches with specific values set are guaranteed to get that value.

Again, there’s totally a workaround for this, but I wanted to at least raise it as a thing that’s mildly frustrating in the event others have this problem too and want to join in the ask for a solution.

Either way, thanks for at least making this possible back in April!

While web UI does not allow it, it is possible to update existing environment variables via an API call.

2 Likes

Ah, perfect, thanks!

Imprint