Git 克隆卡在 Jenkins 管道中

问题描述 投票:0回答:1

我正在尝试使用 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....
enter image description here

有什么建议吗?

我知道我可以使用 Git 插件来实现这一点:

git branch: 'master'
    url: 'ssh://my-host/proj/proj.git'
    credentials: 'my-creds'

但是我想在结账后执行其他操作,因此我希望能够自由地以 CLI 方式使用 git,而不是通过 Jenkins 插件。

git jenkins
1个回答
0
投票

这适用于通过 SSH 的 Bitbucket 上的 git 存储库,但也应该适用于 github/gitlab。

my-creds
SSH Username with Private Key
凭证,您已将其公钥添加到 git 提供商。

  1. 使用 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'
                }
            }
        }
    }
}

  1. 使用 git 命令。确保手动将远程 SSH 主机密钥添加到您的known_hosts。否则 git clone 将失败。
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
                    '''
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.