Environment Variables for Each Deployment Stage

Hello, I’m trying to set the same environment variable, for instance REACT_APP_GRAPHQL_ENDPOINT, with different values for each deployment stage: development, staging, production. Can I use project-level contexts for this? Does something similar exist?

Hi @picosam, welcome to the CircleCI community!

There are a few ways to utilize environment variables in a configuration file:

Do you mean you would like to use values for each deployment stage, which can be used across projects? If so, contexts will be your best bet in order to share this information organization-wide.

You can read more about the order of precedence and how variables are prioritized when processing your config.yml.

Thank you for your swift response @angelisa! No, I meant I’d like to declare the same variable for a single project (not cross-project) but with a different value for each stage — the way BitBucket does it really.

This way I can use the variable in a single script, but it would end up building appropriately as per the stage I’m deploying to. I hope that makes sense?

We’re running into this issue as well, curious to see if there are any updates here.

Building a React app locally I use the same build script with a different .env file based on the ultimate deployment target, for example the .env.development and .env.production files each have an API_KEY variable to use with endpoint calls to the corresponding API deployment. Is there any way in CircleCI to go about this other than creating multiple variables like API_KEY_DEV and API_KEY_PROD and then manually setting API_KEY to the desired one in the job? Makes it harder to re-use the build script which is otherwise the same.