- 
        Couldn't load subscription status. 
- Fork 2.7k
Fix #3192: Reloading With --update-env Option #5881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            mkalygin
  wants to merge
  5
  commits into
  Unitech:development
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
mkalygin:fix/update-env
  
      
      
   
  
    
  
  
  
 
  
      
    base: development
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      4e5fd17
              
                test: add example with dotenvx to reproduce #3192 --update-env bug
              
              
                mkalygin 38d7782
              
                test: complete dotenvx example and add docs
              
              
                mkalygin 83ea160
              
                test: add unit tests for --update-env option
              
              
                mkalygin 42341e7
              
                test: add e2e tests for --update-env option
              
              
                mkalygin 304fc5e
              
                fix: reloading with --update-env option #3192
              
              
                mkalygin File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| DE=dotenv_initial | ||
| SH_DE=dotenv_initial | ||
| DE_PM=dotenv_initial | ||
| SH_DE_PM=dotenv_initial | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| DE=dotenv_updated | ||
| SH_DE=dotenv_updated | ||
| DE_PM=dotenv_updated | ||
| SH_DE_PM=dotenv_updated | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| This is an example of using [dotenvx](https://dotenvx.com/) with pm2. | ||
|  | ||
| It demonstrates how to update the environment for forked and clustered apps using dotenvx. | ||
|  | ||
| In this example, the environment variables come from three sources and are applied in the following order (dotenv files take precedence because of the `--overload` option used): | ||
| - shell environment | ||
| - dotenv files `.env.initial` or `.env.updated`. | ||
| - pm2 ecosystem config `ecosystem.config.cjs` | ||
|  | ||
| See `package.json` scripts for the actual commands. | ||
|  | ||
| ```bash | ||
| # install dotenvx | ||
| npm install | ||
| # start apps with `initial` environment and `.env.initial` dotenv file | ||
| npm run start:json | ||
| # reload apps with `updated` environment and `.env.updated` dotenv file | ||
| npm run reload:json | ||
| # delete apps | ||
| npm run delete:json | ||
| ``` | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| module.exports = { | ||
| apps: [ | ||
| { | ||
| name: 'forked_app', | ||
| script: './index.js', | ||
| env_initial: { | ||
| PORT: 8001, | ||
| PM: 'pm2_initial', | ||
| SH_PM: 'pm2_initial', | ||
| DE_PM: 'pm2_initial', | ||
| SH_DE_PM: 'pm2_initial', | ||
| }, | ||
| env_updated: { | ||
| PORT: 8001, | ||
| PM: 'pm2_updated', | ||
| SH_PM: 'pm2_updated', | ||
| DE_PM: 'pm2_updated', | ||
| SH_DE_PM: 'pm2_updated', | ||
| }, | ||
| }, | ||
| { | ||
| name: 'clustered_app', | ||
| script: './index.js', | ||
| instances: 2, | ||
| exec_mode: 'cluster', | ||
| env_initial: { | ||
| PORT: 8002, | ||
| PM: 'pm2_initial', | ||
| SH_PM: 'pm2_initial', | ||
| DE_PM: 'pm2_initial', | ||
| SH_DE_PM: 'pm2_initial', | ||
| }, | ||
| env_updated: { | ||
| PORT: 8002, | ||
| PM: 'pm2_updated', | ||
| SH_PM: 'pm2_updated', | ||
| DE_PM: 'pm2_updated', | ||
| SH_DE_PM: 'pm2_updated', | ||
| }, | ||
| }, | ||
| ], | ||
| }; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import http from 'http'; | ||
|  | ||
| const { | ||
| PORT, | ||
| SH, | ||
| DE, | ||
| PM, | ||
| SH_DE, | ||
| SH_PM, | ||
| DE_PM, | ||
| SH_DE_PM, | ||
| } = process.env; | ||
|  | ||
| http.createServer((req, res) => { | ||
| res.writeHead(200); | ||
| res.end(JSON.stringify({ | ||
| SH, | ||
| DE, | ||
| PM, | ||
| SH_DE, | ||
| SH_PM, | ||
| DE_PM, | ||
| SH_DE_PM, | ||
| }, null, 2)); | ||
| }).listen(PORT, '0.0.0.0', () => { | ||
| console.log(`App listening on port ${PORT}`); | ||
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "name": "dotenvx-pm2", | ||
| "version": "1.0.0", | ||
| "description": "Example usage of dotenvx with pm2", | ||
| "main": "index.js", | ||
| "type": "module", | ||
| "author": "Michael Kalygin", | ||
| "license": "MIT", | ||
| "scripts": { | ||
| "start:json": "SH=shell_initial SH_DE=shell_initial SH_PM=shell_initial SH_DE_PM=shell_initial dotenvx run --env-file .env.initial --overload -- ../../bin/pm2 start ecosystem.config.cjs --env initial", | ||
| "reload:json": "SH=shell_updated SH_DE=shell_updated SH_PM=shell_updated SH_DE_PM=shell_updated dotenvx run --env-file .env.updated --overload -- ../../bin/pm2 reload ecosystem.config.cjs --env updated --update-env", | ||
| "delete:json": "../../bin/pm2 delete ecosystem.config.cjs", | ||
| "test:json": "../../bin/pm2 update && npm run delete:json && npm run start:json && npm run reload:json" | ||
| }, | ||
| "dependencies": { | ||
| "@dotenvx/dotenvx": "^1.10.2" | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              Empty file.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| module.exports = { | ||
| apps: [ | ||
| { | ||
| name: 'update_env_app', | ||
| script: './update-env.js', | ||
| instances: 2, | ||
| exec_mode: 'cluster', | ||
| out_file: 'out-env.log', | ||
| merge_logs: true, | ||
| env_initial: { | ||
| NODE_ENV: 'test', | ||
| PM: 'pm2_initial', | ||
| SH_PM: 'pm2_initial', | ||
| }, | ||
| env_updated: { | ||
| NODE_ENV: 'test', | ||
| PM: 'pm2_updated', | ||
| SH_PM: 'pm2_updated', | ||
| }, | ||
| }, | ||
| ], | ||
| }; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| setInterval(() => { | ||
| const { SH, PM, SH_PM } = process.env; | ||
|  | ||
| console.log(`SH=${SH} PM=${PM} SH_PM=${SH_PM}`); | ||
| }, 100); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              Empty file.
          
    
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add an
proc.pm2_env.env.updateEnvcheck here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK no, because the env won't be extended with the new variables here:
pm2/lib/API.js
Line 1362 in 304fc5e