使用jenkins管道插件实现动态并行构建的想法 [英] Ideas to implement dynamic parallel build using jenkins pipeline plugin
问题描述
我需要为构建并行运行一组任务,该构建的任务是动态的,可能会更改.在执行以下操作时,我需要一些帮助.
I have a requirement to run a set of tasks for a build in parallel, The tasks for the build are dynamic it may change. I need some help in the implementation of that below are the details of it.
构建的任务详细信息将在xml中动态生成,该xml包含必须并行/串行执行哪些任务的信息
I tasks details for a build will be generated dynamically in an xml which will have information of which tasks has to be executed in parallel/serial
示例:
说有一个版本A.
具有下面的任务和执行顺序,必须先执行第一个任务1,然后再执行task2,然后将task3并行执行,接下来是任务4
Which had below task and the order of execution , first task 1 has to be executed next task2 and task3 will be executed in parallel and next is task 4
任务1
task2,task3
task4
task1
task2,task3
task4
这些细节将在动态生成的xml中,我如何使用管道插件解析该xml并相应地安排任务.我首先需要一些想法.
These details will be in an xml dynamically generated , how can i parse that xml and schedule task accordingly using pipeline plugin. I need some idea to start of with.
推荐答案
You can use Groovy to read the file from the workspace (readFile
) and then generate the map containing the different closures, similar to the following:
parallel(
task2: {
node {
unstash('my-workspace')
sh('...')
}
},
task3: {
node {
unstash('my-workspace')
sh('...')
}
}
}
为了生成这样的数据结构,您只需在Groovy中遍历使用XML解析读取的任务数据,再遍历先前读取的文件内容.
In order to generate such data structure, you simply iterate over the task data read using XML parsing in Groovy over the file contents you read previously.
昨天,我有一次关于管道的演讲,并提供了非常相似的示例(演示,滑动34ff.).相反,我从另一个命令输出中读取了任务"列表.完整的代码可以找到
By occasion, I gave a talk about pipelines yesterday and included very similar example (presentation, slide 34ff.). In contrast, I read the list of "tasks" from another command output. The complete code can be found here (I avoid pasting all of this here and instead refer to this off-site resource).
魔术的种类如下:
def parallelConverge(ArrayList<String> instanceNames) {
def parallelNodes = [:]
for (int i = 0; i < instanceNames.size(); i++) {
def instanceName = instanceNames.get(i)
parallelNodes[instanceName] = this.getNodeForInstance(instanceName)
}
parallel parallelNodes
}
def Closure getNodeForInstance(String instanceName) {
return {
// this node (one per instance) is later executed in parallel
node {
// restore workspace
unstash('my-workspace')
sh('kitchen test --destroy always ' + instanceName)
}
}
}
这篇关于使用jenkins管道插件实现动态并行构建的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!