Jenkinsfile 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/html/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('Check Dependencies') {
  36. steps {
  37. sh 'php --version || echo "PHP not available locally"'
  38. sh 'composer --version || echo "Composer not available locally"'
  39. sh 'node --version || echo "Node not available locally"'
  40. sh 'npm --version || echo "NPM not available locally"'
  41. // Check for rsync
  42. sh 'rsync --version || echo "rsync not available, please install it on Jenkins server"'
  43. }
  44. }
  45. stage('Get Code') {
  46. steps {
  47. checkout scm
  48. }
  49. }
  50. stage('Debug Branch Info') {
  51. steps {
  52. sh 'git branch -v || echo "Could not get branch info"'
  53. sh 'git status || echo "Could not get status"'
  54. sh 'echo "BRANCH_NAME: ${BRANCH_NAME:-not set}"'
  55. sh 'echo "GIT_BRANCH: ${GIT_BRANCH:-not set}"'
  56. script {
  57. // Store the branch info for later use
  58. env.CURRENT_BRANCH = sh(script: 'git rev-parse --abbrev-ref HEAD || echo "HEAD"', returnStdout: true).trim()
  59. echo "Current branch detected as: ${env.CURRENT_BRANCH}"
  60. }
  61. }
  62. }
  63. stage('Deploy to Server') {
  64. when {
  65. expression {
  66. // Always deploy from develop branch or if we're in a detached HEAD state
  67. return env.CURRENT_BRANCH == 'HEAD' || env.CURRENT_BRANCH == 'develop' ||
  68. env.GIT_BRANCH == 'origin/develop' || env.BRANCH_NAME == 'develop'
  69. }
  70. }
  71. steps {
  72. echo "Starting deployment to ${REMOTE_HOST} as user ${REMOTE_USER}"
  73. // Using SSH key authentication - no password required
  74. sh '''
  75. # Make sure SSH directory exists with correct permissions
  76. mkdir -p ~/.ssh
  77. chmod 700 ~/.ssh
  78. # Create remote directory
  79. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "mkdir -p ${REMOTE_DIR}"
  80. # Since rsync isn't available, use scp to copy files
  81. # First, create a tar archive
  82. tar --exclude='.git' --exclude='node_modules' --exclude='vendor' -czf /tmp/deployment.tar.gz .
  83. # Copy the archive to the remote server
  84. scp -o StrictHostKeyChecking=no /tmp/deployment.tar.gz ${REMOTE_USER}@${REMOTE_HOST}:/tmp/
  85. # Extract the archive on the remote server
  86. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "tar -xzf /tmp/deployment.tar.gz -C ${REMOTE_DIR}/"
  87. # Clean up
  88. rm -f /tmp/deployment.tar.gz
  89. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "rm -f /tmp/deployment.tar.gz"
  90. # Run composer install
  91. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && composer install --no-interaction --no-dev --prefer-dist"
  92. # Run npm install and build
  93. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && npm install && npm run build"
  94. # Run Laravel artisan commands
  95. 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"
  96. # Set permissions
  97. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && chmod -R 775 storage bootstrap/cache && sudo chown -R www-data:www-data ."
  98. # Restart services
  99. 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'"
  100. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "sudo systemctl restart nginx || echo 'Nginx restart failed, may need manual intervention'"
  101. # Clear cache
  102. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan cache:clear"
  103. '''
  104. }
  105. }
  106. }
  107. post {
  108. always {
  109. script {
  110. cleanWs()
  111. }
  112. }
  113. success {
  114. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  115. script {
  116. mail to: env.ADMIN_EMAIL,
  117. subject: 'Polizia - Build Successful',
  118. body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10'
  119. }
  120. }
  121. failure {
  122. echo 'Build failed! Please check the console output to fix the issues.'
  123. script {
  124. mail to: env.ADMIN_EMAIL,
  125. subject: 'Polizia - Build Failed',
  126. body: 'The build has failed. Please check Jenkins for details.'
  127. }
  128. }
  129. }
  130. }