Fix Jenkinsfile with memory options and stable deploy

This commit is contained in:
VIPIN 2025-08-28 18:32:01 +05:30
parent e939fe1353
commit f2e40c5089

37
Jenkinsfile vendored
View File

@ -1,10 +1,16 @@
pipeline { pipeline {
agent any agent any
tools { nodejs 'Node' } // make sure in Jenkins -> Global Tool Config -> NodeJS is named "Node"
environment { tools {
WEB_IP = '13.49.223.142' // your EC2 web server IP nodejs 'Node' // Jenkins -> Global Tool Config -> NodeJS must be named "Node"
SSH_CRED = 'deploy-ec2-key' // Jenkins credential ID for SSH key
} }
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 { stages {
stage('Checkout') { stage('Checkout') {
steps { steps {
@ -14,13 +20,19 @@ pipeline {
stage('Install') { stage('Install') {
steps { steps {
sh 'npm install' // sh '''
echo "📦 Installing dependencies..."
npm install
'''
} }
} }
stage('Build') { stage('Build') {
steps { steps {
sh 'npm run build' sh '''
echo "🏗️ Building project..."
npm run build
'''
} }
} }
@ -29,11 +41,14 @@ pipeline {
sh ''' sh '''
OUT_DIR="build" OUT_DIR="build"
[ -d dist ] && OUT_DIR="dist" [ -d dist ] && OUT_DIR="dist"
if [ ! -d "$OUT_DIR" ]; then 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 ls -la
exit 1 exit 1
fi fi
echo "📦 Packaging build output..."
tar -czf build.tar.gz -C "$OUT_DIR" . tar -czf build.tar.gz -C "$OUT_DIR" .
''' '''
archiveArtifacts artifacts: 'build.tar.gz' archiveArtifacts artifacts: 'build.tar.gz'
@ -44,13 +59,17 @@ pipeline {
steps { steps {
sshagent(credentials: [env.SSH_CRED]) { sshagent(credentials: [env.SSH_CRED]) {
sh ''' sh '''
echo "🚀 Deploying to ${WEB_IP} ..."
scp -o StrictHostKeyChecking=no build.tar.gz deploy@${WEB_IP}:/tmp/build.tar.gz scp -o StrictHostKeyChecking=no build.tar.gz deploy@${WEB_IP}:/tmp/build.tar.gz
ssh -o StrictHostKeyChecking=no deploy@${WEB_IP} ' ssh -o StrictHostKeyChecking=no deploy@${WEB_IP} '
sudo rm -rf /var/www/reactapp/*; sudo rm -rf /var/www/reactapp/*;
sudo mkdir -p /var/www/reactapp; sudo mkdir -p /var/www/reactapp;
sudo tar -xzf /tmp/build.tar.gz -C /var/www/reactapp; sudo tar -xzf /tmp/build.tar.gz -C /var/www/reactapp;
sudo systemctl reload nginx sudo systemctl reload nginx
' '
echo "✅ Deployment finished"
''' '''
} }
} }
@ -58,7 +77,7 @@ pipeline {
} }
post { post {
success { echo "✅ Deployed successfully" } success { echo "✅ Pipeline completed successfully" }
failure { echo "❌ Failed — check console" } failure { echo "❌ Pipeline failed — check console log" }
} }
} }