Search code examples
amazon-web-servicesaws-codepipelineaws-codebuild

How to create an environment file specific for build system using aws code build


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


Solution

  • 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.