How to set up a pipeline for specific branch - jenkins

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

Related

Jenkins checkout scm step

I'm new to Jenkins and I'm trying to understand the following step in Jenkins pipeline line by line:
checkout scm
dir("some_directory") {
checkout(
changelog: false,
poll: false,
scm: [
$class : 'GitSCM',
branches : [[name: SOME_BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
extensions : [[$class: 'CloneOption', depth: 0, honorRefspec: true, reference: '', shallow: false]],
submoduleCfg : [],
userRemoteConfigs : [[url: SOME_URL]]
]
)
sh 'pwd; ls'
}
From the research I've done I understood that
checkout scm dir("some_directory")
'dir' creates a folder in workspace if it doesn't exist, and git project gets checked out into this directory
checkout(
changelog:false,
poll:false,
scm: [...]
)
this block of code specifies git parameters of the git project that is being checked out into the directory specified above.
This is so far what I understood, can someone please lt me know if my understanding is correct? And possibly add more details to it.
Also, I am confused with the current code syntax. Would it make any difference if I rewrite the top few lines as:
checkout scm dir("some_directory")(
changelog: false,
poll: false,
etc.
)
instead of using 'checkout' two times.

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.

Running Jenkins job after merge request in Gitlab

I'm trying to use a Gitlab web hook to trigger a job in Jenknis after pushing a commit/opening a merge commit using a pipeline script.
For some reason, Jenkins always checks out the master branch and builds it. How
can I specify which branch to build using the Groovy script?
I tried to use the environment variable from the Gitlab POST request, but it still always uses the master branch:
checkout changelog: false, poll: false, scm: [$class: 'GitSCM' , branches: [[name:'origin/${env.gitlabSourceBranch}']], browser: [$class 'GitLab', repoUrl: 'some-git-repo.com', version: 9.0], doGenerateSubmoduleConfiguration: false, extensions: [[$class: 'SubmoduleOption' disableSubmodules: false, parentCredentials: true, recursiveCredentials: true, recursiveSubmodules: true, reference: '', trackingSubmodules: false], [$class: 'PrebuildMerge', options: [fastForwardMode: 'FF', mergeRemote: '', mergeTarget: 'origin/${env.gitlabTargetBranch}']]], submodulecfg: [], userRemoteConfigs: [[credentialsId: '12345', url: 'git#some-git-repo.com:A/repo.git']]]
(I generated this command using the snippet generator)
You can use the "Generic Webhook trigger" plugin in Jenkins.
It allows you to get the branch name from the POST request sent by Gitlab, map it as a variable and use it inside your pipeline.
Configuration:
Inside the pipeline:
stage('Clone sources') {
steps {
git credentialsId: '...', poll: false, branch: ${branch}, url: '...'
}
}
Full example at: https://pillsfromtheweb.blogspot.com/2021/10/trigger-jenkins-on-merge-request-from.html

Jenkins : Copy artifacts from Multibranch pipeline

I am new to Jenkin's and I have 4 repo's in Bitbucket say A,B,C,D.
I have to fetch the A,B & C repos, build them using gradle build which will generate wars.
Now I have to copy those wars in D\warsFolder
I have created Multibranch pipeline and generated the pipeline syntax which fetches A,B & C from git and builds them. Looks some thing like this
node {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'A']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'id', url: 'http://.../A.git']]])
dir('A') {
bat 'gradle build -i --info --stacktrace --debug'
}
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'B']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'id', url: 'http://.../B.git']]])
dir('B') {
bat 'gradle build -i --info --stacktrace --debug'
}
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'C']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'id', url: 'http://.../C.git']]])
dir('C') {
bat 'gradle build -i --info --stacktrace --debug'
}
}
added the above script in Jenkinsfile which I placed in A repo.
Now I have created a Multibranch pipeline Fetch_all and in branch sources -> Single repository & branch -> Repository URL I have added http://.../A.git (which has Jenkinsfile).
Upto here everything is working fine I am able to fetch the sources and build them.
I have created new job of Freestyle where in Source Code Management -> Git -> Repository URL will be http://.../D.git.
I am trying to copy the wars generated in the Fetch_all pipeline but in Build -> Copy artifacts from another project the Project Name is not accepting the Multibranch pipeline. It is throwing error like
ERROR: Unable to find project for artifact copy:
This may be due to incorrect project name or permission settings; see help for project name in job configuration.
Any help is appreciated.
Finally got it, when I gave pipeline_name/branchname i.e., Fetch_all/%00 it worked fine.
It took some time to find out the correct syntax. The documentations of the Coyartifact Plugin is a little bit confusing, as it mentions the encoding of special characters. Actually spaces don't have to be encoded, but slashes have to.
The Jenkinsfile which copies artifacts is located at 'Other-folder/Multi branch Pipeline Test/', put in this content to copy the artifact of the last successfull build of the 'Folder/Multi branch Pipeline/feature%2Fallow-artifact-copy' project
copyArtifacts(
projectName: 'Folder/Multi branch Pipeline/feature%2Fallow-artifact-copy',// the name of project, as you find it from the root of jenkins
selector: lastSuccessful(), // selector to select the build to copy from. If not specified, latest stable build is used.
filter: 'projects/Output/myzip.zip', // ant-expression to filter artifacts to copy, Attention! Filter is case sensitive
target: 'sources/deploy/', // target directory to copy to, intermediate folders will be created
flatten: true, // ignore directory structures of artifacts, Artifact will be placed at 'sources/deploy/myzip.zip'. Is the option false, you find it at 'projects/Outpu/myzip.py'
optional: false, // do not fail the step even if no appropriate build is found.
fingerprintArtifacts: true, // fingerprint artifacts to track builds using those artifacts
)
And don't forget to allow artifact copy in the project you want to take the artifact from. Add this to the Jenkinsfile of 'Folder/Multi branch Pipeline/feature%2Fallow-artifact-copy'. Use absolute paths, to avoid issues if you move some projects around.
options {
disableConcurrentBuilds()
timeout(time: 30, unit: 'MINUTES')
copyArtifactPermission('/Other-folder/Multi branch Pipeline Test/*, /second Folder/*') // allow all the projects or branches of 'Other-folder/Multi branch Pipeline Test' and 'second Folder' to copy artifacts of this job
} // end of options

Resources