Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration of includedRegions in Jenkinsfile?

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.

like image 628
matsev Avatar asked May 18 '16 08:05

matsev


People also ask

What is checkout scm in Jenkinsfile?

1. The checkout step will checkout code from source control; scm is a special variable which instructs the checkout step to clone the specific revision which triggered this Pipeline run.

How do you write a Jenkinsfile pipeline?

To create a simple pipeline from the Jenkins interface, perform the following steps: Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK. In the Script text area of the configuration screen, enter your pipeline syntax.


2 Answers

This is not supported yet as this issue implies.

like image 127
Oxillery Avatar answered Oct 05 '22 21:10

Oxillery


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"]]])
like image 26
Jack Davidson Avatar answered Oct 05 '22 23:10

Jack Davidson