Configuration of includedRegions in Jenkinsfile? - jenkins

How can I limit the scope of a Jenkins pipeline project to only be built if a file in specific subdirectory is changed using Jenkinsfile?
I have a single Git repository with two directories. Each directory contains a separate subproject and I would like to build each subproject separately by Jenkins using Jenkinsfile. The project has the following file structure:
parent
|
+- subA
| |
| + Jenkinsfile
| + more files related to sub project A
|
+- subB
|
+ Jenkinsfile
+ more files related to sub project B
The Jenkinsfile for subA has the following configuration:
checkout scm: [
$class: 'GitSCM',
branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'https://[path]/parent.git']],
extensions: [[
$class: 'PathRestriction', includedRegions: 'subA/.*'
]]
]
The Jenkinsfile for subB is similar, the only difference being that it has specified subB as includedRegions.
In the Jenkins server, I have created two pipeline projects and pointed them to each Jenkinsfile respectively. If a file is changed in the folder subA, Jenkins pipeline project A is triggered and if a file is changed in folder subB, Jenkins pipeline project B is triggered, which is what I expect.
The problem is that the Jenkins pipeline project A is also triggered if a file is changed in subB and vice versa.
Jenkins version: 2.3
Note:
Configuring the setting Additional Behaviours -> Polling ignores commits in certain paths -> Included Regions to subA/.* or subB/.* respectively in the old Jenkins (ver 1.649) GUI results in the expected behavior.
Update:
Adding excludedRegions to the Jenkinsfiles, e.g.
checkout scm: [
$class: 'GitSCM',
branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'https://[path]/parent.git']],
extensions: [[
$class: 'PathRestriction', excludedRegions: '', includedRegions: 'subA/.*'
]]
]
does not change the behavior. Both subprojects are still rebuilt, despite files are only changed in one subdirectory.

This is not supported yet as this issue implies.

There is an open issue preventing proper function: https://issues.jenkins-ci.org/browse/JENKINS-36195
It has a workaround, which is to disable remote polling ([$class: 'DisableRemotePoll']):
checkout([$class: 'GitSCM',
branches: [[name: "*/master"]],
extensions: [
[$class: 'PathRestriction', excludedRegions: '', includedRegions: '<fill me in with regex \n delimited, leave excludedRegions as empty>'],
[$class: 'DisableRemotePoll']
],
submoduleCfg: [],
userRemoteConfigs: [[url: "<my git url>", credentialsId: "$GIT_KEY"]]])

Related

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 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!

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