Reputation: 6129
I'm new to groovy and I wrote this script for a Jenkins pipeline job that is working, The script acts as a scheduler so it checks the files that were changed in the last commit and then compares them with pre-defined paths so ut will schedule a different job for that specific component.
But I think it is going to grow more and more and I pretty much convinced that this is not the most efficient way to do it.
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\""
}
}
}
Upvotes: 0
Views: 2527
Reputation: 28564
//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
}
}
Upvotes: 1
Reputation: 20699
Small fix to @daggett answer:
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
}
Upvotes: 0