在Jenkins管道作业中使用includedRegions进行SCM轮询

在Jenkins管道作业中使用includedRegions进行SCM轮询,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,我正在使用GitLab。给定多个目录,例如folder1、folder2、folder3。我希望作业仅在folder1下有任何更改时运行。是否有人能够让includedRegions在Jenkins管道工作 checkout( [ $class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false,

我正在使用GitLab。给定多个目录,例如folder1、folder2、folder3。我希望作业仅在folder1下有任何更改时运行。是否有人能够让includedRegions在Jenkins管道工作

    checkout(
      [
        $class: 'GitSCM', 
        branches: [[name: '*/master']], 
        doGenerateSubmoduleConfigurations: false, 
        extensions: [[$class: 'PathRestriction', excludedRegions: '', includedRegions: 'folder1/.*']], 
        submoduleCfg: [], 
        userRemoteConfigs: [[credentialsId: 'user', url: 'ssh://something/experiment.git']]
      ]
    )

如果使用管道,则签出步骤将在启动生成后执行


管理这一点的唯一方法是使用groovy检查变更集,如果包含的目录中没有任何更改,则跳过构建。

我设法使其正常工作。由于签出将在我们Jenkins的项目中使用,因此我编写了共享库以方便使用

示例:strosparsecheckout.groovy

#!/usr/bin/env groovy

import hudson.plugins.git.extensions.impl.SparseCheckoutPath

def call(paths) {
    def list = paths.split('\n')
    def sparsePaths = []
    def isDefaultAdded = false
    list.each {
        def path = it - '/.*'
        def sparsePath = new SparseCheckoutPath(path)
        sparsePaths.push(sparsePath)
    }

    return sparsePaths
}
#!/usr/bin/env groovy

def call(Map namedargs) {
    checkout(
        [
            $class: 'GitSCM',
            branches: [[name: "${namedargs.branch}"]],
            extensions: [
                [$class: 'LocalBranch', localBranch: '**'],
                [$class: 'RelativeTargetDirectory', relativeTargetDir: "${namedargs.target_dir}"],
                [$class: 'SparseCheckoutPaths', sparseCheckoutPaths: strToSparseCheckout("${namedargs.included_regions}")],
                [$class: 'PathRestriction', excludedRegions: """${namedargs.excluded_regions}""", includedRegions: """${namedargs.included_regions}"""]
            ],
            userRemoteConfigs: [[credentialsId: 'user', url: "${namedargs.url}"]]
        ]
    )
}
示例:sparseCheckout.groovy

#!/usr/bin/env groovy

import hudson.plugins.git.extensions.impl.SparseCheckoutPath

def call(paths) {
    def list = paths.split('\n')
    def sparsePaths = []
    def isDefaultAdded = false
    list.each {
        def path = it - '/.*'
        def sparsePath = new SparseCheckoutPath(path)
        sparsePaths.push(sparsePath)
    }

    return sparsePaths
}
#!/usr/bin/env groovy

def call(Map namedargs) {
    checkout(
        [
            $class: 'GitSCM',
            branches: [[name: "${namedargs.branch}"]],
            extensions: [
                [$class: 'LocalBranch', localBranch: '**'],
                [$class: 'RelativeTargetDirectory', relativeTargetDir: "${namedargs.target_dir}"],
                [$class: 'SparseCheckoutPaths', sparseCheckoutPaths: strToSparseCheckout("${namedargs.included_regions}")],
                [$class: 'PathRestriction', excludedRegions: """${namedargs.excluded_regions}""", includedRegions: """${namedargs.included_regions}"""]
            ],
            userRemoteConfigs: [[credentialsId: 'user', url: "${namedargs.url}"]]
        ]
    )
}
在管道中,我们可以调用sparseCheckout共享库

pipeline {
    stage('Checkout') {
        sparseCheckout(
            [
                url: 'ssh://path.to.git/something.git',
                branch: 'refs/heads/master',
                target_dir: 'something',
                excluded_regions: "${params.excluded_regions}",
                included_regions: "${params.included_regions}"
            ]
        )
    }
}