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

Jenkins并行Pipeline阶段执行速度慢于串行问题咨询

问题

我正在开发一个采用并行化的Jenkins Pipeline,却遇到并行阶段相互拖慢、无法实现真正并行的问题。使用的代码如下:

pipeline {
  agent any
  stages {
    stage('Dependencies') {
      when {
        anyOf {
          branch 'develop';
          branch 'demo';
          branch 'test';
        }
      }
      parallel{
        stage('Cloud Libraries') {
          steps {
            sh "umask 0000;" 
            sh "pip3.8 install --no-cache-dir -r requirements/cloud.txt -t ${WORKSPACE}/layers/cloud_libraries/python/"
          }
        }
        stage('Cellect Data Processing') {
          steps {
            sh "umask 0000;" 
            sh "pip3.8 install --no-cache-dir -r requirements/cellect/data_processing.txt -t ${WORKSPACE}/layers/cellect_data_processing/python/"
          }
        }
        stage('Cellect Warranty Processing') {
          steps {
            sh "umask 0000;" 
            sh "pip3.8 install --no-cache-dir -r requirements/cellect/warranty_processing.txt -t ${WORKSPACE}/layers/cellect_warranty_processing/python/"
          }
        }
        stage('Cellect Data Consumer') {
          steps {
            sh "umask 0000;" 
            sh "pip3.8 install --no-cache-dir -r requirements/cellect/data_consumer.txt -t ${WORKSPACE}/layers/cellect_data_consumer/python/"
          }
        }
        stage('Cellect BESS Control') {
          steps {
            sh "umask 0000;" 
            sh "pip3.8 install --no-cache-dir -r requirements/cellect/bess_control.txt -t ${WORKSPACE}/layers/cellect_bess_control/python/"
          }
        }

      }
    }
  }
}

此前串行执行时,各阶段耗时分别为15s、20s、20s、7s、5s;并行执行后,各阶段耗时变为17s、24s、49s、50s、44s,总耗时反而超过串行执行。想咨询是否存在认知误区或遗漏的配置要点?


分析与解决方案

核心问题:单Agent资源竞争

你当前的所有并行阶段都绑定在同一个Agent(agent any)上,本质是在单台机器上启动多个pip进程并行工作,必然引发资源争抢:

  • CPU被多个编译Python扩展包的进程瓜分,导致每个任务的编译速度大幅下降
  • 磁盘IO被大量依赖包的下载、写入操作占满,读写效率骤降
  • 单节点的网络带宽被多个并行下载请求分流,每个依赖的下载耗时变长

具体解决办法

  1. 给每个并行阶段分配独立Agent
    把agent any移到每个并行的stage内部,让每个pip任务跑在不同的Jenkins节点上,彻底避免单节点资源竞争。修改示例:

    pipeline {
      stages {
        stage('Dependencies') {
          when {
            anyOf {
              branch 'develop';
              branch 'demo';
              branch 'test';
            }
          }
          parallel{
            stage('Cloud Libraries') {
              agent any // 每个stage单独指定agent
              steps {
                sh "umask 0000;" 
                sh "pip3.8 install --no-cache-dir -r requirements/cloud.txt -t ${WORKSPACE}/layers/cloud_libraries/python/"
              }
            }
            // 其余4个stage同理添加agent any配置
          }
        }
      }
    }
    

    前提是你的Jenkins集群有足够的可用Agent节点。

  2. 限制单Agent上的并行任务数量
    如果无法新增Agent,就减少同时运行的pip任务数量,降低资源竞争强度。可以通过parallel的concurrency参数控制并发数(需Jenkins版本支持):

    parallel(
      "Cloud Libraries": {
        steps {
          sh "umask 0000;" 
          sh "pip3.8 install --no-cache-dir -r requirements/cloud.txt -t ${WORKSPACE}/layers/cloud_libraries/python/"
        }
      },
      "Cellect Data Processing": { /* 对应步骤 */ },
      "Cellect Warranty Processing": { /* 对应步骤 */ },
      "Cellect Data Consumer": { /* 对应步骤 */ },
      "Cellect BESS Control": { /* 对应步骤 */ },
      failFast: true,
      concurrency: 2 // 限制同时只跑2个任务
    )
    
  3. 优化pip安装逻辑

    • 去掉--no-cache-dir,开启缓存避免重复下载相同依赖,减少网络和磁盘压力
    • 切换到国内PyPI镜像源(如阿里云、清华镜像),加快依赖下载速度
    • 提取多阶段共享的公共依赖到单独层,避免重复安装

认知误区纠正

Jenkins的parallel块只是实现了逻辑上的并行,物理层面的真正并行必须依赖独立的Agent资源。如果所有并行任务都挤在同一个Agent上,反而会因为资源内耗降低效率,尤其像pip安装这种IO+CPU密集型任务。

内容的提问来源于stack exchange,提问作者demetere._

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 22:24:57