Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline git command submodule update

I want to update submodule on git clone.

Is there a way to do this with Jenkins pipeline Git command?

Currently I'm doing this...

git branch: 'master',     credentialsId: 'bitbucket',     url: 'ssh://bitbucket.org/hello.git' 

It doesn't however update submodule once cloned

like image 368
Passionate Engineer Avatar asked Feb 17 '17 05:02

Passionate Engineer


People also ask

How do I update a submodule in git recursive?

If you already cloned the project and forgot --recurse-submodules , you can combine the git submodule init and git submodule update steps by running git submodule update --init . To also initialize, fetch and checkout any nested submodules, you can use the foolproof git submodule update --init --recursive .

How do you update a submodule in a project?

In order to update an existing Git submodule, you need to execute the “git submodule update” with the “–remote” and the “–merge” option. Using the “–remote” command, you will be able to update your existing Git submodules without having to run “git pull” commands in each submodule of your project.

Can you make changes to a git submodule?

Pushing updates in the submodule. The submodule is just a separate repository. If you want to make changes to it, you should make the changes in its repository and push them like in a regular Git repository (just execute the git commands in the submodule's directory).


2 Answers

The git command as a pipeline step is rather limited as it provides a default implementation of the more complex checkout command. For more advanced configuration, you should use checkout command, for which you can pass a whole lot of parameters, including the desired submodules configuration.

What you want to use is probably something like this :

checkout([$class: 'GitSCM',           branches: [[name: '*/master']],           doGenerateSubmoduleConfigurations: false,           extensions: [[$class: 'SubmoduleOption',                         disableSubmodules: false,                         parentCredentials: false,                         recursiveSubmodules: true,                         reference: '',                         trackingSubmodules: false]],            submoduleCfg: [],            userRemoteConfigs: [[url: 'your-git-server/your-git-repository']]]) 

From the documentation it is often cumbersome to write these kind of lines, I recommand you use instead Jenkins very good Snippet Generator (YourJenkins > yourProject > PipelineSyntax) to automatically generate the checkout line !

like image 163
Pom12 Avatar answered Sep 24 '22 04:09

Pom12


With the current Git plugin, you don't even need that.

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

But the OP is using pipeline.

So a simple first build step is enough:

git submodule update --init --recursive 

However, the OP adds:

Yes but if I'm using sh 'git submodule update --init --recursive', this will use $HOME/id_rsa right? I want to pass in my private key for this command if possible.

It is possible: In the Pipeline syntax, you can define environment variables.
Which means you can set GIT_SSH_COMMAND (with Git 2.10+).
That allows you to reference your own private key.

pipeline {     agent any      environment {         GIT_SSH_COMMAND = 'ssh -i /path/to/my/private/key'     }      stages {         stage('Build') {             steps {                 sh 'printenv'                 sh 'git submodule update --init --recursive'             }         }     } }  

If any clone involve an ssh url, that ssh clone will use the right private key.


Note that sancelot points out in the comments:

unfortunately this does not work: JENKINS-38860

The error reported above:

FATAL: Command "git config --get submodule.MySubModule.url"  returned status code 1 

Occurs for me whenever you have nested submodules.

Consider a scenario in which repo A contains submodule B, which contains submodule C.

If "Advanced submodule behaviour" with "Recursively update submodules" is not enabled, Jenkins will clone A, checkout/clone B, and fail to initialise/clone C.
This is probably expected behaviour.

If you enable "Recursively update submodules", you get the error:

FATAL: Command "git config --get submodule.C.url" returned status code 1 
like image 38
VonC Avatar answered Sep 22 '22 04:09

VonC