Reputation: 511
I'm trying to execute a remote command from a jenkins pipeline to other server. I created the user pf-devops on both machines.
On the jenkins server, I created the ssh keys id_rsa and id_rsa.pub in the /home/pf-devops/.ssh.
Then on the other server, created the same user and added his public key on /home/pf-devops/.ssh/authorized_keys.
Then, I added his private key on the Jenkins credentials as it can be seen below:
Then on the Jenkinsfile of my project I have the following:
def DOCKER_COMPOSE_FILE = 'docker-compose.yml'
def UUID = UUID.randomUUID().toString()
pipeline {
agent any
stages {
stage('Deploy to danielserrao') {
when {
branch 'danielserrao'
}
steps {
script {
DOCKER_COMPOSE_FILE = 'docker-compose-dev-databases.yml'
echo "This should be done only on danielserrao with docker compose file ${DOCKER_COMPOSE_FILE}"
sshagent(['821f7495-e873-4a4b-97eb-f00433d078d9']) {
sh "ssh XXX.XXX.X.153 'touch /tmp/${UUID}'"
}
}
}
}
...
but when executing the pipeline I get the following:
This should be done only on danielserrao with docker compose file docker-compose-dev-
databases.yml
[Pipeline] sshagent
[ssh-agent] Using credentials pf-devops (pf-devops private ssh key)
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent] Exec ssh-agent (binary ssh-agent on a remote machine)
$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-GfKzb8sTTsPq/agent.2021
SSH_AGENT_PID=2023
Running ssh-add (command line suppressed)
Identity added: /var/jenkins_home/workspace/tion-deploy-
penguin_danielserrao@tmp/private_key_8009694440488072457.key
(/var/jenkins_home/workspace/tion-deploy-
penguin_danielserrao@tmp/private_key_8009694440488072457.key)
[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
+ ssh XXX.XXX.X.153 touch /tmp/11c8ef5c-4e71-4f38-948a-825af129ce9d
...
Then it hangs forever. If I try to execute the same ssh command from the jenkins server using the user pf-devops it works and creates the temporary file on XXX.XXX.X.153, so the user SSH private key seems to be correct.
I also tried a another solution with the method withCredentials as specified at https://www.reddit.com/r/docker/comments/b8lmc4/jenkins_pipeline_not_correctly_using_sshagent/, but got the same result.
Any idea on what could be happening or how I can troubleshoot this problem?
Jenkins version: 2.204.1
Both machines OS: Ubuntu 18.04.3 LTS
Thanks for any help.
Upvotes: 1
Views: 2902
Reputation: 111
I used this answer recently and it worked: https://stackoverflow.com/a/50881959/10531450
Otherwise, you could try ssh -v ...
(verbose).
Upvotes: 3