my-react-app/Jenkinsfile

65 lines
1.5 KiB
Groovy

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
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Install') {
steps {
sh 'npm install' //
}
}
stage('Build') {
steps {
sh 'npm run build'
}
}
stage('Package') {
steps {
sh '''
OUT_DIR="build"
[ -d dist ] && OUT_DIR="dist"
if [ ! -d "$OUT_DIR" ]; then
echo "ERROR: No build output found (build/ or dist/)"
ls -la
exit 1
fi
tar -czf build.tar.gz -C "$OUT_DIR" .
'''
archiveArtifacts artifacts: 'build.tar.gz'
}
}
stage('Deploy') {
steps {
sshagent(credentials: [env.SSH_CRED]) {
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
'
'''
}
}
}
}
post {
success { echo "✅ Deployed successfully" }
failure { echo "❌ Failed — check console" }
}
}