Jenkinsfile 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. pipeline {
  2. agent any
  3. environment {
  4. APP_NAME = 'Polizia'
  5. APP_ENV = 'testing'
  6. APP_KEY = 'base64:1YsMWZ+cIDVa5NIePkjsXVheT9rbykHDs/CnGPUQdqU='
  7. APP_DEBUG = 'false'
  8. APP_URL = 'https://frascati.dev.webmagistri.biz'
  9. LOG_CHANNEL = 'stack'
  10. LOG_DEPRECATIONS_CHANNEL = 'null'
  11. LOG_LEVEL = 'debug'
  12. DB_CONNECTION = 'mysql'
  13. DB_HOST = '127.0.0.1'
  14. DB_PORT = '3306'
  15. DB_DATABASE = 'polizia_online'
  16. DB_USERNAME = 'admin'
  17. DB_PASSWORD = 'admin'
  18. MCTC_URL = 'https://www.ilportaledellautomobilista.it/Info-ws/services'
  19. MCTC_USER = 'CMRM001301'
  20. MCTC_PASSWORD = '2PMPM*86'
  21. STORAGE_PATH = 'app/public/'
  22. REMOTE_HOST = '10.2.0.10'
  23. REMOTE_USER = 'fratini'
  24. REMOTE_DIR = '/var/www/polizia'
  25. ADMIN_EMAIL = 'f.fratini@webmagistri.it'
  26. }
  27. stages {
  28. stage('Verify Host') {
  29. steps {
  30. sh 'hostname'
  31. sh 'hostname -I'
  32. sh 'whoami'
  33. }
  34. }
  35. stage('Get Code') {
  36. steps {
  37. checkout scm
  38. }
  39. }
  40. stage('Debug Branch Info') {
  41. steps {
  42. script {
  43. // Store the branch info for later use
  44. env.CURRENT_BRANCH = sh(script: 'git rev-parse --abbrev-ref HEAD || echo "HEAD"', returnStdout: true).trim()
  45. echo "Current branch detected as: ${env.CURRENT_BRANCH}"
  46. }
  47. }
  48. }
  49. stage('Deploy to Server') {
  50. when {
  51. expression {
  52. // Always deploy from develop branch or if we're in a detached HEAD state
  53. return env.CURRENT_BRANCH == 'HEAD' || env.CURRENT_BRANCH == 'develop' ||
  54. env.GIT_BRANCH == 'origin/develop' || env.BRANCH_NAME == 'develop'
  55. }
  56. }
  57. steps {
  58. echo "Starting deployment to ${REMOTE_HOST} as user ${REMOTE_USER}"
  59. sh '''
  60. # Make sure SSH directory exists with correct permissions
  61. mkdir -p ~/.ssh
  62. chmod 700 ~/.ssh
  63. # Create remote directory - without using sudo
  64. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "mkdir -p ${REMOTE_DIR}"
  65. # Remove existing files (if any) that we have permission to remove
  66. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "find ${REMOTE_DIR} -type f -writable -delete || true"
  67. # Create a tar archive excluding unnecessary files
  68. tar --exclude='.git' --exclude='node_modules' --exclude='vendor' -czf /tmp/deployment.tar.gz .
  69. # Copy the archive to the remote server
  70. scp -o StrictHostKeyChecking=no /tmp/deployment.tar.gz ${REMOTE_USER}@${REMOTE_HOST}:/tmp/
  71. # Extract the archive on the remote server with --no-same-owner to avoid permission issues
  72. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && tar -xzf /tmp/deployment.tar.gz --no-same-owner || echo 'Tar extraction had some issues, but continuing...'"
  73. # Clean up
  74. rm -f /tmp/deployment.tar.gz
  75. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "rm -f /tmp/deployment.tar.gz"
  76. # Check for PHP
  77. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "php -v || echo 'PHP not available on remote server'"
  78. # Check for Composer and install it if missing (without sudo)
  79. echo "Checking for Composer..."
  80. COMPOSER_INSTALLED=$(ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "command -v composer || echo 'not found'")
  81. if [[ "$COMPOSER_INSTALLED" == *"not found"* ]]; then
  82. echo "Composer not found. Please install it manually on the remote server."
  83. else
  84. echo "Composer is already installed."
  85. fi
  86. # Run Composer install
  87. echo "Running composer install..."
  88. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && composer install --no-interaction --no-dev --prefer-dist || echo 'Composer install failed, continuing...'"
  89. # Check for Node.js/NPM
  90. NODE_INSTALLED=$(ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "command -v node || echo 'not found'")
  91. if [[ "$NODE_INSTALLED" == *"not found"* ]]; then
  92. echo "Node.js not found - skipping npm steps"
  93. else
  94. echo "Running npm install and build..."
  95. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && npm install && npm run build || echo 'NPM build failed, continuing...'"
  96. fi
  97. # Set proper permissions for Laravel directories that need to be writable
  98. echo "Setting directory permissions..."
  99. 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"
  100. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && chmod -R 775 storage bootstrap/cache || echo 'Permission setting had some issues, but continuing...'"
  101. # Run Laravel artisan commands
  102. echo "Running Laravel artisan commands..."
  103. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan migrate --force || echo 'Migration failed, continuing...'"
  104. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan config:cache || echo 'Config cache failed, continuing...'"
  105. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan route:cache || echo 'Route cache failed, continuing...'"
  106. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan view:cache || echo 'View cache failed, continuing...'"
  107. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan optimize || echo 'Optimize failed, continuing...'"
  108. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan storage:link || echo 'Storage link failed, continuing...'"
  109. # Notify completion
  110. echo "Deployment completed. Some steps may have been skipped due to permission issues."
  111. echo "You may need to manually adjust file permissions or restart services if needed."
  112. '''
  113. }
  114. }
  115. }
  116. post {
  117. always {
  118. script {
  119. cleanWs()
  120. }
  121. }
  122. success {
  123. echo 'Build completed! The Polizia application has been deployed to 10.2.0.10'
  124. echo 'Note: You may need to manually restart web services and adjust permissions if needed.'
  125. script {
  126. mail to: env.ADMIN_EMAIL,
  127. subject: 'Polizia - Build Completed',
  128. body: 'The build process has completed. The Polizia application has been deployed to the server at 10.2.0.10. You may need to manually restart web services and adjust permissions if needed.'
  129. }
  130. }
  131. failure {
  132. echo 'Build failed! Please check the console output to fix the issues.'
  133. script {
  134. mail to: env.ADMIN_EMAIL,
  135. subject: 'Polizia - Build Failed',
  136. body: 'The build has failed. Please check Jenkins for details.'
  137. }
  138. }
  139. }
  140. }