否则会影响常规的最佳实践

时间:2019-10-31 13:08:51

标签: groovy jenkins-pipeline jenkins-groovy

我是groovy的新手,我为正在运行的 Jenkins管道作业编写了此脚本, 该脚本充当调度程序,因此它会检查上次提交中更改的文件,然后将它们与预定义的路径进行比较,以便ut将为该特定组件调度其他作业。

但是我认为它会越来越多,我非常相信这不是最有效的方法。

stage("check file paths that changed in this build") {
    ChangedFilesList = gitfunction.getChangedFilesList(changedFiles)
    ChangedFilesList.each {
        println "Changes in file: ${it}"
        def file = "${it}"
        if (file ==~ /(?s).*component\/.*/ || file ==~ /(?s).*component0\/.*/){
            print "scheduling component tests for component0"
            build job: "Component_test_CI", parameters: [[$class: 'StringParameterValue', name: "SERVICE_NAME", value: "component0"]]
            description =  "component0"
        } else if (file ==~ /(?s).*component1\/.*/){
            print "scheduling component tests for component1"
            build job: "Component_test_CI", parameters: [[$class: 'StringParameterValue', name: "SERVICE_NAME", value: "component1"]]
            description =  "component1"
        } else if (file ==~ /(?s).*component1\/.*/){
            print "scheduling component tests for component1"
            build job: "Component_test_CI", parameters: [[$class: 'StringParameterValue', name: "SERVICE_NAME", value: "component1"]]
            description =  "component1"
        } else if (file ==~ /(?s).*component2\/.*/){
            print "scheduling component tests for component2"
            build job: "Component_test_CI", parameters: [[$class: 'StringParameterValue', name: "SERVICE_NAME", value: "component2"]]
            description =  "component2"
        } else if (file ==~ /(?s).*component3\/.*/){
            print "scheduling component tests for component3"
            build job: "Component_test_CI", parameters: [[$class: 'StringParameterValue', name: "SERVICE_NAME", value: "component3"]]
            description =  "component3"
        } else if (file ==~ /(?s).*component4\/.*/){
            print "scheduling component tests for component4"
            build job: "Component_test_CI", parameters: [[$class: 'StringParameterValue', name: "SERVICE_NAME", value: "component4"]]
            description =  "component4"
        } else if (file ==~ /(?s).*component5\/.*/){
            print "scheduling component tests for component5"
            build job: "Component_test_CI", parameters: [[$class: 'StringParameterValue', name: "SERVICE_NAME", value: "component5"]]
            description =  "component5"
        } else {
            sh "printf \"\\e[95m----------------- no specific module was changed - Will not trigger Component_test_CI job --------------------\\e[0m\\n\""
        }
    }
}

2 个答案:

答案 0 :(得分:1)

//define mapping pattern - component name
//it's possible to store this in some config like yaml or json
def path2component = [
    /(?s).*component\/.*/  : "component0",
    /(?s).*component0\/.*/ : "component0",
    /(?s).*component1\/.*/ : "component1",

]
//iterate pattern
path2component.find{pattern,cname->
    if(file.find(pattern)){
        println "scheduling component tests for ${cname}"
        build job: "Component_test_CI", parameters: [[$class: 'StringParameterValue', name: "SERVICE_NAME", value: cname]]
        description=cname
        return true //found
    }else {
        return false //continue searching
    }
}

答案 1 :(得分:0)

对@daggett答案的小修正:

String cname
file.eachMatch(/(component)(\d?)/){ cname = it[ 1 ] + ( it[2] ?: '0' ) }
if(cname){
    print "scheduling component tests for ${cname}"
    build job: "Component_test_CI", parameters: [[$class: 'StringParameterValue', name: "SERVICE_NAME", value: cname]]
    description =  cname
}