Search code examples
node.jsbashcirclecicircleci-2.0

Circleci create environment variable from existing env var


In the circle ci dashboard I set enviornment variables like DEV_USEFUl_API_KEY, PROD_USEFUL_API_KEY. Then in my circleci config file (v2.1) I do this

- run:
    name: Run Tests
    command: |
      if [ "${CIRCLE_BRANCH}" == "master" ]; then
        echo 'export FIREBASE_API_KEY=${PROD_FIREBASE_API_KEY}' >> $BASH_ENV
      elif [[ "${CIRCLE_BRANCH}" == "develop" ]]; then
        echo 'export FIREBASE_API_KEY=${DEV_FIREBASE_API_KEY}' >> $BASH_ENV
      fi
      yarn test

The idea been when my tests are run, the environment variable will be read. I have printed out within my node application process.env and I can see the variables PROD_FIREBASE_API_KEY, DEV_FIREBASE_API_KEY are within the environment, however there is no FIREBASE_API_KEY as intended.

I have tried chaning the syntax of the command to:

echo 'export FIREBASE_API_KEY=$PROD_FIREBASE_API_KEY' >> $BASH_ENV

and also

echo 'export FIREBASE_API_KEY="$PROD_FIREBASE_API_KEY"' >> $BASH_ENV

However its not making any difference, the variable FIREBASE_API_KEY is not set, can anyone please advise? Thanks.


Solution

  • The solution was to put yarn test in another run step, as its necessary for the bash profile to be reloaded after setting new env vars (which happens at the beginning of every run).

          - run:
              name: Configure Environment Variables
              command: |
                if [ "${CIRCLE_BRANCH}" == "master" ]; then
                  # Set env variables 
                elif [[ "${CIRCLE_BRANCH}" == "develop" ]]; then
                  # Set env variables 
                fi
          - run:
              name: Run Tests
              command: |
                yarn test