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 {
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" }
}
}