FabioFratini 10 mēneši atpakaļ
vecāks
revīzija
a262605508
1 mainītis faili ar 55 papildinājumiem un 55 dzēšanām
  1. 55 55
      Jenkinsfile

+ 55 - 55
Jenkinsfile

@@ -30,14 +30,6 @@ pipeline {
         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'
-
-        // Use Jenkins credentials
-        REMOTE_CREDS = credentials('deploy-server-credentials')
     }
 
     stages {
@@ -51,7 +43,6 @@ pipeline {
 
         stage('Check Dependencies') {
             steps {
-                // Check what's available locally (for information only)
                 sh 'php --version || echo "PHP not available locally"'
                 sh 'composer --version || echo "Composer not available locally"'
                 sh 'node --version || echo "Node not available locally"'
@@ -67,10 +58,7 @@ pipeline {
 
         stage('Prepare for Deployment') {
             steps {
-                // Just log what we're doing
                 echo "Preparing application for deployment to ${REMOTE_HOST}"
-
-                // No need to run composer/npm locally since we'll do it on the remote server
                 echo "Dependencies will be installed on the target server during deployment"
             }
         }
@@ -84,64 +72,76 @@ pipeline {
         stage('Deploy to Server') {
             when {
                 expression {
-                    return env.BRANCH_NAME == 'develop' || env.GIT_BRANCH == 'origin/develop'
+                    def branch = sh(script: 'git rev-parse --abbrev-ref HEAD || echo unknown', returnStdout: true).trim()
+                    return branch == 'develop' || branch.contains('develop')
                 }
             }
             steps {
-                // Using withCredentials instead of sshagent
-                withCredentials([sshUserPrivateKey(credentialsId: 'deploy-server-credentials', keyFileVariable: 'SSH_KEY')]) {
-                    // Set up SSH options with the private key
-                    sh """
-                        mkdir -p ~/.ssh
-                        echo "StrictHostKeyChecking no" > ~/.ssh/config
-
-                        # Create remote directory
-                        ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'
-
-                        # Sync files
-                        rsync -avz -e "ssh -i ${SSH_KEY}" --exclude '.git' --exclude 'node_modules' --exclude 'vendor' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
-
-                        # Run deployment commands
-                        ssh -i "${SSH_KEY}" ${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 .'
-
-                        # Restart services
-                        ssh -i "${SSH_KEY}" ${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 -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo "Nginx restart failed, may need manual intervention"'
-
-                        # Clear cache
-                        ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'
-                    """
-                }
+                // Using SSH with identity file from Jenkins home directory
+                // Make sure to place the SSH key at /var/lib/jenkins/.ssh/id_rsa
+                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 && \\
+                    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"
+                '''
             }
         }
     }
 
     post {
         always {
-            cleanWs()
+            script {
+                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'
+
+            script {
+                mail to: env.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.'
+
+            script {
+                mail to: env.ADMIN_EMAIL,
+                     subject: 'Polizia - Build Failed',
+                     body: 'The build has failed. Please check Jenkins for details.'
+            }
         }
     }
 }