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

如何在Jenkins中使用不同代理动态运行并行阶段?

问题描述

我希望在Jenkins中动态运行并行阶段,每个并行阶段分别在不同的代理(jenkins-agent-01、jenkins-agent-02、jenkins-agent-03、jenkins-agent-04)上执行。请问如何触发使用不同代理的动态阶段?
以下是包含需动态执行和顺序执行阶段的Jenkinsfile,除代理名称和函数名称外,Jenkins阶段内的大部分代码均相同,我希望遵循DRY(Don't Repeat Yourself)原则。

@Library('jenkins-shared-lib')_

pipeline {
  agent none


  stages {
    stage('initialize') {


      agent {
        label 'jenkins-agent-01'
      }
        steps {
            script {
              //code for loading env and scripts
            }
        }
    }

    stage('UnitTests') {

        parallel {
            stage('UnitTest-Stage1') {
              agent {
                label 'jenkins-agent-02'
              }

              steps {
                script {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                try {
                unitTest.function1()
                }
                catch (Exception e) {
                caughtException = e
                currentBuild.result = 'FAILURE'
                }
               }
              }
            }
            }
           }
           }

          stage('UnitTest-Stage2') {
            agent {
              label 'jenkins-agent-03'
            }
            steps {
              script {
                try {
                unitTest.function3()
                }
                catch (Exception e) {
                caughtException = e
                currentBuild.result = 'FAILURE'
                }
              }
            }        
     }

        stage('UnitTest-Stage3') {
            agent {
              label 'jenkins-agent-04'
            }
            steps {
              script {
                try {
                unitTest.function4()
                }
                catch (Exception e) {
                caughtException = e
                currentBuild.result = 'FAILURE'
                }
              }
            }       
        }
        }
    }

 stage('Analysis') {

      parallel {
        stage('Analysis-Stage1') {

          steps {
            script {
              try {
                sq.function1()
              }

          catch (Exception e) {
                caughtException = e
                currentBuild.result = 'FAILURE'
          }
            }
          }       
        }
      }
    }
  }
}
解决方案

要实现动态并行阶段并遵循DRY原则,可通过脚本化生成并行阶段映射+封装通用错误处理逻辑改造Jenkinsfile,具体实现如下:

1. 核心思路

  • 用配置列表统一管理所有并行阶段的代理、名称、执行函数,避免重复编写阶段结构
  • 提取重复的错误捕获逻辑为通用函数,减少代码冗余
  • 通过Groovy循环动态生成并行阶段映射,实现批量创建不同代理的并行任务

2. 改造后的完整Jenkinsfile

@Library('jenkins-shared-lib')_

// 封装通用错误处理函数,复用所有阶段的异常捕获逻辑
def runWithErrorHandling(Closure task) {
    script {
        catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
            try {
                task()
            } catch (Exception e) {
                caughtException = e
                currentBuild.result = 'FAILURE'
            }
        }
    }
}

pipeline {
    agent none

    stages {
        stage('initialize') {
            agent { label 'jenkins-agent-01' }
            steps {
                script {
                    // code for loading env and scripts
                }
            }
        }

        stage('UnitTests') {
            steps {
                script {
                    // 定义并行阶段配置:代理标签、阶段名称、执行函数
                    def unitTestConfigs = [
                        [label: 'jenkins-agent-02', name: 'UnitTest-Stage1', func: { unitTest.function1() }],
                        [label: 'jenkins-agent-03', name: 'UnitTest-Stage2', func: { unitTest.function3() }],
                        [label: 'jenkins-agent-04', name: 'UnitTest-Stage3', func: { unitTest.function4() }]
                    ]

                    // 动态生成并行阶段映射
                    def parallelTasks = [:]
                    unitTestConfigs.each { config ->
                        parallelTasks[config.name] = {
                            node(config.label) {
                                runWithErrorHandling(config.func)
                            }
                        }
                    }

                    // 执行所有并行阶段
                    parallel parallelTasks
                }
            }
        }

        stage('Analysis') {
            parallel {
                stage('Analysis-Stage1') {
                    steps {
                        runWithErrorHandling { sq.function1() }
                    }
                }
            }
        }
    }
}

关键说明

  • 动态阶段管理:新增或删除并行阶段时,只需修改unitTestConfigs列表,无需重复编写完整的阶段代码
  • 代理动态绑定:通过node(config.label)在脚本化部分指定每个阶段的代理,实现不同代理的动态分配
  • 代码复用:runWithErrorHandling函数统一处理所有阶段的异常捕获和结果设置,彻底消除重复代码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 04:30:05