How to get environment variables?

I am storing certain sensitive data in environment variables in CircleCI’s project settings. How do I add them to my circle.yml file? I tried the following, but it doesn’t work:

machine:
  python:
    version: 3.5.1
  environment:
    CLIENT_ID: $CLIENT_ID
    CLIENT_SECRET: $CLIENT_SECRET

Is this how I’m supposed to define environment variables so that CircleCI gets them from the project settings?

Those last two lines aren’t needed here. You’d just use $CLIENT_ID or ${CLIENT_ID} in circle.yml where it’s needed. The’d also be available in scripts called via circle.yml.

This is assuming that $CLIENT_ID and $CLIENT_SECRET were configured as Environment Variables via CircleCI’s UI or API.

These variables are needed to run my tests. I have defined them in our project settings through CircleCI’s UI. I tried defining those variables in my circle.yml file because my tests cannot find those variables. Circle CI’s build page says that it is exporting those variables:

Exporting CLIENT_ID
Exporting CLIENT_SECRET

Why can’t my tests find them?

1 Like

Hey aderhg, Did you ever solve this issue? I think I am running into the same thing. My Mocha tests can’t read ENV variables I have set through the Circle CI UI.

Some questions:

Mocha is running as the same user correct?
Is this running within Docker?
This is about CircleCI 1.0?
Are you using the right capitalization for the variables?
Are you storing non-ASCII characters in the variables?
Also, maybe this? https://stackoverflow.com/questions/44598333/referencing-environment-variables-for-mocha-tests

1 Like

Hi!

Thanks for the reply.

  • I believe Mocha is running as the same user. How can I check this? I have done nothing that would lead me to believe this is not the case
  • This is not running within Docker
  • I think this is CircleCI 2.0
  • I am using the correct capitalization for the variables
  • I am not storing non-ASCII characters in the variables

I checked the link you provided, but am still confused.

In my app.js I have this:

if (process.env.NODE_ENV !== 'production') {
  require('dotenv').config();
}

I guess my question is why when in my test:

const authToken = process.env.AUTH_TOKEN

process.env.AUTH_TOKEN is not found when run by CircleCI?

Do these env variables need to be imported via the circle.yml file first and then retrieved from there?
Would I need to do something like this in my circle.yml file:

machine:
  environment:
    AUTH_TOKEN: $AUTH_TOKEN
    TEST_USER_NAME: $TEST_USER_NAME
    TEST_PASSWORD: $TEST_PASSWORD

Please forgive my confusion and thank you again.

2 Likes

Hi, did you ever fingure this out?

1 Like