How to prevent loading environment variables from env.yml during CD

I found this really useful article on using environment variables with Serverless, and it matches the pattern I’d like to use.

I want to be able to store my environment variables in a env.yml file locally, and not check those in to git.

Those variables are then used with serverless-offline, and when deploying to my staging environment.

However, I’d also like to deploy to production directly from Circle as a deploy step.

In the above case, I’ve already added the production environment variables on Circle. Those variables are already available in my code.

How can I prevent my serverless.yml file from attempting to load the env.yml file in this step?

As far as I can see, it isn’t possible to use conditionals in your serverless.yml. Otherwise, I could specify that the file is loaded if the CIRCLECI environment variable is not set.

Any help much appreciated!

@lewisblackwood I’m glad you found the article helpful. :slight_smile:

I don’t use CircleCI but I assume it’s just setting standard environment variables right? Could you add a stage in env.yml for the CI that sets the environment variables from ${env:SOME_VAR}?

For example:

ci:
  SOME_VAR: ${env:SOME_VAR}

Currently you can’t conditionally include the file but this would allow you to pickup the values from the environment variables set inside CircleCI.

Hey @buggy, I did! Thanks for writing it.

It is just standard environment variables on Circle.

The issue is that it’s linked to the GitHub repository for my project, and my env.yml isn’t checked in. So Circle wouldn’t be able to load my env.yml.

I think the best option is probably to follow your suggestion, but use a different file for each stage. I can load env-circle.yml, which would just reference SOME_VAR: ${env:SOME_VAR} as you suggest.

Thanks for the reply! I’ll try out the above and follow up here if there are any issues.

1 Like

Looking at the Circle CI docs I think a better solution would be to commit an env-ci.yml then use a step inside your .circleci/config.yml to copy this to env.yml before the build starts.

1 Like

That’s a much better solution than mine @buggy :grinning:

I’ve just tried that and it works great. Thanks for all your help on this.