Jenkins脚本如何并行执行多个任务

wen 实用脚本 2

本文目录导读:

Jenkins脚本如何并行执行多个任务

  1. 并行执行多个Stage(声明式Pipeline)
  2. 使用failFast选项控制失败行为
  3. 动态并行任务(脚本式Pipeline)
  4. 并行执行不同环境/配置
  5. 矩阵并行(Matrix)
  6. 带超时的并行任务
  7. 使用脚本式Pipeline并行多个节点
  8. 并行构建多模块项目
  9. 限制并行任务数量
  10. 带条件的并行执行
  11. 注意事项
  12. 最佳实践

在Jenkins中并行执行多个任务有多种方式,以下是常用的几种方法:

并行执行多个Stage(声明式Pipeline)

pipeline {
    agent any
    stages {
        stage('并行任务') {
            parallel {
                stage('任务1') {
                    steps {
                        echo '执行任务1'
                        sleep 5
                    }
                }
                stage('任务2') {
                    steps {
                        echo '执行任务2'
                        sleep 3
                    }
                }
                stage('任务3') {
                    steps {
                        echo '执行任务3'
                        sleep 4
                    }
                }
            }
        }
        stage('后续处理') {
            steps {
                echo '所有并行任务完成'
            }
        }
    }
}

使用failFast选项控制失败行为

pipeline {
    agent any
    stages {
        stage('并行任务') {
            parallel {
                // 当任何一个并行分支失败时,立即停止其他分支
                failFast true
                stage('任务1') {
                    steps {
                        echo '任务1'
                    }
                }
                stage('任务2') {
                    steps {
                        echo '任务2'
                    }
                }
            }
        }
    }
}

动态并行任务(脚本式Pipeline)

def tasks = ['编译', '测试', '部署', '打包']
parallel tasks.collectEntries { taskName ->
    ["任务-${taskName}" : {
        node {
            echo "开始执行 ${taskName}"
            // 执行具体任务
            sleep 2
            echo "完成 ${taskName}"
        }
    }]
}

并行执行不同环境/配置

pipeline {
    agent any
    stages {
        stage('多环境测试') {
            parallel {
                stage('Linux测试') {
                    agent { label 'linux' }
                    steps {
                        sh 'echo "在Linux上运行测试"'
                    }
                }
                stage('Windows测试') {
                    agent { label 'windows' }
                    steps {
                        bat 'echo "在Windows上运行测试"'
                    }
                }
                stage('Mac测试') {
                    agent { label 'mac' }
                    steps {
                        sh 'echo "在Mac上运行测试"'
                    }
                }
            }
        }
    }
}

矩阵并行(Matrix)

pipeline {
    agent any
    stages {
        stage('矩阵测试') {
            parallel {
                stage('组合测试') {
                    matrix {
                        axes {
                            axis {
                                name 'OS'
                                values 'linux', 'windows'
                            }
                            axis {
                                name 'BROWSER'
                                values 'chrome', 'firefox'
                            }
                        }
                        stages {
                            stage('测试') {
                                steps {
                                    echo "测试 ${OS} 上的 ${BROWSER}"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

带超时的并行任务

pipeline {
    agent any
    stages {
        stage('超时并行') {
            parallel {
                stage('快速任务') {
                    steps {
                        echo '快速任务'
                    }
                }
                stage('耗时任务') {
                    options {
                        timeout(time: 10, unit: 'SECONDS')
                    }
                    steps {
                        echo '耗时任务开始'
                        sleep 15
                        echo '超时会提前结束'
                    }
                }
            }
        }
    }
}

使用脚本式Pipeline并行多个节点

node {
    // 定义需要并行执行的任务
    def branches = [:]
    branches['并行任务1'] = {
        node('worker1') {
            echo '在worker1上执行任务1'
            // 执行具体操作
        }
    }
    branches['并行任务2'] = {
        node('worker2') {
            echo '在worker2上执行任务2'
            // 执行具体操作
        }
    }
    branches['并行任务3'] = {
        node('worker3') {
            echo '在worker3上执行任务3'
            // 执行具体操作
        }
    }
    parallel branches
}

并行构建多模块项目

pipeline {
    agent any
    stages {
        stage('构建') {
            parallel {
                stage('模块A') {
                    steps {
                        sh 'cd moduleA && mvn clean install'
                    }
                }
                stage('模块B') {
                    steps {
                        sh 'cd moduleB && mvn clean install'
                    }
                }
                stage('模块C') {
                    steps {
                        sh 'cd moduleC && mvn clean install'
                    }
                }
            }
        }
    }
}

限制并行任务数量

def maxParallel = 2
// 使用类来限制并行数量
class ParallelService {
    static int counter = 0
}
pipeline {
    agent any
    stages {
        stage('限量并行') {
            steps {
                script {
                    def tasks = ['任务1', '任务2', '任务3', '任务4', '任务5']
                    tasks.collate(maxParallel).each { batch ->
                        parallel batch.collectEntries { task ->
                            ["${task}" : {
                                echo "执行 ${task}"
                                sleep 3
                            }]
                        }
                    }
                }
            }
        }
    }
}

带条件的并行执行

pipeline {
    agent any
    stages {
        stage('条件并行') {
            parallel {
                stage('必须执行') {
                    steps {
                        echo '这个任务总是执行'
                    }
                }
                stage('条件执行') {
                    when {
                        expression {
                            // 根据条件判断是否执行
                            return params.RUN_EXTRA_TASKS == true
                        }
                    }
                    steps {
                        echo '只在特定条件下执行'
                    }
                }
            }
        }
    }
}

注意事项

  1. 资源限制:并行任务会占用更多系统资源,注意控制并发数量
  2. 依赖关系:确保并行任务之间没有依赖关系
  3. 错误处理:使用 failFastcatchError 处理失败情况
  4. 日志输出:并行任务的日志会交错显示,注意区分
  5. 节点资源:在不同节点上并行时,确保有足够的执行器

最佳实践

  • 合理规划并行粒度,避免过多小任务
  • 使用 stage(name) 明确命名并行分支
  • 考虑使用 failFast 策略节省资源
  • 对于动态数量的并行任务,使用脚本式Pipeline
  • 添加适当的超时和重试机制

选择合适的并行方式取决于你的具体需求和Jenkins环境配置。

抱歉,评论功能暂时关闭!