| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- pipeline {
- agent any
- tools {
- php 'PHP 8.2'
- nodejs 'NodeJS 18'
- }
- environment {
- APP_NAME = 'Polizia'
- APP_ENV = 'testing'
- APP_KEY = credentials('APP_KEY')
- 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 = credentials('DB_USERNAME')
- DB_PASSWORD = credentials('DB_PASSWORD')
- BROADCAST_DRIVER = 'log'
- CACHE_DRIVER = 'file'
- FILESYSTEM_DISK = 'local'
- QUEUE_CONNECTION = 'sync'
- SESSION_DRIVER = 'file'
- SESSION_LIFETIME = '120'
- MAIL_MAILER = 'smtp'
- MAIL_HOST = 'mailpit'
- MAIL_PORT = '1025'
- MAIL_USERNAME = 'null'
- MAIL_PASSWORD = 'null'
- MAIL_ENCRYPTION = 'null'
- MAIL_FROM_ADDRESS = 'hello@example.com'
- MAIL_FROM_NAME = 'Polizia'
- MCTC_URL = 'https://www.ilportaledellautomobilista.it/Info-ws/services'
- MCTC_USER = credentials('MCTC_USER')
- MCTC_PASSWORD = credentials('MCTC_PASSWORD')
- STORAGE_PATH = 'app/public/'
- REMOTE_HOST = '10.2.0.10'
- REMOTE_USER = credentials('REMOTE_USER')
- REMOTE_DIR = '/var/www/html/polizia'
- }
- stages {
- stage('Setup') {
- steps {
- sh 'php -v'
- sh 'node -v'
- sh 'npm -v'
- // Install composer if not available
- sh '''
- if ! command -v composer &> /dev/null; then
- curl -sS https://getcomposer.org/installer | php
- mv composer.phar /usr/local/bin/composer || sudo mv composer.phar /usr/local/bin/composer
- fi
- '''
- sh 'composer --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 'cp .env.example .env'
- sh 'php artisan key:generate'
- sh 'php artisan config:clear'
- sh 'php artisan cache:clear'
- }
- }
- stage('Run Tests') {
- steps {
- sh 'php artisan config:cache'
- sh 'php artisan test --testsuite=Feature --testsuite=Unit || true'
- sh 'php artisan config:clear'
- }
- }
- stage('Build Assets') {
- steps {
- sh 'npm install'
- sh 'npm run build'
- }
- }
- stage('Deploy to Server') {
- when {
- anyOf {
- branch 'master'
- branch 'main'
- }
- }
- steps {
- sshagent(['ssh-key-10.2.0.10']) {
- // Ensure the remote directory exists
- sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
- // Create a tar archive of the project
- sh 'tar -czf project.tar.gz --exclude=node_modules --exclude=vendor --exclude=.git .'
- // Transfer the archive to remote server
- sh "scp -o StrictHostKeyChecking=no project.tar.gz ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}"
- // Extract on remote server and setup
- sh """
- ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
- tar -xzf project.tar.gz && \
- rm project.tar.gz && \
- 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 && \
- chown -R www-data:www-data .'
- """
- // Restart relevant services if needed
- sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart php8.1-fpm || sudo systemctl restart php-fpm'"
- sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx'"
- // Clear any caches
- sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'"
- }
- }
- }
- }
- post {
- always {
- cleanWs()
- }
- success {
- echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
- mail to: credentials('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: credentials('ADMIN_EMAIL'), subject: 'Polizia - Build Failed', body: 'The build has failed. Please check Jenkins for details.'
- }
- }
- }
|