如果由作业 DSL 创建,则多分支管道无法工作

如果由作业 DSL 创建,则多分支管道无法工作

我有一个作业 DSL 作业脚本,它为每个管道创建文件夹,并且在每个文件夹内创建相对的多分支管道。经过一些语法方面的折腾后,我设法创建了该脚本,但问题是当 DSL 作业创建多分支管道时,它不会扫描或构建任何东西,但是使用精确配置手动创建多分支管道是可行的。知道这是为什么吗?

我尝试重新启动詹金斯,但仍然只有在手动创建多分支管道时才有效

这是代码脚本:

import jenkins.model.Jenkins

def map = [
    'a': 'so',
    'b': 'no',
    'c': 'one',
    'd': 'told',
    'e': 'life',
    'f': 'was',
    'g': 'gonna',
    'h': 'be',
    'i': 'this',
    'j': 'way',
    'k': 'your',
    'l': 'love',
    'm': 'is joke',
    'n': 'you broke',
    'o': 'your love life`s DOA',
    'p': 'it`s like you always stuck in the second gear when',
    'q': 'it hasn`t been your day',
    'r': 'your week, your month',
    's': 'or even your year',
]

map.each { service, service_webhook_token ->
    def folderPath = "${Jenkins.getInstance().getItemByFullName(service)}"
    if (folderPath != null) {
        folder("${service}") {
            displayName("${service}")
            description("This is a folder for service ${service}")
        }

        def environments = ["${service}/DEV", "${service}/STG", "${service}/TST"]

        for (environment in environments) {
            if (!Jenkins.getInstance().getItemByFullName(environment)) {
                multibranchPipelineJob("${environment}") {
                    branchSources {
                        branchSource {
                            source {
                                git {
                                    id("${service}-${environment}")
                                    credentialsId("friends")
                                    remote("link/${service}.git")
                                    traits {
                                        headRegexFilter {
                                            if (environment.contains('DEV')) {
                                                regex("develop")
                                            } else if (environment.contains('STG')) {
                                                regex("staging")
                                            } else if (environment.contains('TST')) {
                                                regex("tst")
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    factory {
                        templateBranchProjectFactory {
                            filterBranches(true)
                        }
                    }
                    orphanedItemStrategy {
                        discardOldItems {
                            numToKeep(1)
                        }
                    }
                    triggers {
                        computedFolderWebHookTrigger {
                            token("${service_webhook_token}") // Using the token associated with the service
                        }
                    }
                    properties {
                        templateConfigFolderProperty {
                            tier {
                                configurationProvider {
                                    scmPipelineConfigurationProvider {
                                        scm {
                                            gitSCM {
                                                browser { "" }
                                                branches {
                                                    branchSpec {
                                                        name("*/main")
                                                    }
                                                }
                                                gitTool("")
                                                userRemoteConfigs {
                                                    userRemoteConfig {
                                                        name('repo')
                                                        refspec('+refs/heads/*:refs/remotes/@{remote}/*')
                                                        url("link/${service}-config.git")
                                                        credentialsId("friends")
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                println "Job ${environment} already exists. Skipping creation."
            }
        }
    } else {
        println "Folder ${service} already exists. Skipping creation."
    }
}

相关内容