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.
Jenkinsfile
per submodule, and want to build them independently, why do you specific the parent project's URL inuserRemoteConfigs
then? You could just directly use the URL to the respective submodule's Git repository, and build them as regular Git projects. Or is your goal to build only those changes tomoduleA
andmoduleB
that are actually taken into use by the parent project?git url: "https://[path]/moduleA.git”
config in each Jenkinsfile) would indeed work. However that does not explain why a rebuild of project A is triggered when a file outside of its specifiedincludeRegions
is changed.