From f2e40c50890f2579d38ae870004f3cd8bee8d5d4 Mon Sep 17 00:00:00 2001 From: VIPIN Date: Thu, 28 Aug 2025 18:32:01 +0530 Subject: [PATCH] Fix Jenkinsfile with memory options and stable deploy --- Jenkinsfile | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 607c5a1..e887000 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,10 +1,16 @@ pipeline { agent any - tools { nodejs 'Node' } // make sure in Jenkins -> Global Tool Config -> NodeJS is named "Node" - environment { - WEB_IP = '13.49.223.142' // your EC2 web server IP - SSH_CRED = 'deploy-ec2-key' // Jenkins credential ID for SSH key + + tools { + nodejs 'Node' // Jenkins -> Global Tool Config -> NodeJS must be named "Node" } + + environment { + WEB_IP = '13.49.223.142' // update if EC2 IP changes + SSH_CRED = 'deploy-ec2-key' // Jenkins credential ID for your EC2 SSH key + NODE_OPTIONS = "--max_old_space_size=512" // prevent Node from eating all memory + } + stages { stage('Checkout') { steps { @@ -14,13 +20,19 @@ pipeline { stage('Install') { steps { - sh 'npm install' // + sh ''' + echo "📦 Installing dependencies..." + npm install + ''' } } stage('Build') { steps { - sh 'npm run build' + sh ''' + echo "🏗️ Building project..." + npm run build + ''' } } @@ -29,11 +41,14 @@ pipeline { sh ''' OUT_DIR="build" [ -d dist ] && OUT_DIR="dist" + if [ ! -d "$OUT_DIR" ]; then - echo "ERROR: No build output found (build/ or dist/)" + echo "❌ ERROR: No build output found (build/ or dist/)" ls -la exit 1 fi + + echo "📦 Packaging build output..." tar -czf build.tar.gz -C "$OUT_DIR" . ''' archiveArtifacts artifacts: 'build.tar.gz' @@ -44,13 +59,17 @@ pipeline { steps { sshagent(credentials: [env.SSH_CRED]) { sh ''' + echo "🚀 Deploying to ${WEB_IP} ..." scp -o StrictHostKeyChecking=no build.tar.gz deploy@${WEB_IP}:/tmp/build.tar.gz + ssh -o StrictHostKeyChecking=no deploy@${WEB_IP} ' sudo rm -rf /var/www/reactapp/*; sudo mkdir -p /var/www/reactapp; sudo tar -xzf /tmp/build.tar.gz -C /var/www/reactapp; sudo systemctl reload nginx ' + + echo "✅ Deployment finished" ''' } } @@ -58,7 +77,7 @@ pipeline { } post { - success { echo "✅ Deployed successfully" } - failure { echo "❌ Failed — check console" } + success { echo "✅ Pipeline completed successfully" } + failure { echo "❌ Pipeline failed — check console log" } } }