pipeline { agent any environment { APP_NAME = 'Polizia' APP_ENV = 'testing' APP_KEY = 'base64:1YsMWZ+cIDVa5NIePkjsXVheT9rbykHDs/CnGPUQdqU=' APP_DEBUG = 'false' APP_URL = 'https://frascati.dev.webmagistri.biz' LOG_CHANNEL = 'stack' LOG_DEPRECATIONS_CHANNEL = 'null' LOG_LEVEL = 'debug' DB_CONNECTION = 'mysql' DB_HOST = '127.0.0.1' DB_PORT = '3306' DB_DATABASE = 'polizia_online' DB_USERNAME = 'admin' DB_PASSWORD = 'admin' MCTC_URL = 'https://www.ilportaledellautomobilista.it/Info-ws/services' MCTC_USER = 'CMRM001301' MCTC_PASSWORD = '2PMPM*86' STORAGE_PATH = 'app/public/' REMOTE_HOST = '10.2.0.10' REMOTE_USER = 'deploy' REMOTE_DIR = '/var/www/html/polizia' ADMIN_EMAIL = 'f.fratini@webmagistri.it' // PHP version to install if needed PHP_VERSION = '8.1' // Node version to install if needed NODE_VERSION = '18.x' } stages { stage('Verify Host') { steps { sh 'hostname' sh 'hostname -I' sh 'whoami' } } stage('Setup Tools') { steps { script { // Check and install PHP if not available def phpInstalled = sh(script: 'php --version', returnStatus: true) if (phpInstalled != 0) { echo "Installing PHP ${PHP_VERSION}..." sh """ sudo apt-get update sudo apt-get install -y software-properties-common sudo add-apt-repository -y ppa:ondrej/php sudo apt-get update sudo apt-get install -y php${PHP_VERSION} php${PHP_VERSION}-cli php${PHP_VERSION}-common php${PHP_VERSION}-curl php${PHP_VERSION}-mbstring php${PHP_VERSION}-mysql php${PHP_VERSION}-xml php${PHP_VERSION}-zip """ } // Check and install Composer if not available def composerInstalled = sh(script: 'composer --version', returnStatus: true) if (composerInstalled != 0) { echo "Installing Composer..." sh """ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php --install-dir=/usr/local/bin --filename=composer php -r "unlink('composer-setup.php');" chmod +x /usr/local/bin/composer """ } // Check and install Node.js and NPM if not available def nodeInstalled = sh(script: 'node --version', returnStatus: true) if (nodeInstalled != 0) { echo "Installing Node.js ${NODE_VERSION}..." sh """ curl -sL https://deb.nodesource.com/setup_${NODE_VERSION} | sudo -E bash - sudo apt-get install -y nodejs """ } } // Verify installations sh 'php --version' sh 'composer --version' sh 'node --version' sh 'npm --version' } } stage('Get Code') { steps { checkout scm } } stage('Install Dependencies') { steps { sh 'composer install --no-interaction --no-progress --prefer-dist' } } stage('Setup Environment') { steps { sh 'php artisan key:generate || echo "Skipping key generation"' sh 'php artisan config:clear' sh 'php artisan cache:clear' } } stage('Build Assets') { steps { sh 'npm install' sh 'npm run build' } } stage('Debug') { steps { sh 'git branch --show-current' sh 'git rev-parse --abbrev-ref HEAD' } } stage('Deploy to Server') { when { expression { return env.BRANCH_NAME == 'develop' } } steps { sshagent(['ssh-key-10.2.0.10']) { sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'" sh """ rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' -e 'ssh -o StrictHostKeyChecking=no' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/ """ sh """ ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \ composer install --no-interaction --no-dev --prefer-dist && \ npm install && \ npm run build && \ php artisan migrate --force && \ php artisan config:cache && \ php artisan route:cache && \ php artisan view:cache && \ php artisan optimize && \ php artisan storage:link && \ chmod -R 775 storage bootstrap/cache && \ chown -R www-data:www-data .' """ sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart php8.1-fpm || sudo systemctl restart php-fpm || echo \"PHP service restart failed, may need manual intervention\"'" sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo \"Nginx restart failed, may need manual intervention\"'" sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'" } } } } post { always { node('any') { cleanWs() } } success { echo 'Build successful! The Polizia application is now deployed to 10.2.0.10' mail to: "${ADMIN_EMAIL}", subject: 'Polizia - Build Successful', body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10' } failure { echo 'Build failed! Please check the console output to fix the issues.' mail to: "${ADMIN_EMAIL}", subject: 'Polizia - Build Failed', body: 'The build has failed. Please check Jenkins for details.' } } }