Browse Source

permessi jkf

FabioFratini 10 months ago
parent
commit
92ef564b1e
1 changed files with 17 additions and 9 deletions
  1. 17 9
      Jenkinsfile

+ 17 - 9
Jenkinsfile

@@ -27,7 +27,7 @@ pipeline {
 
         REMOTE_HOST = '10.2.0.10'
         REMOTE_USER = 'fratini'
-        REMOTE_DIR = '/var/www/polizia'  // Updated to the correct path
+        REMOTE_DIR = '/var/www/polizia'
 
         ADMIN_EMAIL = 'f.fratini@webmagistri.it'
     }
@@ -73,8 +73,11 @@ pipeline {
                     mkdir -p ~/.ssh
                     chmod 700 ~/.ssh
 
-                    # Create remote directory
-                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "mkdir -p ${REMOTE_DIR}"
+                    # First, clean up the target directory to ensure we have permissions
+                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "if [ -d '${REMOTE_DIR}' ]; then sudo rm -rf ${REMOTE_DIR}/*; else sudo mkdir -p ${REMOTE_DIR}; fi"
+
+                    # Ensure the directory is owned by our user
+                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "sudo chown -R ${REMOTE_USER}:${REMOTE_USER} ${REMOTE_DIR}"
 
                     # Create a tar archive excluding unnecessary files
                     tar --exclude='.git' --exclude='node_modules' --exclude='vendor' -czf /tmp/deployment.tar.gz .
@@ -82,8 +85,8 @@ pipeline {
                     # Copy the archive to the remote server
                     scp -o StrictHostKeyChecking=no /tmp/deployment.tar.gz ${REMOTE_USER}@${REMOTE_HOST}:/tmp/
 
-                    # Extract the archive on the remote server
-                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "tar -xzf /tmp/deployment.tar.gz -C ${REMOTE_DIR}/"
+                    # Extract the archive on the remote server with --no-same-owner to avoid permission issues
+                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && tar -xzf /tmp/deployment.tar.gz --no-same-owner"
 
                     # Clean up
                     rm -f /tmp/deployment.tar.gz
@@ -115,6 +118,11 @@ pipeline {
                         ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && npm install && npm run build"
                     fi
 
+                    # Set proper permissions for Laravel directories that need to be writable
+                    echo "Setting directory permissions..."
+                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && mkdir -p bootstrap/cache storage/framework/sessions storage/framework/views storage/framework/cache storage/logs"
+                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && chmod -R 775 storage bootstrap/cache"
+
                     # Run Laravel artisan commands
                     echo "Running Laravel artisan commands..."
                     ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan migrate --force || echo 'Migration failed, continuing...'"
@@ -124,9 +132,9 @@ pipeline {
                     ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan optimize || echo 'Optimize failed, continuing...'"
                     ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan storage:link || echo 'Storage link failed, continuing...'"
 
-                    # Set permissions
-                    echo "Setting permissions..."
-                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && chmod -R 775 storage bootstrap/cache && sudo chown -R www-data:www-data ."
+                    # Set final ownership for web server
+                    echo "Setting web server ownership..."
+                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "sudo chown -R www-data:www-data ${REMOTE_DIR}"
 
                     # Restart services
                     echo "Restarting services..."
@@ -135,7 +143,7 @@ pipeline {
 
                     # Clear cache
                     echo "Clearing cache..."
-                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan cache:clear || echo 'Cache clear failed, continuing...'"
+                    ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && sudo -u www-data php artisan cache:clear || echo 'Cache clear failed, continuing...'"
                 '''
             }
         }