简体   繁体   English

Jenkins Multibranch Pipeline缺乏对PathRestriction触发器的支持

[英]Jenkins Multibranch Pipeline Lacks Support for PathRestriction triggers

Goal 目标

Build our repo more frequently for java changes and less frequently for docker base image changes. 针对Java更改更频繁地构建我们的repo,而对于docker基础映像更改则更少。

Situation 情况

We have two multibranch pipeline jobs: build-java and build-base-docker . 我们有两个multibranch管道作业: build-javabuild-base-docker We want the build-base-docker to trigger for master & feature branches only when pushes contain changes beneath /docker . 我们希望build-base-docker仅在推送包含/docker下的更改时才触发主分支和功能分支。

Approach 途径

Following cloudbees How to Customize Checkout for Pipleine Multibranch? 关注cloudbees 如何自定义Pipleine Multibranch的Checkout? doc we implemented the changes below. doc我们实施了以下更改。

However, a changes to src/main/java/foo.java triggers the build-base-docker job for that branch when we'd don't want it to do so. 但是,当我们不希望它执行时,对src/main/java/foo.java会触发该分支的build-base-docker src/main/java/foo.java作业。

Is this really just JENKINS-36195 bug or am I doing something wrong which results in the unexpected trigger? 这真的只是JENKINS-36195错误还是我做错了导致意外触发?

node('java-build') {

stage ('git checkout') {
    checkout([
            $class: 'GitSCM',
            branches: scm.branches,
            extensions: scm.extensions + [
                [$class: 'PathRestriction', excludedRegions: '.*', includedRegions: 'docker/.*']
            ],
            userRemoteConfigs: [[credentialsId: 'our-git-repo-deploy-key', url: 'git@github.com:we/our-repo.git']]

    ])
}
...
}

If Jenkins can't handle path restrictions on a multibranch pipelines well, then we do it from our pipeline code (or from a shared library to reduce duplication). 如果Jenkins无法很好地处理多分支管道的路径限制,那么我们可以从管道代码(或从共享库中)来减少重复。

If we have multiple checkouts, this can get a little problematic as all will be reported. 如果我们有多个结帐,这可能会产生一些问题,因为所有报告都会报告。 However, provided they don't use the same layout, the risk of false positives is low. 但是,如果它们不使用相同的布局,则误报的风险很低。 Eg, the jenkins-shared-library usually sees changes beneath /vars dir not docker. 例如,jenkins-shared-library通常会看到/ vars目录下的更改而不是docker。

node() {
    if ( ! doChangesRequireBuild('^docker/.*')) {
        currentBuild.result = 'ABORTED'
        return 'Changes not made to docker/... base image files. Aborting build'
    }
}

/**
 * Provide List of changed file paths since last successful build
 */
def getChangedFiles() {
    def found = []

    for (def changeSet in currentBuild.getChangeSets()) {
        for (def change in changeSet.logs) {
            for (def path in change.paths) {
                found.add(path.path)
            }
        }
    }
    return found
}

/**
 * do changes since last successful build mandate a build?
 * @param pathRegex path restriction regex pattern
 * @return true if any file paths match the regex
 */
boolean doChangesRequireBuild(def pathRegex) {

    for (def path in getChangedFiles()) {
        if ( path ==~ pathRegex) {
            return true
        }
    }
    return false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM