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

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

我的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

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

git jenkins-pipeline git-submodules
2个回答
1
投票

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

Jenkins UI Screenshot

并在Jenkinsfile中使用以下代码

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

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'
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.