Jenkinsfile 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 = 'deploy'
  24. REMOTE_DIR = '/var/www/html/polizia'
  25. ADMIN_EMAIL = 'f.fratini@webmagistri.it'
  26. // PHP version to install if needed
  27. PHP_VERSION = '8.1'
  28. // Node version to install if needed
  29. NODE_VERSION = '18.x'
  30. }
  31. stages {
  32. stage('Verify Host') {
  33. steps {
  34. sh 'hostname'
  35. sh 'hostname -I'
  36. sh 'whoami'
  37. }
  38. }
  39. stage('Check Remote Tools') {
  40. steps {
  41. script {
  42. // Check if the remote server has the required tools
  43. sshagent(['ssh-key-10.2.0.10']) {
  44. // Check PHP on remote server
  45. def remotePhp = sh(script: "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'php --version || echo PHP_NOT_FOUND'", returnStdout: true).trim()
  46. echo "Remote PHP: ${remotePhp}"
  47. // Check Composer on remote server
  48. def remoteComposer = sh(script: "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'composer --version || echo COMPOSER_NOT_FOUND'", returnStdout: true).trim()
  49. echo "Remote Composer: ${remoteComposer}"
  50. // Check Node.js on remote server
  51. def remoteNode = sh(script: "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'node --version || echo NODE_NOT_FOUND'", returnStdout: true).trim()
  52. echo "Remote Node.js: ${remoteNode}"
  53. // Check NPM on remote server
  54. def remoteNpm = sh(script: "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'npm --version || echo NPM_NOT_FOUND'", returnStdout: true).trim()
  55. echo "Remote NPM: ${remoteNpm}"
  56. // If any tool is missing, log a warning
  57. if (remotePhp.contains("PHP_NOT_FOUND") || remoteComposer.contains("COMPOSER_NOT_FOUND") ||
  58. remoteNode.contains("NODE_NOT_FOUND") || remoteNpm.contains("NPM_NOT_FOUND")) {
  59. echo "WARNING: Some required tools are missing on the remote server!"
  60. echo "Please install PHP, Composer, Node.js and NPM on ${REMOTE_HOST}"
  61. }
  62. }
  63. }
  64. // Check what's actually available locally (for logging purposes)
  65. sh 'php --version || echo "PHP not available locally"'
  66. sh 'composer --version || echo "Composer not available locally"'
  67. sh 'node --version || echo "Node not available locally"'
  68. sh 'npm --version || echo "NPM not available locally"'
  69. }
  70. }
  71. stage('Get Code') {
  72. steps {
  73. checkout scm
  74. }
  75. }
  76. stage('Install Dependencies') {
  77. steps {
  78. echo "Dependencies will be installed on the target server during deployment"
  79. }
  80. }
  81. stage('Setup Environment') {
  82. steps {
  83. echo "Environment will be set up on the target server during deployment"
  84. }
  85. }
  86. stage('Build Assets') {
  87. steps {
  88. // Skip asset building on Jenkins server
  89. echo "Assets will be built on the target server during deployment"
  90. }
  91. }
  92. stage('Debug') {
  93. steps {
  94. sh 'git branch --show-current'
  95. sh 'git rev-parse --abbrev-ref HEAD'
  96. }
  97. }
  98. stage('Deploy to Server') {
  99. when {
  100. expression {
  101. return env.BRANCH_NAME == 'develop'
  102. }
  103. }
  104. steps {
  105. sshagent(['ssh-key-10.2.0.10']) {
  106. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
  107. sh """
  108. rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' -e 'ssh -o StrictHostKeyChecking=no' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  109. """
  110. sh """
  111. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
  112. composer install --no-interaction --no-dev --prefer-dist && \
  113. npm install && \
  114. npm run build && \
  115. php artisan migrate --force && \
  116. php artisan config:cache && \
  117. php artisan route:cache && \
  118. php artisan view:cache && \
  119. php artisan optimize && \
  120. php artisan storage:link && \
  121. chmod -R 775 storage bootstrap/cache && \
  122. chown -R www-data:www-data .'
  123. """
  124. sh "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\"'"
  125. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo \"Nginx restart failed, may need manual intervention\"'"
  126. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'"
  127. }
  128. }
  129. }
  130. }
  131. post {
  132. always {
  133. cleanWs()
  134. }
  135. success {
  136. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  137. mail to: "${ADMIN_EMAIL}",
  138. subject: 'Polizia - Build Successful',
  139. body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10'
  140. }
  141. failure {
  142. echo 'Build failed! Please check the console output to fix the issues.'
  143. mail to: "${ADMIN_EMAIL}",
  144. subject: 'Polizia - Build Failed',
  145. body: 'The build has failed. Please check Jenkins for details.'
  146. }
  147. }
  148. }