使用Jenkins管道在不同节点上并行运行相同的阶段

使用Jenkins管道在不同节点上并行运行相同的阶段,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,本文介绍了一种在不同节点上并行执行步骤/post操作的方法: 是否有可能在多个节点上执行相同的阶段而不复制代码 大概是这样的: stage('Run Tests') { parallel { stage("Test On ${NODE_NAME}") { agents { label "windows" label "linux"

本文介绍了一种在不同节点上并行执行步骤/post操作的方法:

是否有可能在多个节点上执行相同的阶段而不复制代码

大概是这样的:

     stage('Run Tests') {
        parallel {
            stage("Test On ${NODE_NAME}") {
                agents {
                    label "windows"
                    label "linux"
                }
                steps {
                    // do test steps
                }
                post {
                    always {
                        junit "**/TEST-*.xml"
                    }
                }
            }
        }
    }

您可以创建动态阶段,但不需要针对您的案例

pipeline {
    agent any
    stages {
        stage ("Test") {
            steps {
                script {
                    testStages = ["Windows", "Linux"].collectEntries {
                        ["${it}" : runTests(it)]
                    }
                    parallel testStages
                }   
            }
        }
    }
}

def runTests(def name){
    return {
        node(name) {
            stage("Run on ${name}") {
                script {
                    command = "run-tests"
                    try {
                        switch(name.toLowerCase()) {
                            case "windows": 
                                command += ".bat" 
                                break;

                            case "linux": 
                                command += ".sh" 
                                break;
                        } 

                        echo command
                    } catch (Exception ex) {
                        echo ex
                    } finally {
                        echo "post ${name}"
                    }
                }
            }
        }
    }
}

您可以创建动态阶段,但不需要针对您的案例

pipeline {
    agent any
    stages {
        stage ("Test") {
            steps {
                script {
                    testStages = ["Windows", "Linux"].collectEntries {
                        ["${it}" : runTests(it)]
                    }
                    parallel testStages
                }   
            }
        }
    }
}

def runTests(def name){
    return {
        node(name) {
            stage("Run on ${name}") {
                script {
                    command = "run-tests"
                    try {
                        switch(name.toLowerCase()) {
                            case "windows": 
                                command += ".bat" 
                                break;

                            case "linux": 
                                command += ".sh" 
                                break;
                        } 

                        echo command
                    } catch (Exception ex) {
                        echo ex
                    } finally {
                        echo "post ${name}"
                    }
                }
            }
        }
    }
}
最适合我的是:

pipeline {
    agent none
    stages {
        stage('BuildAndTest') {
            matrix {
                agent {
                    label "${PLATFORM}-agent"
                }
                axes {
                    axis {
                        name 'PLATFORM'
                        values 'linux', 'windows'
                    }
                }
                stages {
                    stage('Test') {
                        steps {
                            echo "Do Test for ${PLATFORM}"
                        }
                    }
                }
                post {
                    always {
                        junit "**/TEST-*.xml"
                    }
                }
            }
        }
    }
}
此管道将在两个平台上执行定义的阶段,包括构建后操作,而不存在任何代码重复

引用Jenkins关于申报矩阵的一段话:

在没有矩阵的情况下创建的等效管道很容易是多个管道 时间更长,更难理解和维护

最适合我的是:

pipeline {
    agent none
    stages {
        stage('BuildAndTest') {
            matrix {
                agent {
                    label "${PLATFORM}-agent"
                }
                axes {
                    axis {
                        name 'PLATFORM'
                        values 'linux', 'windows'
                    }
                }
                stages {
                    stage('Test') {
                        steps {
                            echo "Do Test for ${PLATFORM}"
                        }
                    }
                }
                post {
                    always {
                        junit "**/TEST-*.xml"
                    }
                }
            }
        }
    }
}
此管道将在两个平台上执行定义的阶段,包括构建后操作,而不存在任何代码重复

引用Jenkins关于申报矩阵的一段话:

在没有矩阵的情况下创建的等效管道很容易是多个管道 时间更长,更难理解和维护