FabioFratini 10 miesięcy temu
rodzic
commit
aa1b326775
1 zmienionych plików z 34 dodań i 48 usunięć
  1. 34 48
      Jenkinsfile

+ 34 - 48
Jenkinsfile

@@ -26,9 +26,13 @@ pipeline {
         STORAGE_PATH = 'app/public/'
 
         REMOTE_HOST = '10.2.0.10'
-        REMOTE_USER = 'fratini'  // Changed from 'deploy' to 'fratini'
+        REMOTE_USER = 'fratini'
         REMOTE_DIR = '/var/www/html/polizia'
 
+        // Store the SSH password as an environment variable
+        // You need to set this in Jenkins as a secret text credential
+        SSH_PASS = credentials('fratini-ssh-password')
+
         ADMIN_EMAIL = 'f.fratini@webmagistri.it'
     }
 
@@ -47,6 +51,9 @@ pipeline {
                 sh 'composer --version || echo "Composer not available locally"'
                 sh 'node --version || echo "Node not available locally"'
                 sh 'npm --version || echo "NPM not available locally"'
+
+                // Check if sshpass is installed
+                sh 'sshpass -V || echo "sshpass not available, please install it on Jenkins server"'
             }
         }
 
@@ -56,20 +63,12 @@ pipeline {
             }
         }
 
-        stage('Prepare for Deployment') {
-            steps {
-                echo "Preparing application for deployment to ${REMOTE_HOST}"
-                echo "Dependencies will be installed on the target server during deployment"
-            }
-        }
-
         stage('Debug Branch Info') {
             steps {
-                sh 'git branch -v'
-                sh 'git status'
+                sh 'git branch -v || echo "Could not get branch info"'
+                sh 'git status || echo "Could not get status"'
                 sh 'echo "BRANCH_NAME: ${BRANCH_NAME:-not set}"'
                 sh 'echo "GIT_BRANCH: ${GIT_BRANCH:-not set}"'
-                sh 'git rev-parse --abbrev-ref HEAD || echo "Cannot get branch name"'
                 script {
                     // Store the branch info for later use
                     env.CURRENT_BRANCH = sh(script: 'git rev-parse --abbrev-ref HEAD || echo "HEAD"', returnStdout: true).trim()
@@ -89,43 +88,30 @@ pipeline {
             steps {
                 echo "Starting deployment to ${REMOTE_HOST} as user ${REMOTE_USER}"
 
-                // Using SSH with identity file from Jenkins home directory
-                sh '''
-                    # Ensure SSH directory exists
-                    mkdir -p ~/.ssh
-                    chmod 700 ~/.ssh
-
-                    # Configure SSH to skip host key checking
-                    echo "StrictHostKeyChecking no" > ~/.ssh/config
-                    chmod 600 ~/.ssh/config
-
-                    # Create remote directory
-                    ssh ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'
-
-                    # Sync files excluding unnecessary ones
-                    rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
-
-                    # Run deployment commands
-                    ssh ${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 && \\
-                    sudo chown -R www-data:www-data ."
-
-                    # Restart services
-                    ssh ${REMOTE_USER}@${REMOTE_HOST} "sudo systemctl restart php8.1-fpm || sudo systemctl restart php-fpm || echo 'PHP service restart failed, may need manual intervention'"
-                    ssh ${REMOTE_USER}@${REMOTE_HOST} "sudo systemctl restart nginx || echo 'Nginx restart failed, may need manual intervention'"
-
-                    # Clear cache
-                    ssh ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan cache:clear"
-                '''
+                // Create remote directory
+                sh "sshpass -p \"\${SSH_PASS}\" ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
+
+                // Sync files
+                sh "sshpass -p \"\${SSH_PASS}\" rsync -avz -e \"ssh -o StrictHostKeyChecking=no\" --exclude '.git' --exclude 'node_modules' --exclude 'vendor' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/"
+
+                // Run composer install
+                sh "sshpass -p \"\${SSH_PASS}\" ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && composer install --no-interaction --no-dev --prefer-dist'"
+
+                // Run npm install and build
+                sh "sshpass -p \"\${SSH_PASS}\" ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && npm install && npm run build'"
+
+                // Run Laravel artisan commands
+                sh "sshpass -p \"\${SSH_PASS}\" ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan migrate --force && php artisan config:cache && php artisan route:cache && php artisan view:cache && php artisan optimize && php artisan storage:link'"
+
+                // Set permissions
+                sh "sshpass -p \"\${SSH_PASS}\" ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && chmod -R 775 storage bootstrap/cache && sudo chown -R www-data:www-data .'"
+
+                // Restart services
+                sh "sshpass -p \"\${SSH_PASS}\" 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 "sshpass -p \"\${SSH_PASS}\" ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo \"Nginx restart failed, may need manual intervention\"'"
+
+                // Clear cache
+                sh "sshpass -p \"\${SSH_PASS}\" ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'"
             }
         }
     }