Jenkisfile 5.6 KB

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