Jenkinsfile 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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('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('Prepare for Deployment') {
  49. steps {
  50. echo "Preparing application for deployment to ${REMOTE_HOST}"
  51. echo "Dependencies will be installed on the target server during deployment"
  52. }
  53. }
  54. stage('Debug') {
  55. steps {
  56. sh 'git branch --show-current || git rev-parse --abbrev-ref HEAD || echo "Cannot determine branch"'
  57. }
  58. }
  59. stage('Deploy to Server') {
  60. when {
  61. expression {
  62. def branch = sh(script: 'git rev-parse --abbrev-ref HEAD || echo unknown', returnStdout: true).trim()
  63. return branch == 'develop' || branch.contains('develop')
  64. }
  65. }
  66. steps {
  67. // Using SSH with identity file from Jenkins home directory
  68. // Make sure to place the SSH key at /var/lib/jenkins/.ssh/id_rsa
  69. sh '''
  70. # Ensure SSH directory exists
  71. mkdir -p ~/.ssh
  72. chmod 700 ~/.ssh
  73. # Configure SSH to skip host key checking
  74. echo "StrictHostKeyChecking no" > ~/.ssh/config
  75. chmod 600 ~/.ssh/config
  76. # Create remote directory
  77. ssh ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'
  78. # Sync files excluding unnecessary ones
  79. rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  80. # Run deployment commands
  81. ssh ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && \\
  82. composer install --no-interaction --no-dev --prefer-dist && \\
  83. npm install && \\
  84. npm run build && \\
  85. php artisan migrate --force && \\
  86. php artisan config:cache && \\
  87. php artisan route:cache && \\
  88. php artisan view:cache && \\
  89. php artisan optimize && \\
  90. php artisan storage:link && \\
  91. chmod -R 775 storage bootstrap/cache && \\
  92. chown -R www-data:www-data ."
  93. # Restart services
  94. ssh ${REMOTE_USER}@${REMOTE_HOST} "sudo systemctl restart php8.1-fpm || sudo systemctl restart php-fpm || echo 'PHP service restart failed, may need manual intervention'"
  95. ssh ${REMOTE_USER}@${REMOTE_HOST} "sudo systemctl restart nginx || echo 'Nginx restart failed, may need manual intervention'"
  96. # Clear cache
  97. ssh ${REMOTE_USER}@${REMOTE_HOST} "cd ${REMOTE_DIR} && php artisan cache:clear"
  98. '''
  99. }
  100. }
  101. }
  102. post {
  103. always {
  104. script {
  105. cleanWs()
  106. }
  107. }
  108. success {
  109. echo 'Build successful! The Polizia application is now deployed to 10.2.0.10'
  110. script {
  111. mail to: env.ADMIN_EMAIL,
  112. subject: 'Polizia - Build Successful',
  113. body: 'The build was successful and the Polizia application has been deployed to the server at 10.2.0.10'
  114. }
  115. }
  116. failure {
  117. echo 'Build failed! Please check the console output to fix the issues.'
  118. script {
  119. mail to: env.ADMIN_EMAIL,
  120. subject: 'Polizia - Build Failed',
  121. body: 'The build has failed. Please check Jenkins for details.'
  122. }
  123. }
  124. }
  125. }