Jenkinsfile 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. pipeline {
  2. agent any
  3. environment {
  4. APP_NAME = 'Polizia'
  5. APP_ENV = 'testing'
  6. APP_KEY = credentials('APP_KEY')
  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 = credentials('DB_USERNAME')
  17. DB_PASSWORD = credentials('DB_PASSWORD')
  18. BROADCAST_DRIVER = 'log'
  19. CACHE_DRIVER = 'file'
  20. FILESYSTEM_DISK = 'local'
  21. QUEUE_CONNECTION = 'sync'
  22. SESSION_DRIVER = 'file'
  23. SESSION_LIFETIME = '120'
  24. MAIL_MAILER = 'smtp'
  25. MAIL_HOST = 'mailpit'
  26. MAIL_PORT = '1025'
  27. MAIL_USERNAME = 'null'
  28. MAIL_PASSWORD = 'null'
  29. MAIL_ENCRYPTION = 'null'
  30. MAIL_FROM_ADDRESS = 'hello@example.com'
  31. MAIL_FROM_NAME = 'Polizia'
  32. MCTC_URL = 'https://www.ilportaledellautomobilista.it/Info-ws/services'
  33. MCTC_USER = credentials('MCTC_USER')
  34. MCTC_PASSWORD = credentials('MCTC_PASSWORD')
  35. STORAGE_PATH = 'app/public/'
  36. REMOTE_HOST = '10.2.0.10'
  37. REMOTE_USER = credentials('REMOTE_USER')
  38. REMOTE_DIR = '/var/www/html/polizia'
  39. }
  40. stages {
  41. stage('Setup') {
  42. steps {
  43. sh 'which php || echo "PHP not found in PATH"'
  44. sh 'which composer || echo "Composer not found in PATH"'
  45. sh 'which node || echo "Node not found in PATH"'
  46. sh 'which npm || echo "NPM not found in PATH"'
  47. // Check versions if available
  48. sh 'php --version || echo "PHP not available"'
  49. sh 'composer --version || echo "Composer not available"'
  50. sh 'node --version || echo "Node not available"'
  51. sh 'npm --version || echo "NPM not available"'
  52. }
  53. }
  54. stage('Get Code') {
  55. steps {
  56. checkout scm
  57. }
  58. }
  59. stage('Install Dependencies') {
  60. steps {
  61. sh 'composer install --no-interaction --no-progress --prefer-dist || echo "Skipping composer install"'
  62. }
  63. }
  64. stage('Setup Environment') {
  65. steps {
  66. sh 'cp .env.example .env'
  67. sh 'php artisan key:generate'
  68. sh 'php artisan config:clear'
  69. sh 'php artisan cache:clear'
  70. }
  71. }
  72. stage('Run Tests') {
  73. steps {
  74. sh 'php artisan config:cache'
  75. sh 'php artisan test --testsuite=Feature --testsuite=Unit || true'
  76. sh 'php artisan config:clear'
  77. }
  78. }
  79. stage('Build Assets') {
  80. steps {
  81. sh 'npm install || echo "Skipping npm install"'
  82. sh 'npm run build || echo "Skipping npm build"'
  83. }
  84. }
  85. stage('Deploy to Server') {
  86. when {
  87. anyOf {
  88. branch 'develop'
  89. }
  90. }
  91. steps {
  92. sshagent(['ssh-key-10.2.0.10']) {
  93. // Ensure the remote directory exists
  94. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
  95. // Use rsync to transfer files
  96. sh """
  97. rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' -e 'ssh -o StrictHostKeyChecking=no' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  98. """
  99. // Run setup commands on remote server
  100. sh """
  101. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
  102. composer install --no-interaction --no-dev --prefer-dist && \
  103. npm install && \
  104. npm run build && \
  105. php artisan migrate --force && \
  106. php artisan config:cache && \
  107. php artisan route:cache && \
  108. php artisan view:cache && \
  109. php artisan optimize && \
  110. php artisan storage:link && \
  111. chmod -R 775 storage bootstrap/cache && \
  112. chown -R www-data:www-data .'
  113. """
  114. // Restart relevant services if needed
  115. 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\"'"
  116. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo \"Nginx restart failed, may need manual intervention\"'"
  117. // Clear any caches
  118. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'"
  119. }
  120. }
  121. }
  122. }
  123. post {
  124. always {
  125. cleanWs()
  126. }
  127. success {
  128. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  129. mail to: credentials('ADMIN_EMAIL'), subject: 'Polizia - Build Successful', body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10'
  130. }
  131. failure {
  132. echo 'Build failed! Please check the console output to fix the issues.'
  133. mail to: credentials('ADMIN_EMAIL'), subject: 'Polizia - Build Failed', body: 'The build has failed. Please check Jenkins for details.'
  134. }
  135. }
  136. }