Checkout git repo with git submodules in Jenkinsfile with credentials
Asked Answered
D

2

1

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 ?

Diorite answered 18/2, 2019 at 23:39 Comment(1)
this is how credentials are passed to git checkout with submodule: https://mcmap.net/q/223382/-git-submodules-not-updating-in-jenkins-buildPlayground
P
3

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
        }
    }
Pitiful answered 19/2, 2019 at 3:5 Comment(0)
R
1

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'
            }
        }
    }
}
Renascence answered 2/4, 2019 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.