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
A build environment represents a combination of operating system, programming language runtime, and tools that CodeBuild uses to run a build. For information about how a build environment works, see How CodeBuild works. A build environment contains a Docker image.
Choose the icon to edit your CodeBuild action. On the Edit action page, under Environment variables, enter the following: In Name, enter a name for your environment variable. In Value, enter the variable syntax for your pipeline output variable, which includes the namespace assigned to your source action.
If you include a buildspec as part of the source code, by default, the buildspec file must be named buildspec. yml and placed in the root of your source directory.
When you call AWS CodeBuild to run a build, you must provide information about the build environment. A build environment represents a combination of operating system, programming language runtime, and tools that CodeBuild uses to run a build. For information about how a build environment works, see How CodeBuild works.
A build environment represents a combination of operating system, programming language runtime, and tools that CodeBuild uses to run a build. For information about how a build environment works, see How CodeBuild works. A build environment contains a Docker image. For information, see the Docker glossary on the Docker Docs website.
To start running a build, see Run a build (AWS CLI) . If your source code is stored in a GitHub repository, and you want CodeBuild to rebuild the source code every time a code change is pushed to the repository, see Start running builds automatically (AWS CLI) .
Using cb-centos-project.json as a reference, create the input JSON file for the CLI command. This project uses an AWS CodeCommit repository named codebuild-multispec and a file named buildspec-rpm.yml as the build specification file.
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.
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:
.env
file in S3 and retrieve it during the build process..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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With