在声明性管道中,我可以使并行阶段运行相同的代码任意次(在任意数量的节点上)吗? [英] In Declarative Pipeline, can I make a parallel stage that runs the same code an arbitrary number of times (on an arbitrary number of nodes)?

查看:41
本文介绍了在声明性管道中,我可以使并行阶段运行相同的代码任意次(在任意数量的节点上)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条流水线可以做很多事情,但是在构建结束时,我想在同一个构建节点的多个副本上运行测试.这些测试将完全相同,并且都将在同一VM的新克隆上运行.

I have a pipeline that does a handful of things, but at the end of the build I would like to run tests on multiple copies of the same build node. These tests will all be identical, and they will all run on fresh clones of the same VM.

并行块可以做到这一点吗?据我所知,我可以做这样的事情:

Can the parallel block do something to that degree? As far as I can tell, I COULD do something like this:

...
stage('Parallel Testing'){
    parallel{
        stage('Run1'){
            agent{
                label "my_test_machines"
            }
            steps{
                run_my_tests()
            }
        }
        stage('Run2'){
            agent{
                label "my_test_machines"
            }
            steps{
                run_my_tests()
            }
        }
        ...
    }
}
...

但是,这显然很丑陋,如果我想执行多个并行节点,这将变得很糟糕.有什么方法可以使parallel {}在for循环上运行,或者以其他方式创建任意数量的阶段/节点?

But that is obviously pretty ugly and would get awful if I wanted to do more than a few parallel nodes. Is there some way to make parallel{} run on a for loop, or otherwise create an arbitrary number of stages/nodes?

推荐答案

您可以像这样对并行阶段进行编程:

You can program your parallel stages like this:

stage('Parallel Testing') {
   steps {
      script {
         def builders = getParallelBuilders()
         parallel builders
      }
   }
}

其中

def getParallelBuilders() {
    def builders = [:].asSynchronized()
    def my_runs = ["Run1", "Run2", "Run3"]

    my_runs.each { stage_name -> 
        def final_name = stage_name
        builders[final_name] = {
            stage (final_name) {
                node("my_test_machines") {
                   run_my_tests()
                }
            }
        }
    }
    return builders
}

这篇关于在声明性管道中,我可以使并行阶段运行相同的代码任意次(在任意数量的节点上)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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