Jenkinsfile 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. }
  27. stages {
  28. stage('Verify Host') {
  29. steps {
  30. sh 'hostname'
  31. sh 'hostname -I'
  32. sh 'whoami'
  33. }
  34. }
  35. stage('Setup') {
  36. steps {
  37. // Check versions if available
  38. sh '/usr/bin/php --version || echo "PHP not available"'
  39. sh 'composer --version || echo "Composer not available"'
  40. sh 'node --version || echo "Node not available"'
  41. sh 'npm --version || echo "NPM not available"'
  42. }
  43. }
  44. stage('Get Code') {
  45. steps {
  46. checkout scm
  47. }
  48. }
  49. stage('Install Dependencies') {
  50. steps {
  51. sh 'composer install --no-interaction --no-progress --prefer-dist || echo "Skipping composer install"'
  52. }
  53. }
  54. stage('Setup Environment') {
  55. steps {
  56. sh '/usr/bin/php artisan key:generate || echo "Skipping key generation"'
  57. sh '/usr/bin/php artisan config:clear || echo "Skipping config clear"'
  58. sh '/usr/bin/php artisan cache:clear || echo "Skipping cache clear"'
  59. }
  60. }
  61. stage('Build Assets') {
  62. steps {
  63. sh 'npm install || echo "Skipping npm install"'
  64. sh 'npm run build || echo "Skipping npm build"'
  65. }
  66. }
  67. stage('Debug') {
  68. steps {
  69. sh 'git branch --show-current'
  70. sh 'git rev-parse --abbrev-ref HEAD'
  71. }
  72. }
  73. stage('Deploy to Server') {
  74. when {
  75. anyOf {
  76. branch 'develop'
  77. }
  78. }
  79. steps {
  80. sshagent(['ssh-key-10.2.0.10']) {
  81. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
  82. sh """
  83. rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' -e 'ssh -o StrictHostKeyChecking=no' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  84. """
  85. sh """
  86. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
  87. composer install --no-interaction --no-dev --prefer-dist && \
  88. npm install && \
  89. npm run build && \
  90. php artisan migrate --force && \
  91. php artisan config:cache && \
  92. php artisan route:cache && \
  93. php artisan view:cache && \
  94. php artisan optimize && \
  95. php artisan storage:link && \
  96. chmod -R 775 storage bootstrap/cache && \
  97. chown -R www-data:www-data .'
  98. """
  99. // Restart relevant services if needed
  100. 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\"'"
  101. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo \"Nginx restart failed, may need manual intervention\"'"
  102. // Clear any caches
  103. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'"
  104. }
  105. }
  106. }
  107. }
  108. post {
  109. always {
  110. node('any') {
  111. cleanWs()
  112. }
  113. }
  114. success {
  115. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  116. mail to: "${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. failure {
  121. echo 'Build failed! Please check the console output to fix the issues.'
  122. mail to: "${ADMIN_EMAIL}",
  123. subject: 'Polizia - Build Failed',
  124. body: 'The build has failed. Please check Jenkins for details.'
  125. }
  126. }
  127. }