You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在多Jenkins Agent上执行声明式Pipeline阶段并避免代码重复?

最佳实现方案:共享检出逻辑 + 并行阶段

你之前的两个尝试都没命中核心需求:

  • Matrix块确实会让仪表板层级变深,显得混乱;
  • agent(label 'builtin && jenkins-uk-win7')是要求单个Agent同时具备两个标签,这和你需要的「两个不同Agent分别执行」完全不符,所以报错找不到Agent是必然的。

下面是两种简洁且无重复代码的实现方式:

方案一:封装共享检出函数 + 显式并行阶段

把所有检出逻辑封装成一个Groovy函数,然后在Linux和Windows的并行阶段里复用这个函数,逻辑清晰,仪表板显示也直观。

完整Pipeline代码

// 封装所有检出逻辑,避免重复代码
def checkoutProject() {
    // 这里替换成你的实际检出步骤,比如多个git checkout:
    git url: 'https://your-project-repo.git', branch: 'main'
    git url: 'https://dep-repo-1.git', branch: 'stable', dir: 'deps/dep1'
    git url: 'https://dep-repo-2.git', branch: 'v2.0', dir: 'deps/dep2'
    // 其他依赖的检出操作都放在这里
}

pipeline {
    agent none // 全局不指定Agent,由各阶段自行指定
    stages {
        stage('Cross-Platform Tests') {
            parallel {
                // Linux平台任务
                stage('Linux') {
                    agent { label 'builtin' }
                    stages {
                        stage('Checkout Code') {
                            steps {
                                checkoutProject()
                            }
                        }
                        stage('Run Linux Tests') {
                            steps {
                                sh './scripts/run-linux-tests.sh'
                            }
                        }
                    }
                }

                // Windows平台任务
                stage('Windows') {
                    agent { label 'jenkins-uk-win7' }
                    stages {
                        stage('Checkout Code') {
                            steps {
                                checkoutProject()
                            }
                        }
                        stage('Run Windows Tests') {
                            steps {
                                bat 'scripts/run-windows-tests.bat'
                            }
                        }
                    }
                }
            }
        }
    }
}

优点

  • 检出代码完全复用,无重复逻辑;
  • 并行阶段在Jenkins仪表板上会分开显示Linux和Windows的任务流,层级清晰;
  • 逻辑直观,容易维护和修改。

方案二:动态遍历Agent配置(适合未来扩展多Agent)

如果以后需要新增更多平台的Agent,这种方式只需要修改配置列表,不用改动任务逻辑,扩展性更强。

完整Pipeline代码

// 定义所有Agent的配置:标签、测试命令
def agentConfigs = [
    [label: 'builtin', testCommand: 'sh ./scripts/run-linux-tests.sh'],
    [label: 'jenkins-uk-win7', testCommand: 'bat scripts/run-windows-tests.bat']
    // 新增Agent只需在这里加一行配置
]

// 共享检出函数
def checkoutProject() {
    // 同方案一的检出逻辑
    git url: 'https://your-project-repo.git', branch: 'main'
    git url: 'https://dep-repo-1.git', branch: 'stable', dir: 'deps/dep1'
    git url: 'https://dep-repo-2.git', branch: 'v2.0', dir: 'deps/dep2'
}

pipeline {
    agent none
    stages {
        stage('Run All Platform Tests') {
            steps {
                script {
                    // 动态生成并行任务
                    parallel agentConfigs.collectEntries { config ->
                        ["${config.label} Tests": {
                            node(config.label) {
                                stage('Checkout') {
                                    checkoutProject()
                                }
                                stage('Execute Tests') {
                                    // 执行对应平台的测试命令
                                    eval(config.testCommand)
                                }
                            }
                        }]
                    }
                }
            }
        }
    }
}

优点

  • 新增平台Agent无需修改任务结构,仅需添加配置项;
  • 同样实现了检出逻辑的完全复用;
  • 代码更紧凑,适合多平台场景。

内容的提问来源于stack exchange,提问作者cberk1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 14:05:08