From da6358b63d2e8cbb112fdf7123819833232e8deb Mon Sep 17 00:00:00 2001 From: AlexeerCT Date: Thu, 2 Apr 2026 10:39:15 -0500 Subject: [PATCH] Enhance Jenkinsfile to improve SSH command execution. Added SSH private key handling and refactored the deployment commands into a script block for better readability and maintainability. --- Jenkinsfile | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index cdd45885..afebe04c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -220,19 +220,25 @@ pipeline { withCredentials([ usernamePassword(credentialsId: 'harbor-credentials', usernameVariable: 'HARBOR_USERNAME', passwordVariable: 'HARBOR_PASSWORD'), string(credentialsId: 'dev-server-user', variable: 'DEV_SERVER_USER'), - string(credentialsId: 'dev-server-host', variable: 'DEV_SERVER_HOST') + string(credentialsId: 'dev-server-host', variable: 'DEV_SERVER_HOST'), + sshUserPrivateKey(credentialsId: 'dev-server-ssh', keyFileVariable: 'DEV_SERVER_KEY') ]) { - sshagent(credentials: ['dev-server-ssh']) { - sh ''' + script { + def remote = [ + name: 'dev-server', + host: env.DEV_SERVER_HOST, + user: env.DEV_SERVER_USER, + identityFile: env.DEV_SERVER_KEY, + allowAnyHosts: true + ] + sshCommand remote: remote, command: """ set -euo pipefail - ssh -o StrictHostKeyChecking=accept-new "${DEV_SERVER_USER}@${DEV_SERVER_HOST}" ' - cd ~/projects/anexo76 && - echo '"'"'"${HARBOR_PASSWORD}"'"'"' | docker login '"${REGISTRY}"' -u '"${HARBOR_USERNAME}"' --password-stdin && - docker compose -f docker-compose.prod.yml pull && - docker compose -f docker-compose.prod.yml up -d && - docker image prune -f - ' - ''' + cd ~/projects/anexo76 + echo '${env.HARBOR_PASSWORD}' | docker login '${env.REGISTRY}' -u '${env.HARBOR_USERNAME}' --password-stdin + docker compose -f docker-compose.prod.yml pull + docker compose -f docker-compose.prod.yml up -d + docker image prune -f + """ } } }