my-react-app/Jenkinsfile
2025-08-28 21:42:03 +05:30

82 lines
2.1 KiB
Groovy

pipeline {
agent any
tools {
nodejs 'Node' // Name of your NodeJS installation in Jenkins
}
environment {
WEB_IP = '13.49.223.142' // EC2 Web server IP
SSH_CRED = 'deploy-ec2-key' // Jenkins credential ID for SSH key
NODE_OPTIONS = "--max_old_space_size=512" // Limit Node memory
}
options {
ansiColor('xterm') // Colorized console logs
timestamps() // Show timestamps in console
timeout(time: 30, unit: 'MINUTES') // Fail build if it hangs
}
stages {
stage('Checkout') {
steps {
echo "🔄 Checking out code..."
checkout scm
}
}
stage('Install Dependencies') {
steps {
echo "📦 Installing npm dependencies..."
sh 'npm ci'
}
}
stage('Build') {
steps {
echo "🏗️ Building project..."
sh 'npm run build'
}
}
stage('Package Build') {
steps {
script {
def outDir = fileExists('dist') ? 'dist' : 'build'
if (!fileExists(outDir)) {
error "❌ Build output not found in 'build/' or 'dist/'!"
}
echo "📦 Packaging build output..."
sh "tar -czf build.tar.gz -C ${outDir} ."
archiveArtifacts artifacts: 'build.tar.gz'
}
}
}
stage('Deploy') {
steps {
script {
sshagent(credentials: [env.SSH_CRED]) {
echo "🚀 Deploying build to ${WEB_IP}..."
sh """
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!"
}
}
}
}
}
post {
success { echo "🎉 Pipeline completed successfully!" }
failure { echo "❌ Pipeline failed — check console log" }
}
}