一尘不染

如何使用多个并行块同时运行多个阶段?

jenkins

在这里,我需要同时执行Parallel test 1,并Parallel test 2在同一时间。

当我尝试在其上放置一个并行块时,由于在官方站点中这样提到,因此它引发了错误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")
                }
            }
        }
    }
}

}


阅读 299

收藏
2020-07-25

共1个答案

一尘不染

您不必将每个调用都放在阶段内的并行作业中,因此可以这样进行:

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
                        }
                    }
                }
            }
        }
    }
}
2020-07-25