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 ?

Ebeid ElSayed
  • 1,126
  • 2
  • 15
  • 32
  • this is how credentials are passed to git checkout with submodule: https://stackoverflow.com/a/62789511/901508 – Nikita Feb 12 '23 at 09:48

2 Answers2

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
        }
    }
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'
            }
        }
    }
}
MustafaC
  • 41
  • 1
  • 5