How to run multi stages at the same time using multi parallel blocks?

僤鯓⒐⒋嵵緔 提交于 2019-12-13 03:45:00

问题


Here, I need to execute both Parallel test 1 and Parallel test 2 at the same time.

When I tried to put a parallel block on top of these, it throws an error since it mentioned like this in the official site Note: that a stage must have one and only one of steps, stages, or parallel.

pipeline {
agent any
stages {
    stage('Parallel Test 1') {
        parallel {
            stage('Block 1 - Stage 1') {
                steps {
                    echo "Block 1 - Stage 1"
                    build(job: 'jenkins_job_1')
                }
            }
            stage('Block 1 - Stage 2') {
                steps {
                    echo "Block 1 - Stage 2"
                    build(job: 'jenkins_job_2')
                }
            }
        }
    }
    stage('Parallel Test 2') {
        parallel {
            stage('Block 2 - Stage 1') {
                steps {
                    echo "Block 2 - Stage 1"
                    build(job: "jenkins_job_3")
                }
            }
            stage('Block 2 - Stage 2') {
                steps {
                    echo "Block 2 - Stage 2"
                    build(job: "jenkins_job_4")
                }
            }
        }
    }
}

}


回答1:


You don't have to put each call to a parallel-job inside a stage, so you can do it as such:

pipeline {
    agent any
    stages {
        stage('single run') {
            parallel {
                stage('Parallel Test 1') {
                    steps {
                        script {
                            def group1 = [:]
                            group1["test_1"] = {
                                echo "test_1"
                                sh(script: "date -u")
                                build(job: 'jenkins_job_1')
                            }
                            group1["test_2"] = {
                                echo "test_2"
                                sh(script: "date -u")
                                build(job: 'jenkins_job_2')
                            }
                            parallel group1
                        }
                    }
                }
                stage('Parallel Test 2') {
                    steps {
                        script {
                            def group2 = [:]
                            group2["test_3"] = {
                                echo "test_3"
                                sh(script: "date -u")
                                build(job: 'jenkins_job_3')
                            }
                            group2["test_4"] = {
                                echo "test_4"
                                sh(script: "date -u")
                                build(job: 'jenkins_job_4')
                            }
                            parallel group2
                        }
                    }
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/53458217/how-to-run-multi-stages-at-the-same-time-using-multi-parallel-blocks

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