Jenkinsfile 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. steps {
  30. // Check versions if available
  31. sh 'php --version || echo "PHP not available"'
  32. sh 'composer --version || echo "Composer not available"'
  33. sh 'node --version || echo "Node not available"'
  34. sh 'npm --version || echo "NPM not available"'
  35. }
  36. }
  37. stage('Get Code') {
  38. steps {
  39. checkout scm
  40. }
  41. }
  42. stage('Install Dependencies') {
  43. steps {
  44. sh 'composer install --no-interaction --no-progress --prefer-dist || echo "Skipping composer install"'
  45. }
  46. }
  47. stage('Setup Environment') {
  48. steps {
  49. sh 'cp .env.example .env || echo "No .env.example file found"'
  50. sh 'php artisan key:generate || echo "Skipping key generation"'
  51. sh 'php artisan config:clear || echo "Skipping config clear"'
  52. sh 'php artisan cache:clear || echo "Skipping cache clear"'
  53. }
  54. }
  55. stage('Run Tests') {
  56. steps {
  57. sh 'php artisan config:cache || echo "Skipping config cache"'
  58. sh 'php artisan test --testsuite=Feature --testsuite=Unit || echo "Skipping tests"'
  59. sh 'php artisan config:clear || echo "Skipping config clear"'
  60. }
  61. }
  62. stage('Build Assets') {
  63. steps {
  64. sh 'npm install || echo "Skipping npm install"'
  65. sh 'npm run build || echo "Skipping npm build"'
  66. }
  67. }
  68. stage('Deploy to Server') {
  69. when {
  70. anyOf {
  71. branch 'develop'
  72. }
  73. }
  74. steps {
  75. sshagent(['ssh-key-10.2.0.10']) {
  76. // Ensure the remote directory exists
  77. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
  78. // Use rsync to transfer files
  79. sh """
  80. rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' -e 'ssh -o StrictHostKeyChecking=no' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  81. """
  82. // Run setup commands on remote server
  83. sh """
  84. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
  85. composer install --no-interaction --no-dev --prefer-dist && \
  86. npm install && \
  87. npm run build && \
  88. php artisan migrate --force && \
  89. php artisan config:cache && \
  90. php artisan route:cache && \
  91. php artisan view:cache && \
  92. php artisan optimize && \
  93. php artisan storage:link && \
  94. chmod -R 775 storage bootstrap/cache && \
  95. chown -R www-data:www-data .'
  96. """
  97. // Restart relevant services if needed
  98. 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\"'"
  99. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo \"Nginx restart failed, may need manual intervention\"'"
  100. // Clear any caches
  101. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'"
  102. }
  103. }
  104. }
  105. }
  106. post {
  107. always {
  108. node {
  109. cleanWs()
  110. }
  111. }
  112. success {
  113. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  114. mail to: "${ADMIN_EMAIL}",
  115. subject: 'Polizia - Build Successful',
  116. body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10'
  117. }
  118. failure {
  119. echo 'Build failed! Please check the console output to fix the issues.'
  120. mail to: "${ADMIN_EMAIL}",
  121. subject: 'Polizia - Build Failed',
  122. body: 'The build has failed. Please check Jenkins for details.'
  123. }
  124. }
  125. }