如何使用Groovy克隆GitHub git repo?

kokeuurv  于 2023-10-15  发布在  Git
关注(0)|答案(2)|浏览(170)

我以前知道怎么做,但那是三年前的事了。我有这个Jenkins Pipeline Groovy

pipeline {
    agent any

    stages {
        
        stage("Checkout") {
             steps {
                withCredentials([sshUserPrivateKey(credentialsId: 'mySshUserCred', 
                  keyFileVariable: 'key_file_var', usernameVariable: 'username_var')]) {
                    sh 'git clone [email protected]:SorryAssInc/ops.git'
                }
            } 
        }
    }
}

我有这些参数:

当我在Jenkins中运行我的管道时,我得到了这个输出日志:

[Pipeline] sh
+ git clone [email protected]:SorryAssInc/ops.git
Cloning into 'ops'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我做错了什么?我缺少一个插件吗?keyFileVariable: 'key_file_var'是什么意思这是Jenkins将写入我存储在Jenkins凭据中的私钥数据的地方吗?或者Jenkins希望该文件已经存在,如果是这样,如何做到这一点?

e5nqia27

e5nqia271#

您可以按照以下步骤操作。

stage('Git Clone') {
    steps {
        git branch: 'master',
            credentialsId: 'git_credential', #suggest to use credential on Jenkins.
            url: 'git_URL'
    }
}
zwghvu4y

zwghvu4y2#

我在这里找到了我需要的答案:Git from Jenkins pipeline is using wrong SSH private key to push back into Git repository
所以我需要改变的是...

sh 'git clone [email protected]:SorryAssInc/ops.git'

。到这个。。

sh 'GIT_SSH_COMMAND="ssh -i $key_file_var" git clone [email protected]:SorryAssInc/ops.git'

相关问题