Different environment variables for different branches

Hi,

I’m trying to set different values for environment variables on CircleCI according to the current $CIRCLE_BRANCH. I tried setting two different values on CircleCI settings and exporting them accordingly on the deployment phase, but that doesn’t work:

deployment:
  release:
    branch: master
    commands:
      ...
      - export API_URL=$RELEASE_API_URL; npm run build
      ...
  staging:
    branch: develop
    commands:
      ...
      - export API_URL=$STAGING_API_URL; npm run build
      ...

How could I achieve that?

Thanks in advance.

2 Likes

It seems like your example should work, but an alternative to try might be to run a script to set API_URL before running your build step.

Something like

#!/usr/bin/env node

process.env.API_URL = (process.env.CIRCLE_BRANCH === 'master')
    ? process.env.RELEASE_API_URL
    : process.env.STAGING_API_URL;

or a bash equivalent, if you prefer. We typically have something like a prebuild.sh or prebuild.js script that we run as a first step so that we can handle any setup logic we need without having a giant config.yml file.