简体   繁体   中英

How to loop parametrized parallel stages in Jenkins declarative pipeline?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM