我正在尝试使用 git 命令在 Jenkins 构建目录中克隆存储库
我在 Jenkns 作业配置中将 ssh 密钥存储为凭据,我尝试的是:
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' }
不幸的是它挂在:
Cloning into proj....
有什么建议吗?
我知道我可以使用 Git 插件来实现这一点:
git branch: 'master'
url: 'ssh://my-host/proj/proj.git'
credentials: 'my-creds'
但是我想在结账后执行其他操作,因此我希望能够自由地以 CLI 方式使用 git,而不是通过 Jenkins 插件。
这适用于通过 SSH 的 Bitbucket 上的 git 存储库,但也应该适用于 github/gitlab。
my-creds
是 SSH Username with Private Key
凭证,您已将其公钥添加到 git 提供商。
pipeline {
agent any
stages {
stage('Clone Repository') {
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'my-creds', keyFileVariable: 'SSH_KEY')]) {
git credentialsId: 'my-creds',
url: '[email protected]: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 [email protected]:my-org/myrepo.git
'''
}
}
}
}
}