node {
    // Git checkout before load source the file
    checkout scm

    def yaml2Map = load "./sharedLibs/yaml2Map.groovy"
    def k3sUtils = load "./sharedLibs/k3sUtil.groovy"
    def GLOBAL_CONFIG = yaml2Map.read('global.yaml').project
    dir("k3s-INFRA/DEV/traefik-rules") {

        def PROJECT_CONFIG = yaml2Map.read('cfg.yaml').project
        def CONFIG = yaml2Map.merge(PROJECT_CONFIG, GLOBAL_CONFIG)
        
        services = services.split(',')
        for(service in services) {
            stage("Apply service ${service}") {
                final foundFiles = findFiles(glob: "${service}/*.yaml")
                foundFiles.each {
                    println "applying ${it}..."
                    k3sUtils.applyService(CONFIG.k3s, "${it}")
                }
            }
        }
        // final foundFiles = findFiles(glob: '*/*.yaml')
        // foundFiles.each{ 
        //     stage("apply config ${it}") {
        //         println "applying ${it}..."
        //         k3sUtils.applyService(CONFIG.k3s, "${it}")
        //     }
        // }
    }
}