Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-50104

ssh-agent is killed after first use in docker container

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Critical Critical
    • ssh-agent-plugin
    • None

      After storing the corresponding credentials in jenkins, run this simple pipeline.

      pipeline {
        agent none
        stages {
          stage('Build') {
            agent {
              docker {
                image 'ubuntu:xenial'
              }
            }
            steps {
              sh 'apt-get update --fix-missing && apt-get install -y --no-install-recommends openssh-server'
              sshagent(['LOOPYBOT_SSH_KEY']) {
                echo 'this works...'
                sh 'ssh -o StrictHostKeyChecking=no -T git@github.com'
                echo '...but by now, the agent is dead already'
                sh 'ssh -o StrictHostKeyChecking=no -T git@github.com'
              }
            }
          }
        }
      }
      

      The first query to github succeeds, as shown in the following logs, but in the meantime the agent is being killed, leading to failure (the second echo does not even get to be executed)

      [Pipeline] sshagent
      [ssh-agent] Using credentials loopybot (Loopybot private SSH key)
      [ssh-agent] Looking for ssh-agent implementation...
      [ssh-agent]   Exec ssh-agent (binary ssh-agent on a remote machine)
      $ docker exec 4fd40aecda7b7e358e741e6d6718f26c859d469782682dd77272373a4a04c6c3 ssh-agent
      SSH_AUTH_SOCK=/tmp/ssh-ff6fQZ8xmkpj/agent.464
      SSH_AGENT_PID=469
      $ docker exec --env DISPLAY=:0 --env SSH_AGENT_PID=469 --env "SSH_ASKPASS=/var/jenkins_home/workspace/test ssh-agent@tmp/askpass_6666993461119741620.sh" --env SSH_AUTH_SOCK=/tmp/ssh-ff6fQZ8xmkpj/agent.464 --env SSH_PASSPHRASE=P44G4LUcCR 4fd40aecda7b7e358e741e6d6718f26c859d469782682dd77272373a4a04c6c3 ssh-add "/var/jenkins_home/workspace/test ssh-agent@tmp/private_key_750299606857458225.key"
      Identity added: /var/jenkins_home/workspace/test ssh-agent@tmp/private_key_750299606857458225.key (/var/jenkins_home/workspace/test ssh-agent@tmp/private_key_750299606857458225.key)
      [ssh-agent] Started.
      [Pipeline] {
      [Pipeline] echo
      this works...
      [Pipeline] sh
      [test ssh-agent] Running shell script
      + ssh -o StrictHostKeyChecking=no -T git@github.com
      $ docker exec --env SSH_AGENT_PID=469 --env SSH_AUTH_SOCK=/tmp/ssh-ff6fQZ8xmkpj/agent.464 4fd40aecda7b7e358e741e6d6718f26c859d469782682dd77272373a4a04c6c3 ssh-agent -k
      unset SSH_AUTH_SOCK;
      unset SSH_AGENT_PID;
      echo Agent pid 469 killed;
      [ssh-agent] Stopped.
      Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
      Hi loopybot! You've successfully authenticated, but GitHub does not provide shell access.
      [Pipeline] }
      [Pipeline] // sshagent
      [Pipeline] }
      $ docker stop --time=1 4fd40aecda7b7e358e741e6d6718f26c859d469782682dd77272373a4a04c6c3
      $ docker rm -f 4fd40aecda7b7e358e741e6d6718f26c859d469782682dd77272373a4a04c6c3
      [Pipeline] // withDockerContainer
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] End of Pipeline
      ERROR: script returned exit code 1
      Finished: FAILURE
      

            Unassigned Unassigned
            santi Santi Villalba
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: