Jenkins pipeline (parallel && dynamically)?

后端 未结 2 1828
孤城傲影
孤城傲影 2021-02-09 16:50

Question

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

2条回答
  •  礼貌的吻别
    2021-02-09 17:29

    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.

提交回复
热议问题