代码之家  ›  专栏  ›  技术社区  ›  Red Cricket

如何使用Groovy克隆GitHub git仓库?

  •  0
  • Red Cricket  · 技术社区  · 3 年前

    我过去知道怎么做,但那是三年前的事了。我有一个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'
                    }
                } 
            }
        }
    }
    

    我有这些参数:

    enter image description here

    当我在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希望该文件已经存在,如果是这样,该怎么做?

    1 回复  |  直到 3 年前
        1
  •  1
  •   Borbyu    3 年前

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

    stage('Git Clone') {
        steps {
            git branch: 'master',
                credentialsId: 'git_credential', #suggest to use credential on Jenkins.
                url: 'git_URL'
        }
    }
    
        2
  •  0
  •   Red Cricket    3 年前

    我在这里找到了我需要的答案: 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'
    
    
    推荐文章