Jenkinsfile 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // PHP version to install if needed
  27. PHP_VERSION = '8.1'
  28. // Node version to install if needed
  29. NODE_VERSION = '18.x'
  30. }
  31. stages {
  32. stage('Verify Host') {
  33. steps {
  34. sh 'hostname'
  35. sh 'hostname -I'
  36. sh 'whoami'
  37. }
  38. }
  39. stage('Setup Tools') {
  40. steps {
  41. script {
  42. // Check and install PHP if not available
  43. def phpInstalled = sh(script: 'php --version', returnStatus: true)
  44. if (phpInstalled != 0) {
  45. echo "Installing PHP ${PHP_VERSION}..."
  46. sh """
  47. sudo apt-get update
  48. sudo apt-get install -y software-properties-common
  49. sudo add-apt-repository -y ppa:ondrej/php
  50. sudo apt-get update
  51. sudo apt-get install -y php${PHP_VERSION} php${PHP_VERSION}-cli php${PHP_VERSION}-common php${PHP_VERSION}-curl php${PHP_VERSION}-mbstring php${PHP_VERSION}-mysql php${PHP_VERSION}-xml php${PHP_VERSION}-zip
  52. """
  53. }
  54. // Check and install Composer if not available
  55. def composerInstalled = sh(script: 'composer --version', returnStatus: true)
  56. if (composerInstalled != 0) {
  57. echo "Installing Composer..."
  58. sh """
  59. php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  60. php composer-setup.php --install-dir=/usr/local/bin --filename=composer
  61. php -r "unlink('composer-setup.php');"
  62. chmod +x /usr/local/bin/composer
  63. """
  64. }
  65. // Check and install Node.js and NPM if not available
  66. def nodeInstalled = sh(script: 'node --version', returnStatus: true)
  67. if (nodeInstalled != 0) {
  68. echo "Installing Node.js ${NODE_VERSION}..."
  69. sh """
  70. curl -sL https://deb.nodesource.com/setup_${NODE_VERSION} | sudo -E bash -
  71. sudo apt-get install -y nodejs
  72. """
  73. }
  74. }
  75. // Verify installations
  76. sh 'php --version'
  77. sh 'composer --version'
  78. sh 'node --version'
  79. sh 'npm --version'
  80. }
  81. }
  82. stage('Get Code') {
  83. steps {
  84. checkout scm
  85. }
  86. }
  87. stage('Install Dependencies') {
  88. steps {
  89. sh 'composer install --no-interaction --no-progress --prefer-dist'
  90. }
  91. }
  92. stage('Setup Environment') {
  93. steps {
  94. sh 'php artisan key:generate || echo "Skipping key generation"'
  95. sh 'php artisan config:clear'
  96. sh 'php artisan cache:clear'
  97. }
  98. }
  99. stage('Build Assets') {
  100. steps {
  101. sh 'npm install'
  102. sh 'npm run build'
  103. }
  104. }
  105. stage('Debug') {
  106. steps {
  107. sh 'git branch --show-current'
  108. sh 'git rev-parse --abbrev-ref HEAD'
  109. }
  110. }
  111. stage('Deploy to Server') {
  112. when {
  113. expression {
  114. return env.BRANCH_NAME == 'develop'
  115. }
  116. }
  117. steps {
  118. sshagent(['ssh-key-10.2.0.10']) {
  119. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
  120. sh """
  121. rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' -e 'ssh -o StrictHostKeyChecking=no' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  122. """
  123. sh """
  124. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
  125. composer install --no-interaction --no-dev --prefer-dist && \
  126. npm install && \
  127. npm run build && \
  128. php artisan migrate --force && \
  129. php artisan config:cache && \
  130. php artisan route:cache && \
  131. php artisan view:cache && \
  132. php artisan optimize && \
  133. php artisan storage:link && \
  134. chmod -R 775 storage bootstrap/cache && \
  135. chown -R www-data:www-data .'
  136. """
  137. 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\"'"
  138. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo \"Nginx restart failed, may need manual intervention\"'"
  139. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'"
  140. }
  141. }
  142. }
  143. }
  144. post {
  145. always {
  146. node('any') {
  147. cleanWs()
  148. }
  149. }
  150. success {
  151. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  152. mail to: "${ADMIN_EMAIL}",
  153. subject: 'Polizia - Build Successful',
  154. body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10'
  155. }
  156. failure {
  157. echo 'Build failed! Please check the console output to fix the issues.'
  158. mail to: "${ADMIN_EMAIL}",
  159. subject: 'Polizia - Build Failed',
  160. body: 'The build has failed. Please check Jenkins for details.'
  161. }
  162. }
  163. }