仅针对特定路径的更改触发 Jenkinsfile 构建

Only trigger Jenkinsfile build for changes to certain paths

我在同一个存储库中有一个用于多个多分支管道的 Jenkinsfile,令我感到沮丧的一件事是,当我进行任何更改时,所有管道都会被触发,而实际上通常只需要触发一个管道。

是否可以使用 Jenkins 文件选择性地包含或排除将触发构建的路径?

我正在考虑类似于下面的内容,但是查看 the documentation 我看不到任何关于这种简单方法的参考。但是,我是 Jenkins 的新手,所以我希望我遗漏了一些东西。

pipeline {
    agent {
        label 'Codebuild'
    }
    trigger {
        include '/some/path
    }
    stages {
        stage('Do the magic') {
            steps {
                script {
                    ...do stuff here...
                }
            }
        }       
    }
}

您可以使用 when condition 根据给定条件确定是否应执行该阶段。
在这种情况下,您可以使用 option/condition changeset 将阶段执行限制为仅在特定文件发生更改的情况下执行。

有关 whenchangeset 选项的更多信息:
https://www.jenkins.io/doc/book/pipeline/syntax/#when

示例:

stages {
    stage('Do the magic') {
       // Below condition you can change based on your usecase
        when { changeset "src/*.js"}
        steps {
            sh "make build"
            ...

        }
    }
}