Unitech / Unitech/pm2

ecosystem.config.js environment variables unexpected behaviour

Open
#5,152 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

What's going wrong?

When launching an application using the ecosystem.config.js we can specify which environments variable to load as documented here. However the behaviour I'm experiencing is not what I expected.

What seems to happens is that the pm2 process scour through the whole array of env_something or at least the initial env, and instantiate all environment variables, stopping only when reaching the one selected with the flag --env <environment>.

So in this case for example:

module.exports = {
  apps: [{
    name: "app",
    script: "./app.js",
    env: {
      NODE_ENV: "development"
      DEBUG_MODE: "true"
    },
    env_test: {
      NODE_ENV: "test",
    },
    env_staging: {
      NODE_ENV: "staging",
    },
    env_production: {
      NODE_ENV: "production",
    }
  }]
}

If I launch the process with this command pm2 ecosystem.config.js --env production I will end up with this result when running pm2 env <process id>:

...
NODE_ENV: production
DEBUG_MODE: true
...

Even if I didn't have DEBUG_MODE in the production environment.

How could we reproduce this issue?

Create a simple node project and use even just the ecosystem example I posted above.

Supporting information

--- PM2 report ----------------------------------------------------------------
Date                 : Fri Aug 20 2021 12:14:56 GMT+0100 (British Summer Time)
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 5.1.1
node version         : 12.2.0
node path            : not found
argv                 : /usr/local/bin/node,/usr/local/bin/pm2-runtime,ecosystem.config.js,--env,production
argv0                : node
user                 : undefined
uid                  : 0
gid                  : 0
uptime               : 12min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 5.1.1
node version         : 12.2.0
node path            : /usr/local/bin/pm2
argv                 : /usr/local/bin/node,/usr/local/bin/pm2,report
argv0                : node
user                 : undefined
uid                  : 0
gid                  : 0
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : linux
type                 : Linux
cpus                 : Intel(R) Xeon(R) CPU @ 2.20GHz
cpus nb              : 4
freemem              : 119861248
totalmem             : 4127956992
home                 : /root
===============================================================================
--- PM2 list -----------------------------------------------
┌─────┬─────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name    │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼─────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ scar    │ default     │ 1.0.0   │ fork    │ 17       │ 12m    │ 0    │ online    │ 0%       │ 112.2mb  │ root     │ enabled  │
└─────┴─────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------

This is running on a kubernetes cluster, inside a docker container. I can provide the dockerfile as well:

# base image
FROM node:12.2.0

ENV TZ Europe/London

RUN npm install pm2 -g

WORKDIR /usr/src/node-app
COPY . .
RUN npm ci
RUN npm install --no-progress --loglevel=error

EXPOSE 4010

ENTRYPOINT ["pm2-runtime", "ecosystem.config.js", "--env", "production"]

If this can be solved relatively easy, I'm willing to work on it myself provided I get some pointers as where I should start looking :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ecosystem.config.js example and reproduce the behavior using pm2-runtime with --env production, then inspect the environment shown by pm2 env . Compare the selected production variables with the base env block, and consider the documented environment-variable behavior linked in the issue. Done means variables from unselected environments are not present in the production process.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli, devops
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.