如何等待Jenkinsfile“并行”块内的所有执行程序?

问题描述 投票:1回答:1

我是Jenkins的新手并配置它的脚本,所以如果我说任何愚蠢的话,请原谅我。

我有一个脚本化的Jenkins管道,它将代码库的构建重新分配到多个节点,使用包含node块的parallel块实现。现在,问题在于,在构建之后,我想对正在构建的一个节点上构建的文件执行某个操作 - 但仅在所有节点完成之后。基本上,我想要的是类似于barrier,但是在Jenkins的节点之间。

简化,我的Jenkinsfile看起来像这样:

def buildConf = ["debug", "release"]

parallel buildConf.collectEntries { conf ->
    [ conf, {
        node {
            sh "./checkout_and_build.sh"

            // and here I need a barrier
            if (conf == "debug") {
                // I cannot do this outside this node block,
                // because execution may be redirected to a node
                // that doesn't have my files checked out and built
                sh "./post_build.sh"
            }
        }
    }]
}

有什么办法可以实现吗?

jenkins groovy parallel-processing jenkins-pipeline
1个回答
1
投票

您可以做的是添加一个全局计数器来计算已完成任务的数量,您需要指示每个具有后期作业的任务等到计数器等于任务总数,然后您可以执行后期任务部分。像这样:

def buildConf = ["debug", "release"]
def doneCounter = 0

parallel buildConf.collectEntries { conf ->
    [ conf, {
        node {
            sh "./checkout_and_build.sh"

            doneCounter++
            // and here I need a barrier
            if (conf == "debug") {
                waitUntil { doneCounter == buildConf.size() }
                // I cannot do this outside this node block,
                // because execution may be redirected to a node
                // that doesn't have my files checked out and built
                sh "./post_build.sh"
            }
        }
    }]
}

请注意,具有后期任务部分的每个任务都将阻止执行程序,直到完成所有其他并行任务并且可以执行后期部分。如果您有大量执行程序或任务很短,那么这可能不是问题。但是如果你有很少的执行者,它可能会导致拥堵。如果执行者的数量少于或等于需要后期工作的并行任务总数,则可能会遇到死锁!

© www.soinside.com 2019 - 2024. All rights reserved.