I have a build job in Jenkins created by the Github Organization plugin. The Jenkinsfile
for this build checkouts the code using checkout scm
which is good as it figures out the correct branch/revision to checkout when building either PR triggered changes or pushes to the master branch.
How can I make this:
node {
checkout scm
}
checkout submodules?
Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .
This is because of some major drawbacks around git submodules, such as being locked to a specific version of the outer repo, the lacking of effective merge management, and the general notion that the Git repository itself doesn't really know it's now a multi-module repository.
The solution with sh 'git submodule...'
works only for Repositorys without special authentication.
We use following solution in our set up:
node {
checkout([
$class: 'GitSCM',
branches: scm.branches,
doGenerateSubmoduleConfigurations: true,
extensions: scm.extensions + [[$class: 'SubmoduleOption', parentCredentials: true]],
userRemoteConfigs: scm.userRemoteConfigs
])
}
In the Github organization plugin, add the advanced submodule behaviors.
And configure it like this:
As @oeuftete pointed out, you also may need to add the "Checkout over SSH" behaviour (and provide a key credential) if the submodule(s) use the SSH protocol.
As documented here: https://wiki.jenkins.io/display/JENKINS/Git+Plugin
Change it to this:
node {
checkout scm
sh 'git submodule update --init'
}
Use bat
instead of sh
if Jenkins is running on Windows.
We had similar issue, Jenkin user is using https to pull from Github but the submodule is using SSH and we want to handle the pull requests with Jenkins. I did the below checkout stage hope it will help someone here:
stage('Checkout') {
if(env.BRANCH_NAME == "develop" || env.BRANCH_NAME == "master") {
checkout([
$class: 'GitSCM',
branches: scm.branches,
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false],
[$class: 'CleanBeforeCheckout'],
[$class: 'CleanCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'jenkins-ssh',
url: '[email protected]:<AccountName>/<RepoName.git>']]
])
}
else if (env.CHANGE_ID) {
checkout([
$class: 'GitSCM',
branches: [[name: "FETCH_HEAD"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false],
[$class: 'CleanBeforeCheckout'],
[$class: 'CleanCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'jenkins-ssh',
refspec: "+refs/pull/${CHANGE_ID}/head:refs/remotes/origin/${BRANCH_NAME} +refs/heads/${CHANGE_TARGET}:refs/remotes/origin/${CHANGE_TARGET}",
url: '[email protected]:<AccountName>/<RepoName.git>']]
])
}
}
Maybe there is an easier way to do it, I would be happy to hear from you :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With