使用凭据在Jenkinsfile中使用git子模块签出git repo

时间:2019-02-18 23:39:20

标签: git jenkins-pipeline git-submodules

我的Jenkinsfile中有一个阶段,如下所示:

stage('Pull Source Code') {
    steps {
        script {
            git branch: "master",
                    credentialsId: 'myCredentialId',
                    url: "${GIT_URL}"
        }
        sh 'git submodule update --recursive'
    }
}

我想为git git子模块更新步骤提供凭据,因为它给出以下错误:

+ git submodule update --recursive
Cloning into 'submodule-destination-folder'...
fatal: could not read Username for 'https://tfsgit.mycompany.com': No such device or address
fatal: clone of 'https://tfsgit.mycompany.com/submodule-repo' into submodule path 'submodule-destination-folder' failed

有没有办法为git子模块更新提供Jenkins凭证?

2 个答案:

答案 0 :(得分:1)

其中一种方法是在用户界面中使用“高级子模块行为”

Jenkins UI Screenshot

,并在Jenkinsfile中包含以下代码

stage('Pull Source Code') {
        steps {
          checkout scm
        }
    }

答案 1 :(得分:0)

您可以这样做:

stage('Pull Source Code') {
    steps {
        script {
            git branch: "master",
            credentialsId: 'myCredentialId',
            url: "${GIT_URL}"
            }
        def submodulesList = sh(script:"git submodule init", returnStdout:true).split("\n")
        for (String submodule : submodulesList) {
            def submoduleName = submodule.split("'")[1]
            def submoduleGitRepoUrl = submodule.split("\\(")[1].split("\\)")[0]
            dir(submoduleName){
                git url: submoduleGitRepoUrl, branch: "master", credentialsId: 'myCredentialId'
            }
        }
    }
}
相关问题