Jenkinsfile 5.4 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. // PHP version to install if needed
  27. PHP_VERSION = '8.1'
  28. // Node version to install if needed
  29. NODE_VERSION = '18.x'
  30. // Use Jenkins credentials
  31. REMOTE_CREDS = credentials('deploy-server-credentials')
  32. }
  33. stages {
  34. stage('Verify Host') {
  35. steps {
  36. sh 'hostname'
  37. sh 'hostname -I'
  38. sh 'whoami'
  39. }
  40. }
  41. stage('Check Dependencies') {
  42. steps {
  43. // Check what's available locally (for information only)
  44. sh 'php --version || echo "PHP not available locally"'
  45. sh 'composer --version || echo "Composer not available locally"'
  46. sh 'node --version || echo "Node not available locally"'
  47. sh 'npm --version || echo "NPM not available locally"'
  48. }
  49. }
  50. stage('Get Code') {
  51. steps {
  52. checkout scm
  53. }
  54. }
  55. stage('Prepare for Deployment') {
  56. steps {
  57. // Just log what we're doing
  58. echo "Preparing application for deployment to ${REMOTE_HOST}"
  59. // No need to run composer/npm locally since we'll do it on the remote server
  60. echo "Dependencies will be installed on the target server during deployment"
  61. }
  62. }
  63. stage('Debug') {
  64. steps {
  65. sh 'git branch --show-current || git rev-parse --abbrev-ref HEAD || echo "Cannot determine branch"'
  66. }
  67. }
  68. stage('Deploy to Server') {
  69. when {
  70. expression {
  71. return env.BRANCH_NAME == 'develop' || env.GIT_BRANCH == 'origin/develop'
  72. }
  73. }
  74. steps {
  75. // Using withCredentials instead of sshagent
  76. withCredentials([sshUserPrivateKey(credentialsId: 'deploy-server-credentials', keyFileVariable: 'SSH_KEY')]) {
  77. // Set up SSH options with the private key
  78. sh """
  79. mkdir -p ~/.ssh
  80. echo "StrictHostKeyChecking no" > ~/.ssh/config
  81. # Create remote directory
  82. ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'
  83. # Sync files
  84. rsync -avz -e "ssh -i ${SSH_KEY}" --exclude '.git' --exclude 'node_modules' --exclude 'vendor' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
  85. # Run deployment commands
  86. ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
  87. composer install --no-interaction --no-dev --prefer-dist && \
  88. npm install && \
  89. npm run build && \
  90. php artisan migrate --force && \
  91. php artisan config:cache && \
  92. php artisan route:cache && \
  93. php artisan view:cache && \
  94. php artisan optimize && \
  95. php artisan storage:link && \
  96. chmod -R 775 storage bootstrap/cache && \
  97. chown -R www-data:www-data .'
  98. # Restart services
  99. ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart php8.1-fpm || sudo systemctl restart php-fpm || echo "PHP service restart failed, may need manual intervention"'
  100. ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo "Nginx restart failed, may need manual intervention"'
  101. # Clear cache
  102. ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'
  103. """
  104. }
  105. }
  106. }
  107. }
  108. post {
  109. always {
  110. cleanWs()
  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. }