Jenkinsfile 5.6 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 = 'fratini'
  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('Check Dependencies') {
  36. steps {
  37. sh 'php --version || echo "PHP not available locally"'
  38. sh 'composer --version || echo "Composer not available locally"'
  39. sh 'node --version || echo "Node not available locally"'
  40. sh 'npm --version || echo "NPM not available locally"'
  41. }
  42. }
  43. stage('Get Code') {
  44. steps {
  45. checkout scm
  46. }
  47. }
  48. stage('Debug Branch Info') {
  49. steps {
  50. sh 'git branch -v || echo "Could not get branch info"'
  51. sh 'git status || echo "Could not get status"'
  52. sh 'echo "BRANCH_NAME: ${BRANCH_NAME:-not set}"'
  53. sh 'echo "GIT_BRANCH: ${GIT_BRANCH:-not set}"'
  54. script {
  55. // Store the branch info for later use
  56. env.CURRENT_BRANCH = sh(script: 'git rev-parse --abbrev-ref HEAD || echo "HEAD"', returnStdout: true).trim()
  57. echo "Current branch detected as: ${env.CURRENT_BRANCH}"
  58. }
  59. }
  60. }
  61. stage('Deploy to Server') {
  62. when {
  63. expression {
  64. // Always deploy from develop branch or if we're in a detached HEAD state
  65. return env.CURRENT_BRANCH == 'HEAD' || env.CURRENT_BRANCH == 'develop' ||
  66. env.GIT_BRANCH == 'origin/develop' || env.BRANCH_NAME == 'develop'
  67. }
  68. }
  69. steps {
  70. echo "Starting deployment to ${REMOTE_HOST} as user ${REMOTE_USER}"
  71. // Using SSH key authentication - no password required
  72. sh '''
  73. # Make sure SSH directory exists with correct permissions
  74. mkdir -p ~/.ssh
  75. chmod 700 ~/.ssh
  76. # Create remote directory
  77. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "mkdir -p ${REMOTE_DIR}"
  78. # Sync files excluding unnecessary ones
  79. rsync -avz -e "ssh -o StrictHostKeyChecking=no" --exclude '.git' --exclude 'node_modules' --exclude 'vendor' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  80. # Run composer install
  81. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && composer install --no-interaction --no-dev --prefer-dist"
  82. # Run npm install and build
  83. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && npm install && npm run build"
  84. # Run Laravel artisan commands
  85. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan migrate --force && php artisan config:cache && php artisan route:cache && php artisan view:cache && php artisan optimize && php artisan storage:link"
  86. # Set permissions
  87. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && chmod -R 775 storage bootstrap/cache && sudo chown -R www-data:www-data ."
  88. # Restart services
  89. 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'"
  90. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "sudo systemctl restart nginx || echo 'Nginx restart failed, may need manual intervention'"
  91. # Clear cache
  92. ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan cache:clear"
  93. '''
  94. }
  95. }
  96. }
  97. post {
  98. always {
  99. script {
  100. cleanWs()
  101. }
  102. }
  103. success {
  104. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  105. script {
  106. mail to: env.ADMIN_EMAIL,
  107. subject: 'Polizia - Build Successful',
  108. body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10'
  109. }
  110. }
  111. failure {
  112. echo 'Build failed! Please check the console output to fix the issues.'
  113. script {
  114. mail to: env.ADMIN_EMAIL,
  115. subject: 'Polizia - Build Failed',
  116. body: 'The build has failed. Please check Jenkins for details.'
  117. }
  118. }
  119. }
  120. }