Remi
Remi

Reputation: 5367

How to set environment variable in Circle CI to use in application as process.env.FOO

For example I've setup name: FOO value: 'bar'.

I've validated that the key value works. Because what does work is:

jobs:
  build:
    docker: 
      - image: circleci/node:10.17.0
    steps:
      - run: |
          node something $FOO

However, the following does not work:

Now when I deploy and try to use it in my application it returns undefined:

console.log(process.env.FOO); // returns undefined

I tried setting it under the 'environment' key in the config.yml file:

jobs:
  build:
    docker: 
      - image: circleci/node:10.17.0
      environment:
        - FOO # note, don't use $FOO
    steps:
      - run: |
        node something $FOO
        ssh $MACHINE -- 'cd /home/ && docker build -t myApp . docker restart myApp'

But still no change.

Should I perhaps pass the variables to the build script in the ssh command?

Any ideas?


update based on Delena's tip

Kept ./circle-ci/config.yml as:

jobs:
  build:
    docker: 
      - image: circleci/node:10.17.0
      environment:
        FOO: $FOO

Then in the docker-compose file:

myApp:
  environment:
    - FOO

Will accept the answer when the build is green

Upvotes: 2

Views: 1481

Answers (1)

D Malan
D Malan

Reputation: 11484

It looks like you're trying to access the environment variable from an app that runs in a Docker container, but you're not setting the environment variable in the container.

If that's the case, you can check out How to set an environment variable in a running docker container, but it looks like you'll have to stop the container and restart it again with the environment variable.

You could do something like:

ssh $MACHINE -- 'cd /home/ && docker build -t myApp && docker stop myApp && docker run -e "FOO=$FOO"'

Also check out the ENV (environment variables) section in the docker run docs.

Upvotes: 1

Related Questions