ubuntu

Jenkins如何在Ubuntu上实现多任务并行

小樊
37
2025-12-08 13:01:46
栏目: 智能运维

在 Ubuntu 上,Jenkins 可通过多 Executors、Pipeline 并行阶段、多节点分布式构建以及并发构建 Job 四种方式实现多任务并行。下面给出可直接落地的配置方法与示例。


一、并行方式总览与选择

方式 作用范围 适用场景 关键配置点
单机多 Executors 同一 Ubuntu 节点 轻量任务、快速构建、单机资源利用 设置 Manage Jenkins → Configure System → # of executors
Pipeline parallel 阶段 单个流水线内部 多模块/多环境同时构建与测试 使用 parallel { stage … }
多节点分布式构建 多个 Agent/节点 大规模构建、隔离环境、横向扩展 Manage Nodes and Clouds 添加节点,使用 标签 调度
并发构建多个 Job 多个 Job 实例 不同项目/分支同时跑 允许并发构建(默认支持),必要时用 Throttle Concurrent Builds 插件限流

二、单机多 Executors 并行


三、Pipeline 并行阶段

pipeline {
  agent any
  stages {
    stage('并行构建与测试') {
      parallel {
        stage('构建前端') {
          steps { echo 'Building frontend...' }
        }
        stage('构建后端') {
          steps { echo 'Building backend...' }
        }
        stage('并行测试集A') {
          steps {
            script {
              timeout(time: 5, unit: 'MINUTES') {
                echo 'Running tests A...'
                // sh 'run-tests-a.sh'
              }
            }
          }
        }
        stage('并行测试集B') {
          steps {
            script {
              timeout(time: 5, unit: 'MINUTES') {
                echo 'Running tests B...'
                // sh 'run-tests-b.sh'
              }
            }
          }
        }
      }
    }
  }
  post {
    success { echo '✅ 全部完成' }
    failure { echo '❌ 存在失败或超时' }
  }
}

四、多节点分布式构建


五、并发构建多个 Job 与扩展实践

jobs {
  pipelineJob('Parallel-Job-1') {
    definition {
      cpsScm {
        scm {
          git {
            remote { url('https://github.com/your-org/your-repo.git') }
            credentialsId('your-credentials-id')
          }
        }
        scriptPath('Jenkinsfile')
      }
    }
  }
  pipelineJob('Parallel-Job-2') {
    definition {
      cpsScm {
        scm {
          git {
            remote { url('https://github.com/your-org/your-repo.git') }
            credentialsId('your-credentials-id')
          }
        }
        scriptPath('Jenkinsfile')
      }
    }
  }
}

0
看了该问题的人还看了