Skip to content

Commit 4fc3339

Browse files
thehabesclaude
andauthored
Fix PM2 cluster mode environment variable loading (#379)
Problem: - When running with `pm2 start` in cluster mode, Node.js 22's --env-file flag was not being passed - This caused all process.env variables to be undefined, breaking database connections and app configuration - The issue only occurred in PM2 deployments; npm start worked correctly Solution: - Created ecosystem.config.js to configure PM2 with proper node_args - Added --env-file=config.env --env-file=.env flags to node_args - Updated CI/CD workflows (cd_dev.yaml and cd_prod.yaml) to use ecosystem config - Configured separate development and production environments Changes: - NEW: ecosystem.config.js - PM2 configuration with cluster mode and env loading - UPDATED: .github/workflows/cd_dev.yaml - Use ecosystem config for development deployment - UPDATED: .github/workflows/cd_prod.yaml - Use ecosystem config for production deployment Testing: - Local: pm2 start ecosystem.config.js --env development - Verify env variables load: process.env values should be defined from config.env and .env 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5e88be0 commit 4fc3339

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

.github/workflows/cd_dev.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jobs:
8080
git checkout development
8181
git pull
8282
npm install
83-
pm2 -s start -i max bin/tpen3_services.js
83+
pm2 start ecosystem.config.js --env development
8484
- name: Wait for service to be ready
8585
run: sleep 5
8686
- name: Run smoke tests

.github/workflows/cd_prod.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jobs:
8080
git checkout main
8181
git pull
8282
npm install
83-
pm2 -s start -i max bin/tpen3_services.js
83+
pm2 start ecosystem.config.js --env production
8484
- name: Wait for service to be ready
8585
run: sleep 5
8686
- name: Run smoke tests

ecosystem.config.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* PM2 Ecosystem Configuration for TPEN Services
3+
*
4+
* This configuration ensures that Node.js 22's --env-file flag is properly
5+
* passed when running in PM2 cluster mode, allowing config.env and .env
6+
* to be loaded correctly.
7+
*
8+
* Usage:
9+
* pm2 start ecosystem.config.js --env development
10+
* pm2 start ecosystem.config.js --env production
11+
*/
12+
13+
module.exports = {
14+
apps: [
15+
{
16+
name: 'tpen3-services',
17+
script: './bin/tpen3_services.js',
18+
instances: 'max',
19+
exec_mode: 'cluster',
20+
node_args: '--env-file=config.env --env-file=.env',
21+
env_development: {
22+
NODE_ENV: 'development'
23+
},
24+
env_production: {
25+
NODE_ENV: 'production'
26+
},
27+
// PM2 cluster mode settings
28+
listen_timeout: 10000,
29+
kill_timeout: 5000,
30+
wait_ready: false,
31+
max_memory_restart: '500M',
32+
// Logging
33+
error_file: './logs/pm2-error.log',
34+
out_file: './logs/pm2-out.log',
35+
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
36+
merge_logs: true,
37+
// Auto-restart settings
38+
autorestart: true,
39+
max_restarts: 10,
40+
min_uptime: '10s'
41+
}
42+
]
43+
};

0 commit comments

Comments
 (0)