gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:03:26 25 4
gpt4 key购买 nike

目标

为 java 更改更频繁地构建我们的 repo,为 docker 基础镜像更改更频繁地构建我们的 repo。

情况

我们有两个多分支管道作业:build-javabuild-base-docker。我们希望 build-base-docker 仅在推送包含 /docker 下的更改时触发 master 和 feature 分支。

方法

关注云蜂 How to Customize Checkout for Pipleine Multibranch? doc 我们实现了以下更改。

但是,当我们不想要时,对 src/main/java/foo.java 的更改会触发该分支的 build-base-docker 作业它这样做。

这真的只是JENKINS-36195 bug吗?还是我做错了什么导致意外触发?

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']]

])
}
...
}

最佳答案

如果 Jenkins 不能很好地处理多分支管道的路径限制,那么我们可以从我们的管道代码(或从共享库来减少重复)中进行处理。

如果我们有多个 checkout ,这可能会出现一些问题,因为所有这些都会被报告。但是,只要它们不使用相同的布局,误报的风险就会很低。例如,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
}

关于git - Jenkins Multibranch Pipeline 缺乏对 PathRestriction 触发器的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50456688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com