Jenkinsfile 6.2 KB

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