How to loop parametrized parallel stages in Jenkins declarative pipeline?

大城市里の小女人 提交于 2020-03-26 04:06:25

问题


How to loop parametrized parallel stages in Jenkins declarative pipeline? (Or scripted pipeline, if declarative is not able to)

Here is my simple pseudo example. How to loop ('deploy serverN') stages?

Array may have 1..n variables.

I would not like to duplicate code. There must be a way in Jenkins pipelines?? Or should I use matrix. I have tried a few, but not succesfully.

@Library('adm-jenkins-lib@trunk')

def SERVERS = ['server1.com','server2.com',...]

deployPipeline([servers: SERVERS, manage_tasks: TASKS])

...
def call(Map params) {  
    pipeline {
        agent any
        environment {
        }
        stages {
            stage ('common task') {
            }       
            stage ('Deploying..') { 
                parallel {
                    stage ('deploy server1') {
                        stages {
                            stage ('deploy tasks #1') {
                                steps { ... }
                            }
                            stage ('deploy tasks #2') {
                                steps { ... }
                            }
                        }
                    stage ('deploy server2') {
                        stages {
                            stage ('deploy tasks #1') {
                                steps { ... }
                            }
                            stage ('deploy tasks #2') {
                                steps { ... }
                            }
                        }
                    }
                }
            }
        }
    }
}

I have tried also this kind of approach but it is not working perfect because previous stages are not dependent on next ones.

            stage ('deploy serverX') {
                when { expression { params.manage_tasks =~ /task01/ } }
                steps {
                    script {
                        servers = params.servers
                        servers.each { server ->
                            deploys[server] = {
                                sh "run task#1 stuff.."
                            }
                        }
                        parallel deploys
                    }
                }
            }

It should look like this in Blue Ocean (but dynamically created) : It should look like this in Blue Ocean


回答1:


I have the solution:

Update Blue Ocean at least to version 1.22 to see pipeline correctly.

Install library https://github.com/comquent/imperative-when as @zett42 suggested.

This example is scripted pipeline. (I did not found solution for declarative pipeline)

@Library('adm-jenkins-lib@trunk') _

properties([
    parameters([
        string(name: 'countTotal', defaultValue: '4'),
        choice(name: 'servers', choices: ['all', '1', '2','3','4'], description: 'Run on specific platform'),
        choice(name: 'manage_steps', choices: ['all_tasks','common_task','deploy_task','test_task'], description: 'Choose task')
    ])
])

node{
    stage('common task'){
        when(params.manage_steps ==~ /common_task|all_tasks/) {
            sh "echo common task"   
        }
    }
    def stages = [failFast: true]
    for (int i = 1; i < params.countTotal.toInteger()+1; i++) {
        if (params.servers == 'all' || params.servers == i.toString() )
        {
            def vmNumber = i //alias the loop variable to refer it in the closure
            stages["server${vmNumber}"] = {
                stage("deploy ${vmNumber}") {
                        when(params.manage_steps ==~ /deploy_task|all_tasks/) {
                        sh "echo deploy; sleep 5"
                  }
                }
                stage("test ${vmNumber}") {
                        when(params.manage_steps ==~ /test_task|all_tasks/) {
                        sh "echo testing; sleep 5"
                        }
                }
            }
        }
    }
    parallel stages
}


BlueOcean Pipeline example1 BlueOcean Pipeline example2



来源:https://stackoverflow.com/questions/60321508/how-to-loop-parametrized-parallel-stages-in-jenkins-declarative-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!