Jenkinsfile 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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('Setup') {
  29. stage('Verify Host') {
  30. steps {
  31. sh 'hostname'
  32. sh 'hostname -I'
  33. sh 'whoami'
  34. }
  35. }
  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('Deploy to Server') {
  68. when {
  69. anyOf {
  70. branch 'develop'
  71. }
  72. }
  73. steps {
  74. sshagent(['ssh-key-10.2.0.10']) {
  75. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
  76. sh """
  77. rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' -e 'ssh -o StrictHostKeyChecking=no' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  78. """
  79. sh """
  80. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
  81. composer install --no-interaction --no-dev --prefer-dist && \
  82. npm install && \
  83. npm run build && \
  84. php artisan migrate --force && \
  85. php artisan config:cache && \
  86. php artisan route:cache && \
  87. php artisan view:cache && \
  88. php artisan optimize && \
  89. php artisan storage:link && \
  90. chmod -R 775 storage bootstrap/cache && \
  91. chown -R www-data:www-data .'
  92. """
  93. // Restart relevant services if needed
  94. 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\"'"
  95. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo \"Nginx restart failed, may need manual intervention\"'"
  96. // Clear any caches
  97. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'"
  98. }
  99. }
  100. }
  101. }
  102. post {
  103. always {
  104. node('any') {
  105. cleanWs()
  106. }
  107. }
  108. success {
  109. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  110. mail to: "${ADMIN_EMAIL}",
  111. subject: 'Polizia - Build Successful',
  112. body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10'
  113. }
  114. failure {
  115. echo 'Build failed! Please check the console output to fix the issues.'
  116. mail to: "${ADMIN_EMAIL}",
  117. subject: 'Polizia - Build Failed',
  118. body: 'The build has failed. Please check Jenkins for details.'
  119. }
  120. }
  121. }