Can I create dynamically stages in a Jenkins pipeline?

后端 未结 6 1599
南方客
南方客 2020-12-04 15:25

I need to launch a dynamic set of tests in a declarative pipeline. For better visualization purposes, I\'d like to create a stage for each test. Is there a way to do so?

6条回答
  •  一整个雨季
    2020-12-04 16:07

    As JamesD suggested, you may create stages dynamically (but they will be sequential) like that:

    def list
    pipeline {
        agent none
        options {buildDiscarder(logRotator(daysToKeepStr: '7', numToKeepStr: '1'))}
        stages {
            stage('Create List') {
                agent {node 'nodename'}
                steps {
                    script {
                        // you may create your list here, lets say reading from a file after checkout
                        list = ["Test-1", "Test-2", "Test-3", "Test-4", "Test-5"]
                    }
                }
                post {
                    cleanup {
                        cleanWs()
                    }
                }
            }
            stage('Dynamic Stages') {
                agent {node 'nodename'}
                steps {
                    script {
                        for(int i=0; i < list.size(); i++) {
                            stage(list[i]){
                                echo "Element: $i"
                            }
                        }
                    }
                }
                post {
                    cleanup {
                        cleanWs()
                    }
                }
            }
        }
    }
    
    

    That will result in: dynamic-sequential-stages

提交回复
热议问题