Reputation: 943
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?
Upvotes: 23
Views: 42871
Reputation: 2618
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
Upvotes: 26
Reputation: 879
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 :-)
Upvotes: 2
Reputation: 539
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
])
}
Upvotes: 28
Reputation: 4879
Change it to this:
node {
checkout scm
sh 'git submodule update --init'
}
Use bat
instead of sh
if Jenkins is running on Windows.
Upvotes: 6