fix: Add .env file loading to ecosystem-test.config.cjs

Allows test environment PM2 processes to load environment variables
from /var/www/flyer-crawler-test.projectium.com/.env file, enabling
manual restarts without requiring CI/CD to inject variables.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-17 13:38:15 -08:00
parent dd31141d4e
commit f49f786c23

View File

@@ -7,10 +7,53 @@
// //
// These apps: // These apps:
// - Run from /var/www/flyer-crawler-test.projectium.com // - Run from /var/www/flyer-crawler-test.projectium.com
// - Use NODE_ENV='test' (enables file logging in logger.server.ts) // - Use NODE_ENV='staging' (enables file logging in logger.server.ts)
// - Use Redis database 1 (isolated from production which uses database 0) // - Use Redis database 1 (isolated from production which uses database 0)
// - Have distinct PM2 process names to avoid conflicts with production // - Have distinct PM2 process names to avoid conflicts with production
// --- Load Environment Variables from .env file ---
// This allows PM2 to start without requiring the CI/CD pipeline to inject variables.
// The .env file should be created on the server with the required secrets.
// NOTE: We implement a simple .env parser since dotenv may not be installed.
const path = require('path');
const fs = require('fs');
const envPath = path.join('/var/www/flyer-crawler-test.projectium.com', '.env');
if (fs.existsSync(envPath)) {
console.log('[ecosystem-test.config.cjs] Loading environment from:', envPath);
const envContent = fs.readFileSync(envPath, 'utf8');
const lines = envContent.split('\n');
for (const line of lines) {
// Skip comments and empty lines
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
// Parse KEY=value
const eqIndex = trimmed.indexOf('=');
if (eqIndex > 0) {
const key = trimmed.substring(0, eqIndex);
let value = trimmed.substring(eqIndex + 1);
// Remove quotes if present
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
// Only set if not already in environment (don't override CI/CD vars)
if (!process.env[key]) {
process.env[key] = value;
}
}
}
console.log('[ecosystem-test.config.cjs] Environment loaded successfully');
} else {
console.warn('[ecosystem-test.config.cjs] No .env file found at:', envPath);
console.warn(
'[ecosystem-test.config.cjs] Environment variables must be provided by the shell or CI/CD.'
);
}
// --- Environment Variable Validation --- // --- Environment Variable Validation ---
// NOTE: We only WARN about missing secrets, not exit. // NOTE: We only WARN about missing secrets, not exit.
// Calling process.exit(1) prevents PM2 from reading the apps array. // Calling process.exit(1) prevents PM2 from reading the apps array.