Example below shows how to use the sshUserPrivateKey of a server to do Git clone in Jenkinsfile:
node(‘jenkins_node’){
stage(‘checkout code’){
//Private key for server is stored in Jenkins with id ‘private_key_for_server’ and can be accessible via variable ‘private_key’
withCredentials([sshUserPrivateKey(credentialsId: ‘private_key_for _server’, keyFileVariable: ‘private_key’, passphraseVariable: ”, usernameVariable: ”)]){
// start ssh-agent
sh ‘ssh-agent /bin/bash’// add private key to ssh-agent, check if private key is successfully added and git clone using the private key
sh ‘eval $(ssh-agent) && ssh-add ${private_key} && ssh-add -l && git clone git@git.test.com:test.git’
}
}
}
Leave a Reply