简体   繁体   中英

Checkout git repo with git submodules in Jenkinsfile with credentials

I have a stage in my Jenkinsfile that look like:

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

I want to provide credentials for the git git submodule update step because it giving the following error:

+ 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

Is there a way to provide Jenkins credentials to git submodule update ?

One of the approaches is to use the "Advanced sub-modules behaviours" in the UI

Jenkins UI Screenshot

and having the following piece of code in Jenkinsfile

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

You can do this:

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'
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM