Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 609
pkpk Avatar asked Oct 03 '17 12:10

pkpk


People also ask

What is the build environment for CodeBuild?

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.

How do I set environment variables in CodeBuild?

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.

Where should the Buildspec Yml file be placed in your code for CodeBuild to work properly?

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.

What is a build environment in AWS codebuild?

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.

What is a build environment and how does it work?

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.

How do I start a codebuild build from source?

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

How do I create a JSON file in AWS codecommit?

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.


2 Answers

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.

like image 170
Jose Avatar answered Nov 15 '22 03:11

Jose


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.

like image 34
Noel Llevares Avatar answered Nov 15 '22 04:11

Noel Llevares