Jenkinsfile 5.7 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' // Changed from 'deploy' to '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. }
  42. }
  43. stage('Get Code') {
  44. steps {
  45. checkout scm
  46. }
  47. }
  48. stage('Prepare for Deployment') {
  49. steps {
  50. echo "Preparing application for deployment to ${REMOTE_HOST}"
  51. echo "Dependencies will be installed on the target server during deployment"
  52. }
  53. }
  54. stage('Debug Branch Info') {
  55. steps {
  56. sh 'git branch -v'
  57. sh 'git status'
  58. sh 'echo "BRANCH_NAME: ${BRANCH_NAME:-not set}"'
  59. sh 'echo "GIT_BRANCH: ${GIT_BRANCH:-not set}"'
  60. sh 'git rev-parse --abbrev-ref HEAD || echo "Cannot get branch name"'
  61. script {
  62. // Store the branch info for later use
  63. env.CURRENT_BRANCH = sh(script: 'git rev-parse --abbrev-ref HEAD || echo "HEAD"', returnStdout: true).trim()
  64. echo "Current branch detected as: ${env.CURRENT_BRANCH}"
  65. }
  66. }
  67. }
  68. stage('Deploy to Server') {
  69. when {
  70. expression {
  71. // Always deploy from develop branch
  72. return env.CURRENT_BRANCH == 'HEAD' || env.CURRENT_BRANCH == 'develop' ||
  73. env.GIT_BRANCH == 'origin/develop' || env.BRANCH_NAME == 'develop'
  74. }
  75. }
  76. steps {
  77. echo "Starting deployment to ${REMOTE_HOST} as user ${REMOTE_USER}"
  78. // Using SSH with identity file from Jenkins home directory
  79. sh '''
  80. # Ensure SSH directory exists
  81. mkdir -p ~/.ssh
  82. chmod 700 ~/.ssh
  83. # Configure SSH to skip host key checking
  84. echo "StrictHostKeyChecking no" > ~/.ssh/config
  85. chmod 600 ~/.ssh/config
  86. # Create remote directory
  87. ssh ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'
  88. # Sync files excluding unnecessary ones
  89. rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  90. # Run deployment commands
  91. ssh ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && \\
  92. composer install --no-interaction --no-dev --prefer-dist && \\
  93. npm install && \\
  94. npm run build && \\
  95. php artisan migrate --force && \\
  96. php artisan config:cache && \\
  97. php artisan route:cache && \\
  98. php artisan view:cache && \\
  99. php artisan optimize && \\
  100. php artisan storage:link && \\
  101. chmod -R 775 storage bootstrap/cache && \\
  102. sudo chown -R www-data:www-data ."
  103. # Restart services
  104. 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'"
  105. ssh ${REMOTE_USER}@${REMOTE_HOST} "sudo systemctl restart nginx || echo 'Nginx restart failed, may need manual intervention'"
  106. # Clear cache
  107. ssh ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan cache:clear"
  108. '''
  109. }
  110. }
  111. }
  112. post {
  113. always {
  114. script {
  115. cleanWs()
  116. }
  117. }
  118. success {
  119. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  120. script {
  121. mail to: env.ADMIN_EMAIL,
  122. subject: 'Polizia - Build Successful',
  123. body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10'
  124. }
  125. }
  126. failure {
  127. echo 'Build failed! Please check the console output to fix the issues.'
  128. script {
  129. mail to: env.ADMIN_EMAIL,
  130. subject: 'Polizia - Build Failed',
  131. body: 'The build has failed. Please check Jenkins for details.'
  132. }
  133. }
  134. }
  135. }