首页 > 解决方案 > Jenkins 中的分布式构建

问题描述

我在 Jenkins 有一项测试执行工作。该作业包含许多测试用例,因此为了减少构建时间,我希望它以分布式方式执行。假设作业有 100 个测试用例,如果我触发作业构建,那么从属 1 应该执行 50 个测试用例,从属 2 应该执行剩余的 50 个测试用例。如何实现这个场景?

提前致谢。

标签: jenkins

解决方案


使用Jenkins Pipeline,您可以借助部分轻松地在任意数量的代理之间分配工作任务parallelJenkinsfile执行您想要的操作的示例可能如下所示:

pipeline {
    agent none 
    stages {
        stage('Tests') {
            parallel {
                stage('Tests1') {
                    agent { label 'slave1' }
                    steps {
                        echo "tests part 1"
                    }
                }
                stage('Tests2') {
                    agent { label 'slave2' }
                    steps {
                        echo "tests part 2"
                    }
                }
            }
        }
    }
}

对于更复杂的场景,您可以添加matrix与标记代理相结合的部分。

假设您标记了一些 Jenkins slave 'test-runner',并且您将测试分成 10 个部分。一次最多可以运行 10 个任务(matrix受代理数量限制):

pipeline {
    agent none
    stages {
        stage('Distribute Tests') {
            matrix {
                axes {
                    axis {
                        name 'PART'
                        values '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'
                    }
                }
                stages {
                    stage('Tests') {
                        agent { label 'test-runner' }
                        steps {
                            echo "tests part ${PART}"
                        }
                    }
                }
            }
        }
    }
}

推荐阅读