Jenkins:如何在Jenkins声明式管道中实现并行动态阶段 [英] Jenkins:How to Achieve parallel dynamic stages in jenkins declarative pipeline

查看:135
本文介绍了Jenkins:如何在Jenkins声明式管道中实现并行动态阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究声明式管道.我正在尝试实现动态阶段,该阶段应该与定义的代理平行地分配各个阶段.当我探索时,我学会了如何实现动态顺序阶段.下面是我的示例代码.

I am working on declarative pipeline. I am trying to achieve dynamic stages which should distribute the stages parallel with the agents defined. When i explored i learned how to achieve Dynamic sequential stages. Below is my sample code.

我现在的问题是,如何与我拥有的代理商实现并行阶段.例如,如果我有3个代理,则这5个阶段应在代理并行中并行运行.我尝试使用并行测试,但无法正常工作.请帮助我进一步改进!

My problem now is, how to achieve parallel stages with agents i have. For example, if i have 3 agents all the 5 stages should run parallel in the agents parallel. I tried using parallel tests but not working. Please help me to improve further !

def learn
pipeline {
    agent none

    stages {
        stage('Dynamic Stages') {
          steps {
                script {
                    learn = ["1", "2", "3", "4", "5"]
                    for(int i=0; i < list.size(); i++) {

                        stage(list[i]){
                            echo "value: $i"
                        }
                    }
                }
            }

        }
    }
}

推荐答案

以下内容应并行运行所有阶段.詹金斯将采取任何可用的节点.

The following should run all stages in parallel. Jenkins will just take whatever node is available.

def learn
pipeline {
    agent none

    stages {
        stage('Dynamic Stages') {
          steps {
                script {
                    learn = ["1", "2", "3", "4", "5"]
                    def builders = [:]
                    for(i in learn) {
                        def value = i // Need to bind the label variable before the closure - can't do 'for (i in learn)
                        builders[i] = {
                            node {
                                echo "value: $i"
                            }
                        }
                    }
                    parallel builders
                }
            }
        }
    }
}

这篇关于Jenkins:如何在Jenkins声明式管道中实现并行动态阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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