title: Jenkins kubernetes agent checkout scm not using configured key
body: <p>I am getting errors using <code>checkout scm</code> in a pipeline, because of two issues.</p>
  <p>The setup:</p>
  <ul>
  <li>Private Kubernetes cluster - 1 controller, 2 workers, on Ubuntu 20.04 VMs</li>
  <li>Jenkins running in Kubernetes pods</li>
  <li>Kubernetes plug-in to instantiate Jenkins build agents</li>
  <li>Private GIT server on the controller VM outside of the cluster, ssh access</li>
  <li>ssh private key for GIT configured in Jenkins credentials</li>
  <li>Jenkins project 'hello' configured to use this private GIT and associated ssh key</li>
  <li>Jenkinsfile (pipeline) to build</li>
  </ul>
  <p>I want to use a simple <code>checkout scm</code> step in the Jenkinsfile.</p>
  <p><strong>Problem 1</strong> The build fails with <code>Host key verification failed.</code> because the Kubernetes agent pod doesn't have the GIT server in its <code>known_hosts</code>.</p>
  <p><strong>Problem 2</strong> If I force the controller cert into <code>known_hosts</code> (for example, hard-code an echo into Jenkinsfile, and then add a <code>git ls-remote</code> step), it fails with <code>Permission denied</code> because the configured ssh private key is not present in the agent pod.</p>
  <p>I've found a workaround for both of these:</p>
  <pre><code>podTemplate(
  ...
  {
    node(POD_LABEL) {
      stage('Checkout') {
        withCredentials([sshUserPrivateKey(
            credentialsId: 'private_git', 
            keyFileVariable: 'PRIVATE_GIT_KEY',
            passphraseVariable: '',
            usernameVariable: ''
        )]) {
          sh 'mkdir -p ~/.ssh'
          sh 'cp $PRIVATE_GIT_KEY ~/.ssh/id_rsa'
          sh '/usr/bin/ssh-keyscan -t rsa kube-master.cluster.dev &gt;&gt; ~/.ssh/known_hosts'
          sh 'chown -R $USER:$USER ~/.ssh'
          sh '/usr/bin/git ls-remote ssh://git@kube-master.cluster.dev:/git/hello.git'
        }
        checkout scm
      }
    ...
    }
  }
  </code></pre>
  <p>What do I need to avoid this workaround and just use <code>checkout scm</code> like it is intended?</p>
  <p>Example failure log:</p>
  <pre><code>Running on build-pod-xdh86-53wh7 in /home/jenkins/agent/workspace/hello
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (Checkout)
  [Pipeline] checkout
  Selected Git installation does not exist. Using Default
  The recommended git tool is: NONE
  using credential private_git
  Cloning the remote Git repository
  ERROR: Error cloning remote repo 'origin'
  hudson.plugins.git.GitException: Command &quot;git fetch --tags --force --progress -- ssh://git@kube-master.cluster.dev/git/hello.git +refs/heads/*:refs/remotes/origin/*&quot; returned status code 128:
  stdout: 
  stderr: Host key verification failed.
  fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights
  and the repository exists.
  </code></pre>