pipeline { environment { production_server_ip = "46.173.16.83" production_server_name = "4e1f741c-3f7a-407d-904e-6778ec6e6219" project_path = "/projects/dovodi/dovodi-backend" gitBranch = "origin/master" } agent any stages { stage('🚀 Trigger Deployment') { when { expression { gitBranch == "origin/master" } } steps { script { withCredentials([usernamePassword(credentialsId: production_server_name, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { sh """ sshpass -p \$PASSWORD ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 \$USERNAME@${production_server_ip} " cd ${project_path} && \ chmod +x ./runner.sh 2>/dev/null || true && \ rm -f /tmp/dovodi_backend_deploy_status.txt /tmp/dovodi_backend_runner_deploy.log && \ (nohup bash ./runner.sh > /tmp/dovodi_backend_runner_deploy.log 2>&1 &) " """ } } } } stage('⏳ Live Build & Deploy Monitor') { when { expression { gitBranch == "origin/master" } } steps { timeout(time: 15, unit: 'MINUTES') { script { withCredentials([usernamePassword(credentialsId: production_server_name, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { waitUntil { def output = sh( script: """ sshpass -p \$PASSWORD ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 \$USERNAME@${production_server_ip} ' if [ -f /tmp/dovodi_backend_deploy_status.txt ]; then RAW=\$(cat /tmp/dovodi_backend_deploy_status.txt | tr -d "\\r\\n") echo "DEPLOY_STATE_\${RAW}" else echo "DEPLOY_STATE_PENDING" fi echo "===LOG_TAIL===" tail -n 6 /tmp/dovodi_backend_runner_deploy.log 2>/dev/null || true ' """, returnStdout: true ).trim() if (output.contains("DEPLOY_STATE_SUCCESS")) { echo "==========================================" echo "✅ Dovodi Backend deployment completed successfully!" echo "==========================================" return true } else if (output.contains("DEPLOY_STATE_FAIL")) { echo "==========================================" echo "❌ ERROR: Dovodi Backend deployment failed! Full log:" echo "==========================================" sh "sshpass -p \$PASSWORD ssh -o StrictHostKeyChecking=no \$USERNAME@${production_server_ip} 'cat /tmp/dovodi_backend_runner_deploy.log'" error("Dovodi Backend build failed on server.") return true } else { if (output.contains("===LOG_TAIL===")) { def parts = output.split("===LOG_TAIL===") if (parts.length > 1 && parts[1].trim()) { echo "--- 📋 Live Progress ---" echo parts[1].trim() } } sleep 10 return false } } } } } } } } }