pipeline { agent any environment { DEPLOY_SERVER = '192.168.1.161' DEPLOY_USER = 'root' DEPLOY_PASS = 'Temp1234!' DEPLOY_PATH = '/opt/trip' } stages { stage('Checkout') { steps { checkout scm } } stage('Build Docker Image') { steps { script { sh 'docker build -t trip:${BUILD_NUMBER} .' sh 'docker tag trip:${BUILD_NUMBER} trip:latest' } } } stage('Save Docker Image') { steps { script { sh 'docker save trip:latest > trip.tar' } } } stage('Deploy') { steps { script { if (env.BRANCH_NAME != 'main') { echo "Skipping deployment: not on main branch" return } echo "Deploying to PRODUCTION VM: ${DEPLOY_SERVER}" sh """ # Installa sshpass se non presente which sshpass || (apt-get update && apt-get install -y sshpass) sshpass -p '${DEPLOY_PASS}' scp -o StrictHostKeyChecking=no trip.tar ${DEPLOY_USER}@${DEPLOY_SERVER}:${DEPLOY_PATH}/ sshpass -p '${DEPLOY_PASS}' scp -o StrictHostKeyChecking=no docker-compose.yml ${DEPLOY_USER}@${DEPLOY_SERVER}:${DEPLOY_PATH}/ # Copia storage solo al primo deploy, poi commenta questa riga sshpass -p '${DEPLOY_PASS}' scp -o StrictHostKeyChecking=no -r storage ${DEPLOY_USER}@${DEPLOY_SERVER}:${DEPLOY_PATH}/ 2>/dev/null || true sshpass -p '${DEPLOY_PASS}' ssh -o StrictHostKeyChecking=no ${DEPLOY_USER}@${DEPLOY_SERVER} " cd ${DEPLOY_PATH} docker load < trip.tar docker-compose down || true docker-compose up -d rm trip.tar " """ echo "Deployment completed to ${DEPLOY_SERVER}" } } } } post { success { echo 'Deployment successful!' } failure { echo 'Deployment failed!' } always { cleanWs() } } }