Multibranch pipelines not working if it's created by job DSL

20 Views Asked by At

i have a job DSL job script that creates folders for each pipeline and inside each folder it creates the relative multi-branch pipeline.

After suffering a bit with syntax as I'm new to it, I managed to to create that script.

But the issue is when the multi-branch pipeline is created by DSL job, it doesn't scan nor build anything.

However creating manually mulitbranch pipeline with exact configuration works.

any idea why is that?

I tried to restart jenkins but still only works if multibranch pipeline is created manually

here's the code script:

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."
    }
}

by the way, this script doesn't create as well the folders as it says folders exist but they're actually not. i had to set

if (folderPath == null)

to

if (folderPath != null)

so it creates the folders with multi-branch pipelines

I tried to restart Jenkins but didn't work, also did a research bu couldn't find relative helpful article.

0

There are 0 best solutions below