🚀 Improve local deployment script with enhanced error handling, health checks, and logging

This commit is contained in:
2026-01-30 04:58:44 +00:00
parent 6dd1be08a4
commit d64d2a032e

View File

@@ -206,52 +206,67 @@ async function healthCheck(host, port, retries = 5) {
async function deployLocal(config) { async function deployLocal(config) {
const { execSync } = await import('child_process'); const { execSync } = await import('child_process');
console.log('\n🐳 Deploying to local Docker...'); console.log('\n🐳 Deploying to local Docker...');
// Create modified docker-compose // Create modified docker-compose
const modifiedCompose = createModifiedDockerCompose(config.port, config.sslPort, config.backendPort); const modifiedCompose = createModifiedDockerCompose(
config.port,
config.sslPort,
config.backendPort
);
const tmpComposePath = path.join(SOURCE_DIR, 'docker-compose.tmp.yml'); const tmpComposePath = path.join(SOURCE_DIR, 'docker-compose.tmp.yml');
fs.writeFileSync(tmpComposePath, modifiedCompose); fs.writeFileSync(tmpComposePath, modifiedCompose);
try { try {
// Stop existing // Stop existing
console.log(' 🛑 Stopping existing containers...'); console.log(' 🛑 Stopping existing containers...');
try {
execSync(`docker compose -f "${tmpComposePath}" down --remove-orphans`, {
cwd: SOURCE_DIR,
stdio: 'inherit'
});
} catch (e) {
// Ignore if file doesn't exist yet or other issues on down
try { try {
execSync(`docker compose -f "${tmpComposePath}" down --remove-orphans`, { cwd: SOURCE_DIR, stdio: 'inherit' }); execSync(
} catch (e) { `docker compose -f docker-compose.production.yml down --remove-orphans`,
// Ignore if file doesn't exist yet or other issues on down { cwd: SOURCE_DIR, stdio: 'inherit' }
try { );
execSync(`docker compose -f docker-compose.production.yml down --remove-orphans`, { cwd: SOURCE_DIR, stdio: 'inherit' }); } catch (e2) {
} catch (e2) { /* ignore */ } /* ignore */
} }
}
// Up // Up
console.log(' 🚀 Starting containers...'); console.log(' 🚀 Starting containers...');
execSync(`docker compose -f "${tmpComposePath}" up -d --build`, { cwd: SOURCE_DIR, stdio: 'inherit' }); execSync(`docker compose -f "${tmpComposePath}" up -d --build`, {
cwd: SOURCE_DIR,
// Health Check stdio: 'inherit'
console.log('\n🏥 Performing health checks...'); });
console.log(' Checking frontend...');
const frontendHealthy = await healthCheck('localhost', config.port);
if (!frontendHealthy) throw new Error('Frontend health check failed');
console.log(' ✅ Frontend healthy');
console.log(' Checking backend...'); // Health Check
const backendHealthy = await healthCheck('localhost', config.backendPort); console.log('\n🏥 Performing health checks...');
// Backend might need more time console.log(' Checking frontend...');
if (!backendHealthy) throw new Error('Backend health check failed'); const frontendHealthy = await healthCheck('localhost', config.port);
console.log(' ✅ Backend healthy'); if (!frontendHealthy) throw new Error('Frontend health check failed');
console.log(' ✅ Frontend healthy');
console.log(`\n🎉 Local Deployment successful!`); console.log(' Checking backend...');
console.log(`🌐 Frontend: http://localhost:${config.port}`); const backendHealthy = await healthCheck('localhost', config.backendPort);
if (config.sslPort) console.log(`🔒 HTTPS: https://localhost:${config.sslPort}`); // Backend might need more time
console.log(`🔌 Backend: http://localhost:${config.backendPort}`); if (!backendHealthy) throw new Error('Backend health check failed');
console.log(' ✅ Backend healthy');
console.log(`\n🎉 Local Deployment successful!`);
console.log(`🌐 Frontend: http://localhost:${config.port}`);
if (config.sslPort)
console.log(`🔒 HTTPS: https://localhost:${config.sslPort}`);
console.log(`🔌 Backend: http://localhost:${config.backendPort}`);
} catch (e) { } catch (e) {
console.error('❌ Local deployment failed:', e.message); console.error('❌ Local deployment failed:', e.message);
// Clean up tmp file? Maybe keep for debugging if failed // Clean up tmp file? Maybe keep for debugging if failed
throw e; throw e;
} finally { } finally {
console.log(`\n Docker Compose file: ${tmpComposePath}`); console.log(`\n Docker Compose file: ${tmpComposePath}`);
} }
} }