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.

This commit is contained in:
2026-04-02 10:39:15 -05:00
parent 5ef77a9eef
commit da6358b63d

28
Jenkinsfile vendored
View File

@@ -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
"""
}
}
}