首页 > 解决方案 > Jenkins Pipelines:如何并行运行一个阶段?

问题描述

我想将一个阶段与两个阶段并行运行,所有这一切都在其他三个阶段之后进行。

像这样的东西(不是有效的语法):

pipeline {
    stages {
        stage('Build A') {
        }
        stage('Build B') {
        }
        stage('Build C') {
        }
        parallel {
            stages {
                stage('Build D1') {
                }
                stage('Build D2') {
                }
            }
            stage('Build D3') {
            }
        } 
    }
}

有可能安排这种结构吗?

标签: jenkinsjenkins-pipeline

解决方案


做到这一点的方法是并行的顺序阶段

pipeline {
    agent none

    stages {
        stage("build and deploy on Windows and Linux") {
            parallel {
                stage("windows") {
                    agent {
                        label "windows"
                    }
                    stages {
                        stage("build") {
                            steps {
                                bat "run-build.bat"
                            }
                        }
                        stage("deploy") {
                            when {
                                branch "master"
                            }
                            steps {
                                bat "run-deploy.bat"
                            }
                        }
                    }
                }

                stage("linux") {
                    agent {
                        label "linux"
                    }
                    stages {
                        stage("build") {
                            steps {
                                sh "./run-build.sh"
                            }
                        }
                        stage("deploy") {
                             when {
                                 branch "master"
                             }
                             steps {
                                sh "./run-deploy.sh"
                            }
                        }
                    }
                }
            }
        }
    }
}

推荐阅读