2

I am working on a NodeJS application which is a containerized application. I use Jenkins to build and deploy it. I have an environment .env file and along with this, also have .env files based on environments.

For example: .env.DEV, .env.SQA, .env.STG and .env.PROD.

Each file has different values for the variables based on environments.

Project Structure with all env files

When I am deploying my application, it always fetches variables from the .env file instead of specific environment file i.e. .env.DEV (if deploying on DEV server).

How do we use specific environment file while doing the deployment on Jenkins?

Note: I followed this great content on dotenv library but I didn't find anything helpful for my use-case. I did some research on the web, but didn't find much on this.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ram
  • 3,887
  • 4
  • 27
  • 49
  • 1
    Have you tried to use something like `require('dotenv').config({ path: .env.${ENVIRONMENT_TO_READ}`})` and seting the ENVIRONMENT_TO_READ when running the container – Woohaik Jan 24 '23 at 11:52
  • Are you talking about Docker environment variables or Node environment variables? Are your environment files inside the image or outside? – Hans Kilian Jan 24 '23 at 13:02
  • Does this answer your question? [Toggle between multiple .env files like .env.development with node.js](https://stackoverflow.com/questions/55406055/toggle-between-multiple-env-files-like-env-development-with-node-js) – Fcmam5 Feb 26 '23 at 23:15

1 Answers1

0

You can use dotenv-flow which does exactly this, given the value of NODE_ENV environment value it will load the expected environment.

You also will need to make sure that the container receives the proper environment values from jenkins, this might help.

Sombriks
  • 3,370
  • 4
  • 34
  • 54