I am trying to use git commands to clone a repository within the directory of a Jenkins build
I have ssh keys stored as credentials in my Jenkns job configuration, what i try:
withCredentials([sshUserPrivateKey(credentialsId: 'my-creds', keyFileVariable: 'SSH_KEY')]) { sh 'export GIT_SSH_COMMAND="ssh -i $SSH_KEY"' sh 'git clone --verbose --progress ssh://my-host/proj/proj.git' }
Unfortunatelly it hangs at:
Cloning into proj....
Any suggestions?
I know i could achieve this with the Git plugin:
git branch: 'master'
url: 'ssh://my-host/proj/proj.git'
credentials: 'my-creds'
But there are other operations i would like to perform after the checkout, therefore i would like to have the freedom to use git in the CLI way and not via the Jenkins plugin.
This works for a git repo on Bitbucket over SSH, but should also work on github/gitlab.
my-creds
being a SSH Username with Private Key
credential, for which you've added the public key to your git provider.
pipeline {
agent any
stages {
stage('Clone Repository') {
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'my-creds', keyFileVariable: 'SSH_KEY')]) {
git credentialsId: 'my-creds',
url: 'git@bitbucket.org:my-org/myrepo.git',
branch: 'master'
}
}
}
}
}
pipeline {
agent any
stages {
stage('Clone Repository') {
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'my-creds', keyFileVariable: 'SSH_KEY')]) {
sh '''
ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts
export GIT_SSH_COMMAND="ssh -v -i $SSH_KEY"
git clone --verbose --progress git@bitbucket.org:my-org/myrepo.git
'''
}
}
}
}
}
(Instead of ssh-keyscan
you can also add -o StrictHostKeyChecking=no
to GIT_SSH_COMMAND
, but this is not such a good idea from security perspective as it will just trust any host in this case.