I am getting errors using checkout scm
in a pipeline, because of two issues.
The setup:
- Private Kubernetes cluster - 1 controller, 2 workers, on Ubuntu 20.04 VMs
- Jenkins running in Kubernetes pods
- Kubernetes plug-in to instantiate Jenkins build agents
- Private GIT server on the controller VM outside of the cluster, ssh access
- ssh private key for GIT configured in Jenkins credentials
- Jenkins project 'hello' configured to use this private GIT and associated ssh key
- Jenkinsfile (pipeline) to build
I want to use a simple checkout scm
step in the Jenkinsfile.
Problem 1 The build fails with Host key verification failed.
because the Kubernetes agent pod doesn't have the GIT server in its known_hosts
.
Problem 2 If I force the controller cert into known_hosts
(for example, hard-code an echo into Jenkinsfile, and then add a git ls-remote
step), it fails with Permission denied
because the configured ssh private key is not present in the agent pod.
I've found a workaround for both of these:
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 >> ~/.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
}
...
}
}
What do I need to avoid this workaround and just use checkout scm
like it is intended?
Example failure log:
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 "git fetch --tags --force --progress -- ssh://git@kube-master.cluster.dev/git/hello.git +refs/heads/*:refs/remotes/origin/*" 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.