Jenkins Pipeline 并行执行多个任务

场景:前端业务使用了微服务架构,需要在一个主项目中编译打包所有子项目。

1、创建一个流水线任务

2、填写项目描述

3、编写 pipeline 脚本

pipeline {

    agent any

    stages {
        stage("build console all") {
            steps {
                script {
                    def starting = 'prod-'
                    def ending = ""
                    def job_list = [
                        "woc-common",
                        "woc-ecs",
                        "woc-eip",
                        "woc-home",
                        "woc-person-center",
                        "woc-user-iam",
                        "woc-vpc",
                        "woc-console",
                        "woc-oss",
                        "woc-slb"
                    ]

                    def branches = [:]
                    MAX_CONCURRENT = 4

                    //创建fifo
                    latch = new java.util.concurrent.LinkedBlockingDeque(MAX_CONCURRENT)

                    //往fifo中,填入最大个数元素
                    for(int i=0; i<MAX_CONCURRENT; i++)
                        latch.offer("$i")

                    for(int i=0; i<job_list.size(); i++) {
                        def name = starting + job_list[i] + ending
                        branches[name] = {
                            def thing = null
                            waitUntil {
                                //获取一个资源
                                thing = latch.pollFirst();
                                return thing != null;
                            }

                            try {
                                //执行job
                                build(job: name, propagate: false)
                            }

                            finally {
                                //释放一个资源
                                latch.offer(thing)
                            }
                        }
                    }

                    timestamps {
                        parallel branches
                    }
                }
            }
        }
    }
}

参考:
http://www.lujun.org.cn/?p=4025
https://www.cnblogs.com/sparkdev/archive/2018/10/04/9742739.html
https://www.cnblogs.com/sparkdev/p/7617765.html
https://www.w3cschool.cn/jenkins/jenkins-c7qs28n5.html
https://www.cnblogs.com/zangxueyuan/p/9207739.html

posted @ 2020-06-20 02:21  KeithTt  阅读(6530)  评论(1编辑  收藏  举报