Jenkinsfile 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. pipeline {
  2. agent any
  3. tools {
  4. php 'PHP 8.2'
  5. nodejs 'NodeJS 18'
  6. }
  7. environment {
  8. APP_NAME = 'Polizia'
  9. APP_ENV = 'testing'
  10. APP_KEY = credentials('APP_KEY')
  11. APP_DEBUG = 'false'
  12. APP_URL = 'https://frascati.dev.webmagistri.biz'
  13. LOG_CHANNEL = 'stack'
  14. LOG_DEPRECATIONS_CHANNEL = 'null'
  15. LOG_LEVEL = 'debug'
  16. DB_CONNECTION = 'mysql'
  17. DB_HOST = '127.0.0.1'
  18. DB_PORT = '3306'
  19. DB_DATABASE = 'polizia_online'
  20. DB_USERNAME = credentials('DB_USERNAME')
  21. DB_PASSWORD = credentials('DB_PASSWORD')
  22. BROADCAST_DRIVER = 'log'
  23. CACHE_DRIVER = 'file'
  24. FILESYSTEM_DISK = 'local'
  25. QUEUE_CONNECTION = 'sync'
  26. SESSION_DRIVER = 'file'
  27. SESSION_LIFETIME = '120'
  28. MAIL_MAILER = 'smtp'
  29. MAIL_HOST = 'mailpit'
  30. MAIL_PORT = '1025'
  31. MAIL_USERNAME = 'null'
  32. MAIL_PASSWORD = 'null'
  33. MAIL_ENCRYPTION = 'null'
  34. MAIL_FROM_ADDRESS = 'hello@example.com'
  35. MAIL_FROM_NAME = 'Polizia'
  36. MCTC_URL = 'https://www.ilportaledellautomobilista.it/Info-ws/services'
  37. MCTC_USER = credentials('MCTC_USER')
  38. MCTC_PASSWORD = credentials('MCTC_PASSWORD')
  39. STORAGE_PATH = 'app/public/'
  40. REMOTE_HOST = '10.2.0.10'
  41. REMOTE_USER = credentials('REMOTE_USER')
  42. REMOTE_DIR = '/var/www/html/polizia'
  43. }
  44. stages {
  45. stage('Setup') {
  46. steps {
  47. sh 'php -v'
  48. sh 'node -v'
  49. sh 'npm -v'
  50. // Install composer if not available
  51. sh '''
  52. if ! command -v composer &> /dev/null; then
  53. curl -sS https://getcomposer.org/installer | php
  54. mv composer.phar /usr/local/bin/composer || sudo mv composer.phar /usr/local/bin/composer
  55. fi
  56. '''
  57. sh 'composer --version'
  58. }
  59. }
  60. stage('Get Code') {
  61. steps {
  62. checkout scm
  63. }
  64. }
  65. stage('Install Dependencies') {
  66. steps {
  67. sh 'composer install --no-interaction --no-progress --prefer-dist'
  68. }
  69. }
  70. stage('Setup Environment') {
  71. steps {
  72. sh 'cp .env.example .env'
  73. sh 'php artisan key:generate'
  74. sh 'php artisan config:clear'
  75. sh 'php artisan cache:clear'
  76. }
  77. }
  78. stage('Run Tests') {
  79. steps {
  80. sh 'php artisan config:cache'
  81. sh 'php artisan test --testsuite=Feature --testsuite=Unit || true'
  82. sh 'php artisan config:clear'
  83. }
  84. }
  85. stage('Build Assets') {
  86. steps {
  87. sh 'npm install'
  88. sh 'npm run build'
  89. }
  90. }
  91. stage('Deploy to Server') {
  92. when {
  93. anyOf {
  94. branch 'master'
  95. branch 'main'
  96. }
  97. }
  98. steps {
  99. sshagent(['ssh-key-10.2.0.10']) {
  100. // Ensure the remote directory exists
  101. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
  102. // Create a tar archive of the project
  103. sh 'tar -czf project.tar.gz --exclude=node_modules --exclude=vendor --exclude=.git .'
  104. // Transfer the archive to remote server
  105. sh "scp -o StrictHostKeyChecking=no project.tar.gz ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}"
  106. // Extract on remote server and setup
  107. sh """
  108. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
  109. tar -xzf project.tar.gz && \
  110. rm project.tar.gz && \
  111. composer install --no-interaction --no-dev --prefer-dist && \
  112. npm install && \
  113. npm run build && \
  114. php artisan migrate --force && \
  115. php artisan config:cache && \
  116. php artisan route:cache && \
  117. php artisan view:cache && \
  118. php artisan optimize && \
  119. php artisan storage:link && \
  120. chown -R www-data:www-data .'
  121. """
  122. // Restart relevant services if needed
  123. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart php8.1-fpm || sudo systemctl restart php-fpm'"
  124. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx'"
  125. // Clear any caches
  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: 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'
  138. }
  139. failure {
  140. echo 'Build failed! Please check the console output to fix the issues.'
  141. mail to: credentials('ADMIN_EMAIL'), subject: 'Polizia - Build Failed', body: 'The build has failed. Please check Jenkins for details.'
  142. }
  143. }
  144. }