如何在Jenkins声明式管道中使用节流并发构建

时间:2020-03-24 22:33:56

标签: jenkins jenkins-pipeline jenkins-plugins jenkins-declarative-pipeline

我有一些用于触发数据库刷新的Jenkins声明性管道,以及依赖于数据库的单元测试。这些Jenkins作业是从GitHub中的请求请求触发的。

为避免资源冲突,我需要防止这些作业在每个项目内以及跨项目同时运行。

“节流并发构建”插件似乎是为此目的而构建的。

我已经安装了插件并配置了如下类别:

TCB system configuration

我在应该限制其构建的一个存储库中的Jenkinsfile中添加了“ throttle”选项:

pipeline {

    agent any

    options {
        throttle(['ci_database_build'])
    }

    stages {
        stage('Build') {
            parallel {
                stage('Build source') {
                    steps {

                        // etc etc...

但是,这似乎并不能阻止2个作业同时执行。作为证据,这是同时执行的2个作业(均包含上述Jenkisfile更改):

Two throttled jobs running at the same time

我想念什么?

1 个答案:

答案 0 :(得分:6)

options块中的以下内容应该起作用

options {
    throttleJobProperty(
        categories: ['ci_database_build'],
        throttleEnabled: true,
        throttleOption: 'category',
    )
}

完整语法如下:https://github.com/jenkinsci/throttle-concurrent-builds-plugin/pull/68