Git submodules not updating in Jenkins build

GitJenkinsGit Submodules

Git Problem Overview


I have a submodule in a project in Jenkins. I've enabled the advanced setting to recursively update submodules.

When I run the build, I see that the workspace has the files from the submodule. The problem is, it seems to be the first revision of the submodule. When I push changes (repository hosted on GitHub) Jenkins doesn't seem to update the submodule to get the right changes. Has anyone ever seen this?

Git Solutions


Solution 1 - Git

Note that the Jenkins Git plugin 2.0 will have "advance submodule behaviors", which should ensure proper updates of the submodules:

git 2.0

As commented by vikramvi:

> Advanced sub-modules behavior > "Path of the reference repo to use during submodule update" against this field , add submodule git url.

Path


Owen B mentions in the comments:

> For the authentication issue, there's now a "Use credentials from default remote of parent repository" option

Seen here in JENKINS-20941:

https://issues.jenkins-ci.org/secure/attachment/33245/Screen%20Shot%202016-07-08%20at%2010.09.17.png

Solution 2 - Git

This is covered in the Git Plugin documentation on the Jenkins site under the section: Recursive submodules.

excerpt

> The GIT plugin supports repositories with submodules which in turn have submodules themselves. This must be turned on though: in Job Configuration -> Section Source Code Management, Git -> Advanced Button (under Branches to build) -> Recursively update submodules.

Example

From the configuration screen of your job, in the Source Code Management section, pull the Add button down select "Advanced sub-modules behavior".

   s1

                                 s2

Then select "Recursively update submodules":

   s3

Solution 3 - Git

Are you aware that your Git repository always refers to a particular revision of a submodule? Jenkins is not going to automatically change the revision.

If you want to take a newer revision of the submodule into use, you have to do this in your local Git repository:

cd submoduledir
git pull
cd ..
git add submoduledir
git commit -m 'Updated to latest revision of submoduledir'
git push # Go and watch Jenkins build with the new revision of the submodule

When you do it like this, Jenkins will check out the exact same revision of the submodule during the build. Jenkins does not on its own decide which revision of the submodule to use. This is the fundamental difference between Git submodules and SVN externals.

You might want to read a good reference on submodules, e.g. http://progit.org/book/ch6-6.html.

Solution 4 - Git

Finally stumbled on a way to do this and it's simple.

The Issue:

The initial clone with credentials works fine but subsequent submodule cloning fails with incorrect credentials.

  1. Automatic advanced sub-module cloning: Source Code Management >> Additional Behaviours >> Advanced sub-modules behaviours: results in credential error.
  2. git submodule update --init in the Execute Shell section also fails with credentials error.

The Solution:

I'm using jenkins-1.574.

  1. Check the Build Environment >> SSH Agent box.

  2. Select the correct credentials (probably the same as selected in Source Code Management section

  3. Update submodules in the Execute Shell section

     git submodule sync
     git submodule update --init --recursive
    

Here's a screen shotenter image description here

Solution 5 - Git

It looks like I found a solution:

I added a build step to execute the following shell commands:

git submodule foreach git checkout master
git submodule foreach git pull

Solution 6 - Git

If you are using Jenkins Git module, you can set it to "Wipe out workspace before build", this way it will always gets the correct sub module.

Solution 7 - Git

I am using scripted pipelining with the checkout plugin. If you want the submodules to be the same as in your repository, simply switch off the trackingSubmodules option like this:

checkout([$class: 'GitSCM', branches: [[name: '*/develop']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: false, reference: '', trackingSubmodules: false]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '[myCredentials]', url: 'https://git.myRepo.git']]])

Solution 8 - Git

On "Advanced sub-modules behaviours", check "Update tracking submodules to tip of branch"

Reference Image

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitslmView Answer on Stackoverflow
Solution 3 - GitstiView Answer on Stackoverflow
Solution 4 - GitpotenchView Answer on Stackoverflow
Solution 5 - GitBenView Answer on Stackoverflow
Solution 6 - GitAmin YView Answer on Stackoverflow
Solution 7 - GitJochen GunzelmannView Answer on Stackoverflow
Solution 8 - GitAnjaliView Answer on Stackoverflow