Jenkinsfile 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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('Verify Host') {
  29. steps {
  30. sh 'hostname'
  31. sh 'hostname -I'
  32. sh 'whoami'
  33. }
  34. }
  35. stage('Setup') {
  36. steps {
  37. sh 'php --version || echo "PHP not available"'
  38. sh 'composer --version || echo "Composer not available"'
  39. sh 'node --version || echo "Node not available"'
  40. sh 'npm --version || echo "NPM not available"'
  41. }
  42. }
  43. stage('Get Code') {
  44. steps {
  45. checkout scm
  46. }
  47. }
  48. stage('Install Dependencies') {
  49. steps {
  50. sh 'composer install --no-interaction --no-progress --prefer-dist || echo "Skipping composer install"'
  51. }
  52. }
  53. stage('Setup Environment') {
  54. steps {
  55. sh 'php artisan key:generate || echo "Skipping key generation"'
  56. sh 'php artisan config:clear || echo "Skipping config clear"'
  57. sh 'php artisan cache:clear || echo "Skipping cache clear"'
  58. }
  59. }
  60. stage('Build Assets') {
  61. steps {
  62. sh 'npm install || echo "Skipping npm install"'
  63. sh 'npm run build || echo "Skipping npm build"'
  64. }
  65. }
  66. stage('Debug') {
  67. steps {
  68. sh 'git branch --show-current'
  69. sh 'git rev-parse --abbrev-ref HEAD'
  70. }
  71. }
  72. stage('Deploy to Server') {
  73. when {
  74. expression {
  75. return env.BRANCH_NAME == 'develop'
  76. }
  77. }
  78. steps {
  79. sshagent(['ssh-key-10.2.0.10']) {
  80. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
  81. sh """
  82. rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' -e 'ssh -o StrictHostKeyChecking=no' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  83. """
  84. sh """
  85. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
  86. composer install --no-interaction --no-dev --prefer-dist && \
  87. npm install && \
  88. npm run build && \
  89. php artisan migrate --force && \
  90. php artisan config:cache && \
  91. php artisan route:cache && \
  92. php artisan view:cache && \
  93. php artisan optimize && \
  94. php artisan storage:link && \
  95. chmod -R 775 storage bootstrap/cache && \
  96. chown -R www-data:www-data .'
  97. """
  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. sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'"
  101. }
  102. }
  103. }
  104. }
  105. post {
  106. always {
  107. node('any') {
  108. cleanWs()
  109. }
  110. }
  111. success {
  112. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  113. mail to: "${ADMIN_EMAIL}",
  114. subject: 'Polizia - Build Successful',
  115. body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10'
  116. }
  117. failure {
  118. echo 'Build failed! Please check the console output to fix the issues.'
  119. mail to: "${ADMIN_EMAIL}",
  120. subject: 'Polizia - Build Failed',
  121. body: 'The build has failed. Please check Jenkins for details.'
  122. }
  123. }
  124. }