|
|
@@ -35,6 +35,9 @@ pipeline {
|
|
|
PHP_VERSION = '8.1'
|
|
|
// Node version to install if needed
|
|
|
NODE_VERSION = '18.x'
|
|
|
+
|
|
|
+ // Use Jenkins credentials
|
|
|
+ REMOTE_CREDS = credentials('deploy-server-credentials')
|
|
|
}
|
|
|
|
|
|
stages {
|
|
|
@@ -46,37 +49,9 @@ pipeline {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- stage('Check Remote Tools') {
|
|
|
+ stage('Check Dependencies') {
|
|
|
steps {
|
|
|
- script {
|
|
|
- // Check if the remote server has the required tools
|
|
|
- sshagent(['ssh-key-10.2.0.10']) {
|
|
|
- // Check PHP on remote server
|
|
|
- def remotePhp = sh(script: "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'php --version || echo PHP_NOT_FOUND'", returnStdout: true).trim()
|
|
|
- echo "Remote PHP: ${remotePhp}"
|
|
|
-
|
|
|
- // Check Composer on remote server
|
|
|
- def remoteComposer = sh(script: "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'composer --version || echo COMPOSER_NOT_FOUND'", returnStdout: true).trim()
|
|
|
- echo "Remote Composer: ${remoteComposer}"
|
|
|
-
|
|
|
- // Check Node.js on remote server
|
|
|
- def remoteNode = sh(script: "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'node --version || echo NODE_NOT_FOUND'", returnStdout: true).trim()
|
|
|
- echo "Remote Node.js: ${remoteNode}"
|
|
|
-
|
|
|
- // Check NPM on remote server
|
|
|
- def remoteNpm = sh(script: "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'npm --version || echo NPM_NOT_FOUND'", returnStdout: true).trim()
|
|
|
- echo "Remote NPM: ${remoteNpm}"
|
|
|
-
|
|
|
- // If any tool is missing, log a warning
|
|
|
- if (remotePhp.contains("PHP_NOT_FOUND") || remoteComposer.contains("COMPOSER_NOT_FOUND") ||
|
|
|
- remoteNode.contains("NODE_NOT_FOUND") || remoteNpm.contains("NPM_NOT_FOUND")) {
|
|
|
- echo "WARNING: Some required tools are missing on the remote server!"
|
|
|
- echo "Please install PHP, Composer, Node.js and NPM on ${REMOTE_HOST}"
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Check what's actually available locally (for logging purposes)
|
|
|
+ // Check what's available locally (for information only)
|
|
|
sh 'php --version || echo "PHP not available locally"'
|
|
|
sh 'composer --version || echo "Composer not available locally"'
|
|
|
sh 'node --version || echo "Node not available locally"'
|
|
|
@@ -90,48 +65,44 @@ pipeline {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- stage('Install Dependencies') {
|
|
|
- steps {
|
|
|
- echo "Dependencies will be installed on the target server during deployment"
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- stage('Setup Environment') {
|
|
|
+ stage('Prepare for Deployment') {
|
|
|
steps {
|
|
|
- echo "Environment will be set up on the target server during deployment"
|
|
|
- }
|
|
|
- }
|
|
|
+ // Just log what we're doing
|
|
|
+ echo "Preparing application for deployment to ${REMOTE_HOST}"
|
|
|
|
|
|
- stage('Build Assets') {
|
|
|
- steps {
|
|
|
- // Skip asset building on Jenkins server
|
|
|
- echo "Assets will be built on the target server during deployment"
|
|
|
+ // No need to run composer/npm locally since we'll do it on the remote server
|
|
|
+ echo "Dependencies will be installed on the target server during deployment"
|
|
|
}
|
|
|
}
|
|
|
|
|
|
stage('Debug') {
|
|
|
steps {
|
|
|
- sh 'git branch --show-current'
|
|
|
- sh 'git rev-parse --abbrev-ref HEAD'
|
|
|
+ sh 'git branch --show-current || git rev-parse --abbrev-ref HEAD || echo "Cannot determine branch"'
|
|
|
}
|
|
|
}
|
|
|
|
|
|
stage('Deploy to Server') {
|
|
|
when {
|
|
|
expression {
|
|
|
- return env.BRANCH_NAME == 'develop'
|
|
|
+ return env.BRANCH_NAME == 'develop' || env.GIT_BRANCH == 'origin/develop'
|
|
|
}
|
|
|
}
|
|
|
steps {
|
|
|
- sshagent(['ssh-key-10.2.0.10']) {
|
|
|
- sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'"
|
|
|
-
|
|
|
+ // Using withCredentials instead of sshagent
|
|
|
+ withCredentials([sshUserPrivateKey(credentialsId: 'deploy-server-credentials', keyFileVariable: 'SSH_KEY')]) {
|
|
|
+ // Set up SSH options with the private key
|
|
|
sh """
|
|
|
- rsync -avz --exclude '.git' --exclude 'node_modules' --exclude 'vendor' -e 'ssh -o StrictHostKeyChecking=no' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
|
|
|
- """
|
|
|
+ mkdir -p ~/.ssh
|
|
|
+ echo "StrictHostKeyChecking no" > ~/.ssh/config
|
|
|
|
|
|
- sh """
|
|
|
- ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
|
|
|
+ # Create remote directory
|
|
|
+ ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'mkdir -p ${REMOTE_DIR}'
|
|
|
+
|
|
|
+ # Sync files
|
|
|
+ rsync -avz -e "ssh -i ${SSH_KEY}" --exclude '.git' --exclude 'node_modules' --exclude 'vendor' ./ ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
|
|
|
+
|
|
|
+ # Run deployment commands
|
|
|
+ ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && \
|
|
|
composer install --no-interaction --no-dev --prefer-dist && \
|
|
|
npm install && \
|
|
|
npm run build && \
|
|
|
@@ -143,12 +114,14 @@ pipeline {
|
|
|
php artisan storage:link && \
|
|
|
chmod -R 775 storage bootstrap/cache && \
|
|
|
chown -R www-data:www-data .'
|
|
|
- """
|
|
|
|
|
|
- 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\"'"
|
|
|
- sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo \"Nginx restart failed, may need manual intervention\"'"
|
|
|
+ # Restart services
|
|
|
+ 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"'
|
|
|
+ ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'sudo systemctl restart nginx || echo "Nginx restart failed, may need manual intervention"'
|
|
|
|
|
|
- sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'"
|
|
|
+ # Clear cache
|
|
|
+ ssh -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_DIR} && php artisan cache:clear'
|
|
|
+ """
|
|
|
}
|
|
|
}
|
|
|
}
|