Setting UI Environment variables in config

Lets say I create a project environment variable in circleci’s UI called USER with value Tom. In the config.yml, I would like to set the variable in the environment jobs section:

jobs:
  build:
    docker:
      - image: circleci/someimage
        environment:
          WORDPRESS_USER:       $USER

In the job, echo $WORDPRESS_USER will output $USER and not the value of USER (Tom) created in the UI. Is what I would like to do possible in circleci?

Hi @chestercopperpot

I just tested with the syntax you’re trying and some variations; “${USER}” etc. It seems values are interpreted literally. However, there is a way to get your desired outcome.

Apart from setting this variable through the UI as well, you can make use of Circle’s special BASH_ENV environment variable. BASH_ENV points to a file that’s sourced for every step in a build.

To accomplish what you’re trying to do through the config file, you can add this assignment WORDPRESS_USER="$USER" to BASH_ENV with an echo command andd an output redirect >> as follows:

echo WORDPRESS_USER="$USER" >> "${BASH_ENV}"

A full configuration might look as:

version: 2

jobs:
  build:
    docker:
      - image: circleci/node
    environment:
      USER: Tom
    steps:
      - run:
          name: Set custom environment variable
          command: echo WORDPRESS_USER="$USER" >> "${BASH_ENV}"

      - run:
          name: Test custom environment variable
          command: echo $WORDPRESS_USER # will print out Tom

Hope this helps.