Jenkins: Can we checkout the submodules with user defined branch - jenkins

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.

Related

How to set up a pipeline for specific branch

I have a Java project in http://localhost:7990/scm/bout/boutique-a.git
I want to have 2 Jenkins pipeline jobs:
Job 1/ trigger on commit done on */develop
Job 2/ trigger on commit done on any */feature
p
each job will do a basic mvn install, mvn test, sonar ...
a simple script with
node {
checkout([$class: 'GitSCM',
branches: [[name: 'develop]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption', disableSubmodules: false,
parentCredentials: false, recursiveSubmodules: true, reference: '',
trackingSubmodules: false]], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'admin',
url: 'http://localhost:7990/scm/bout/boutique-a.git']]])
}
works if a commit is done in /develop or if I give explicitly the branch name like feature/test-a but how to configure a script for any feature/
It seems that what i'm asking is not possible using pipeline job.
I found a work arround for "feature/** ". I created a param BRANCH_NAME in the job, then the branch name is send by bitbucket when a push is made on "feature/** " through a basic POST request.
http://user:token#localhost:8081/jenkins/job/MY_JOB_NAME/buildWithParameters?token=U1C1yQo7x3&BRANCH_NAME=feature/branche-test

Shallow git submodule checkout using 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!

Can I augment scm in Jenkinsfile?

It's taken me ages to understand what checkout scm really means in Jenkinsfile (checkout is a function and scm is a default global variable by the way).
Now that I've understood it, I want to augment scm for example to increase the timeout for a particular checkout or to set sparseCheckoutPaths. Is this possible? If so, how?
For Git, checkout scm is basically equivalent to :
checkout([
$class: 'GitSCM',
branches: scm.branches,
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
extensions: scm.extensions,
userRemoteConfigs: scm.userRemoteConfigs
])
If you want to add sparse checkout to the existing scm, what you would do is something like:
checkout([
$class: 'GitSCM',
branches: scm.branches,
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
extensions: scm.extensions + [$class: 'SparseCheckoutPaths', sparseCheckoutPaths:[[$class:'SparseCheckoutPath', path:'path/to/file.xml']]],
userRemoteConfigs: scm.userRemoteConfigs
])
Even better, you can define a custom step, sparseCheckout in a shared library.
def call(scm, files) {
if (scm.class.simpleName == 'GitSCM') {
def filesAsPaths = files.collect {
[path: it]
}
return checkout([$class : 'GitSCM',
branches : scm.branches,
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
extensions : scm.extensions +
[[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: filesAsPaths]],
submoduleCfg : scm.submoduleCfg,
userRemoteConfigs : scm.userRemoteConfigs
])
} else {
// fallback to checkout everything by default
return checkout(scm)
}
}
Then you call it with:
sparseCheckout(scm, ['path/to/file.xml'])
You can definitely customize the checkout scm command to add more flexibility. Check out this link for all of the options - https://jenkins.io/doc/pipeline/steps/workflow-scm-step/
Timeouts:
$class: CheckoutOption timeout::::
Specify a timeout (in minutes) for checkout.
This option overrides the default timeout of 10 minutes.
You can change the global git timeout via the property org.jenkinsci.plugins.gitclient.Git.timeOut (see JENKINS-11286). Note that property should be set on both master and slave to have effect (see JENKINS-22547).
Type: int
SparseCheckoutPaths:
$class: SparseCheckoutPaths
Specify the paths that you'd like to sparse checkout. This may be used for saving space (Think about a reference repository). Be sure to use a recent version of Git, at least above 1.7.10

Change username in submodule checkout in Jenkins Pipeline DSL

I have a project that spins up EC2 instances as build slaves, the username is "ec2-user".
I am checking out a repository with submodules like this:
checkout([
$class: 'GitSCM',
branches: [[name: 'deadbeefdeadbeefcafebabe']],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false]
], submoduleCfg: [],
userRemoteConfigs: [
[credentialsId: 'deadbeef-cafe-babe-cafe-babebeef1234',
refspec: 'refs/changes/12/34567/89',
url: 'ssh://user#host:29418/some/project']
]
])
Please note that the URL has a username.
Now, the .gitmodules file looks like this:
[submodule "path/in/project"]
path = path/in/project
url = ssh://host:29418/other/project
branch = somebranch
The submodule description does not have a username specified. When it is time for the submodule clone, the clone is denied with invalid credentials. Manually editing the .gitmodules file and adding the user# in front of the URL works, but this is a bad workaround at best. Changing the URL on disk and then calling checkout again does also not work.
How can I make Jenkins use the username it used in the parent checkout? If that is not possible, what is a workaround which does not involve changing the source repo?

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