Configuration of includedRegions in Jenkinsfile?

2019-02-12 15:28发布

问题:

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.

回答1:

This is not supported yet as this issue implies.



回答2:

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"]]])