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

时间:2018-11-24 12:37:10

标签: jenkins jenkins-plugins jenkins-pipeline devops

enter image description here

在这里,我需要同时执行Parallel test 1Parallel 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")
                }
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分: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
                        }
                    }
                }
            }
        }
    }
}