使用 jenkins 管道插件实现动态并行构建的想法 [英] Ideas to implement dynamic parallel build using jenkins pipeline plugin

查看:28
本文介绍了使用 jenkins 管道插件实现动态并行构建的想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要并行运行一组构建任务,构建任务是动态的,可能会发生变化.我需要一些帮助来实现以下是它的细节.

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 中动态生成,其中包含必须并行/串行执行哪些任务的信息

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必须执行下一个任务2,任务3将并行执行,接下来是任务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
任务2,任务3
任务4

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.

推荐答案

可以使用 Groovy 从工作区读取文件(readFile) 然后生成包含不同闭包的映射,类似于以下:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆