6

I source my environment specific variables from .env file (I'm using dotenv package). This file is not version controlled.

Using code pipeline stage codebuild how do I create this .env file and its contents ?

I'm thinking, I have to use buildspec to create and add content to the .env file, but have no idea how ?

Thanks

2 Answers 2

9

So this is actually a simple script, taking the second option of the first answer you can add the following to your buildspec.yml:

phases:
  pre_build:
    commands:
      - printenv > .env

the shell script printenv > .env is all that is needed to output all of your process env to a file.

Sign up to request clarification or add additional context in comments.

Comments

2

My suggestion would be to get rid of dotenv and use real environment variables.

The problem with dotenv is that it simply mocks process.env inside your node process. It does not really modify environment variables for you. They are not available outside of your node process.

I would use direnv instead and replace your .env files with .envrc for local development.

For CI and production environments, the environment variables are now defined in your CodeBuild project so .envrc files are not needed.

Please see this related answer for more information from another angle.


If you wish to continue using dotenv, you have several options:

  • Store the .env file in S3 and retrieve it during the build process.
  • Construct the .env file using shell scripts and environment variables defined on the CodeBuild project.

I'm sure there are others but they are all hacky for me.

2 Comments

Thanks for the suggestion, how about storing env information in buildspec under env parameter-store ?
Yes, that's exactly what the linked related answer suggests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.