Jenkins管道(并行&&动态)吗? [英] Jenkins pipeline (parallel && dynamically)?

查看:115
本文介绍了Jenkins管道(并行&&动态)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的并行管道(请参阅代码),该管道与Jenkins 2.89.2一起使用.另外,我使用参数,现在希望能够通过在作业执行之前提供参数来自动增加/减少deployVM A..Z阶段的数量.

I have simple parallel pipeline (see code) which I use together with Jenkins 2.89.2. Additionally I use parameters and now want to be able to in-/decrease the number of deployVM A..Z stages automatically by providing the parameter before job execution.

如何通过提供参数来动态构建管道?

How can I dynamically build my pipeline by providing a parameter?

到目前为止已研究:

  • Jenkins pipeline script created dynamically - Not getting this to work with my Jenkins version
  • Can I create dynamically stages in a Jenkins pipeline? - Not working either

我想要的伪代码-动态生成:

The pseudo code of what I want - dynamic generation:

pipeline {

    agent any

    parameters {
        string(name: 'countTotal', defaultValue: '3')
    }

    stages {

       stage('deployVM') {

        def list = [:]
        for(int i = 0; i < countTotal.toInteger; i++) {
            list += stage("deployVM ${i}") {
                steps {
                    script {
                        sh "echo p1; sleep 12s; echo phase${i}"
                    }

                }
            }
        }

        failFast true
        parallel list
       }

   }

}

到目前为止,我的代码是-并行执行但是静态的:

The code I have so far - executes parallel but is static:

pipeline {

    agent any
    stages {

       stage('deployVM') {
        failFast true
        parallel {
            stage('deployVM A') {
                steps {
                    script {
                        sh "echo p1; sleep 12s; echo phase1"
                    }

                }
            }
            stage('deployVM B') {
                steps {
                    script {
                        sh "echo p1; sleep 20s; echo phase2"
                    }

                }
            }
        }
       }

   }

}

推荐答案

尽管该问题假定使用声明性管道,但我还是建议使用

Although the question assumes using declarative pipeline I would suggest to use scripted pipeline because it's way more flexible.
Your task can be accomplished this way

properties([
    parameters([
        string(name: 'countTotal', defaultValue: '3')
    ])
])

def stages = [failFast: true]
for (int i = 0; i < params.countTotal.toInteger(); i++) {
    def vmNumber = i //alias the loop variable to refer it in the closure
    stages["deployVM ${vmNumber}"] = {
        stage("deployVM ${vmNumber}") {
            sh "echo p1; sleep 12s; echo phase${vmNumber}"
        }
    }
}

node() {
    parallel stages
}

还可以查看片段生成器,它允许您可以生成一些脚本化管道代码.

Also take a look at snippet generator which allows you to generate some scripted pipeline code.

这篇关于Jenkins管道(并行&amp;&amp;动态)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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