Shallow git submodule checkout using Jenkins - jenkins

I have no problem doing this using git commands, as in Set Git submodule to shallow clone & sparse checkout? but I have a project using Jenkins and I'm using the checkout() function in the Groovy Pipeline syntax. I can't find any option to pass the --depth option to the git submodule update commands.

You should be able to use the extensions parameter in the checkout step:
checkout([
$class: 'GitSCM',
branches: [[name: 'master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: true]],
submoduleCfg: [],
userRemoteConfigs: [[url: 'git#yourrepo.com:repo/repo.git']]])
If you are using the snippet generator, select Additional Behaviors -> Advanced clone behaviors to see the different options. Hope that helps!

Related

how to override default Jenkins Git plugin checkout with pipeline code?

I have jenkins multibranch pipeline with jenkins git plugin.
When the new pull requested is created a new PR job starts, and checkout of the repository is done automatically. The problem is sometimes it hits timeout (networking).
I try to do retry in pipeline by using GitSCM code with some conditionals:
checkout([
$class: 'GitSCM',
branches: scm.branches,
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
extensions: scm.extensions + [[$class: 'CloneOption', noTags: false, reference: '', shallow: false]],
submoduleCfg: [],
userRemoteConfigs: scm.userRemoteConfigs
])
}
It repeats the checkout just fine, but I still need to disable the first default checkout from the plugin(if it fails the job fails). How do I do that? How do I override the built-in checkout?
skipDefaultCheckout option should disable default checkout. E.g.:
options { skipDefaultCheckout() }
Read more here about it: https://www.jenkins.io/doc/book/pipeline/syntax/#available-options

Jenkins: Can we checkout the submodules with user defined branch

I have a peculiar requirement, I need to change the branch name of the submodule which is mentioned in the .gitmodule file.
ex:
consider below is my .gitmodule file.
[submodule "submodule"]
path = aiml/core/model/modelcollection
url = ssh://git#git.myorg.com/project/repo.git
branch = module1
I am using Jenkins checkout function as below.
checkout([
$class: 'GitSCM',
branches: [[name: 'develop']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false]
],
submoduleCfg: [],
userRemoteConfigs: [[url: "git#git.myorg.com/project/repo.git"]]
])
My case: I need to change the branch name which is already mentioned in .gitmodule (i.e module1) to a different branch name example "module2". Is this possible while using the Jenkins checkout function?
Is there any other way to fulfil the above requirement?
Note: Just a curious add on the question. Can we add two entries in .gitmodule and clone only one based on the need. Is this possible with Jenkins checkout.

Jenkins Pipeline checkout branch which can parameterised

I am trying to write a stage in Jenkinsfile where I'll pass the branch name from the Jenkins job to checkout the code at a specific location.
stage("Prepare") {
steps {
checkout([$class: 'GitSCM',
branches: [[name: '*/master' ]],
extensions: scm.extensions,
userRemoteConfigs: [[
url: 'https://gitlab.example.com/user/example_repo.git',
credentialsId: 'my-gitlab-repo-creds'
]]
])
}
}
Also, how can we define the location where to checkout the project.
You're missing the power of the SCM checkout step. In turn, your pipeline is missing some important configuration to get what you want:
pass the branch name from the Jenkins job to checkout the code
define the location where to checkout the project
This can all be done. I'll explain the options used to accomplish this.
1) Pass the branch name to checkout step
You can achieve this with the BRANCH_NAME environment variable.
2) Define project checkout location
Add the following extension and get rid of the scm.extensions value.
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'myrepo']]
Specify a local directory (relative to the workspace root) where the Git repository will be checked out. If left empty, the workspace root itself will be used.
For the branches option,
The safest way is to use the refs/heads/ syntax. This way the expected branch is unambiguous.
For example:
branches: [[name: 'refs/heads/${env.BRANCH_NAME}']]
Piecing it all together,
checkout(
[
$class: 'GitSCM',
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'example_repo']],
branches: [[name: 'refs/heads/${env.BRANCH_NAME}']],
userRemoteConfigs: [
[
url: 'https://gitlab.example.com/user/example_repo.git',
credentialsId: 'my-gitlab-repo-creds',
name: 'origin'
]
]
]
)
The above code will checkout the ${env.BRANCH_NAME} branch of https://gitlab.example.com/user/example_repo.git to the $WORKSPACE_DIR/example_repo folder.

How to write 'Git LFS pull after checkout' settings in the Jenkins pipeline

I created a configuration file with Jenkins file to configure the Jenkins pipeline.
An error occurred when doing git pull.
The cause was that the groovy file did not have the Git LFS pull after checkout setting.
I do not know how to write Git LFS pull after checkout setting to groovy.
git(
 url: git#...,
 branch: "master",
 credentialsId:"abcdefg"
)
// Git LFS pull after checkout setting??
Here's how I was able to use the Git plugin in the pipeline. Refer to the documentation here for more info:
checkout([ $class: "GitSCM",
branches: [[name: "refs/heads/${your branch name}"]],
extensions: [
[$class: "GitLFSPull"]
],
userRemoteConfigs: [
[credentialsId: "${your git credential ID}",
url: "${your git URL}"]
]
])
Considering you are trying to download a large file, you may want to increase the time out limit too (default is set to 10 minutes):
checkout([ $class: 'GitSCM',
branches: [[name: 'refs/heads/'+"${branch_or_tag}"]],
extensions: [[$class: 'GitLFSPull']]
+[[$class: 'CloneOption', timeout: 30]],
userRemoteConfigs: [
[credentialsId: "${your git credential ID}",
url: "${your git URL}"]
]
])

Checkout repository based on Tag in the Jenkins Workflow plugin

With the Jenkins Workflow Plugin, I can checkout a repository based on branch. However, I would like to checkout a repository based on a tag.
This is my current configuration for checking out the master branch
node {
git url: src, branch: 'master'
}
Now I would like to achieve to check out tag 3.6.1. I tried to change the branch to a tag, but that won't work. Neither is there something in the documentation regarding checking out on tag.
Is this currently possible? Am I overseeing something?
references;
https://github.com/jenkinsci/workflow-plugin
https://github.com/jenkinsci/workflow-plugin/blob/master/scm-step/README.md
https://github.com/jenkinsci/workflow-plugin/blob/master/scm-step/src/main/resources/org/jenkinsci/plugins/workflow/steps/scm/GitStep/config.jelly
https://github.com/jenkinsci/workflow-plugin/blob/master/scm-step/src/main/java/org/jenkinsci/plugins/workflow/steps/scm/GitStep.java
Just found the answer myself by crawling through the issue list. Seems like they won't change it; https://issues.jenkins-ci.org/browse/JENKINS-27018
This is the suggested solution;
checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: src]], branches: [[name: 'refs/tags/3.6.1']]], poll: false
This works:
checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: repoURL,
credentialsId: credential]], branches: [[name: tag-version]]],poll: false
Not This:
checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: repoURL],
[credentialsId: credential]], branches: [[name: tag-version]]],poll: false
noTags: false does the trick.
checkout([$class: 'GitSCM', branches: [[name: githash ]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CloneOption',
depth: 0,
noTags: false,

Resources