首页 文章

如何跨分支限制Jenkins并发multibranch管道构建?

提问于
浏览
2

有没有人知道如何使用跨分支的多分支作业中设置的声明性管道来限制并发构建?

无论何时我们为舞台设置代理,都会分配新的执行者 . 这导致死锁,例如当你有与执行程序一样多的分支同时触发构建时 . 不设置代理导致阶段随机选择执行程序,这是不可接受的,因为某些阶段需要在某些代理上运行...

经典方法不起作用:

  • Throttle Concurrent Builds Plugin不适用于multibranch

  • 设置 properties([disableConcurrentBuilds()]) 只是限制每个分支的并发

  • lock 步骤需要管道根目录中的 agent none 以防止分配执行程序,但是这会阻碍我们的全局 post 块执行suff,因为它需要一个代理程序,显然无法为post块设置代理程序

1 回答

  • 0

    这个问题有点陈旧,我不确定当时是否存在解决方案,但是如果你在每个阶段设置一个代理,那么除非你依赖于另一个使用代理的构建,否则它不应该死锁 . 执行以下部分也将在与相应阶段中指定的代理相同的节点上执行post块 . 我还在最后添加了另一个管道帖子部分,如果你必须为整个管道运行post块并且需要在前一个阶段使用的同一个代理上运行它 . 希望这可以帮助!

    def agentNameStage2 = null
    pipeline {
        agent none
        stages {
            stage("Stage 1") {
                agent { label "somelabel" }
                steps {
                    println getContext(hudson.model.Node)
                }
            }
            stage("Stage 2") {
                agent { label "anotherlabel" }
                steps {
                    script {
                        def agentStage2 = getContext(hudson.model.Node)
                        println agentStage2
    
                        // Either use this hack or add whitelist hudson.model.Node getNodeName and use agentStage2.nodeName
                        //agentNameStage2 = agentStage2.nodeName // <-- Best method that needs whitelisting or to run as trusted
                        agentNameStage2 = agentStage2.toString().replaceAll('.*?\\[', '').replaceAll('\\]$', '')
                    }
                }
                post {
                    always {
                        // Running on an agent with "anotherlabel"
                       println getContext(hudson.model.Node)
                    }
                }
            }
        }
    
        post {
            always {
                script {
                    if(agentNameStage2) {
                        // Note this doesn't guarantee the same workspace as was used in the above Stage 2 section
                        node (agentNameStage2) {
                            println "Pipeline post is running on: " + getContext(hudson.model.Node)
                        }
                    } else {
                        // Handle else case
                    }
                }
            }
        }
    }
    

相关问题