You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Jenkins声明式流水线语法并行运行多个Job

你的需求完全可以实现,你当前的写法不符合Jenkins声明式流水线的语法规范:声明式流水线中parallel不能直接包裹顶层stages块,需要放在某一个具体的stage内部使用。

如果你的预期是Test1、Test2两个阶段并行执行,两个都执行完成后再执行Test3生成报告,正确的写法如下:

pipeline {
    agent any
    stages {
        // 定义父阶段,内部包裹需要并行执行的子阶段
        stage('并行执行测试准备与用例') {
            parallel {
                stage('Test1') {
                    steps {
                        sh 'pip install -r requirements.txt'
                    }
                }
                stage('Test2') {
                    steps {
                        echo 'Stage 2'
                        sh 'behave -f allure_behave.formatter:AllureFormatter -o allure-results features/scenarios/**/*.feature'
                    }
                }
            }
        }
        // 并行阶段全部执行完成后,再执行生成报告的阶段
        stage('Test3') {
            steps {
                script {
                    allure([
                        includeProperties: false,
                        jdk: '',
                        properties: [],
                        reportBuildPolicy: 'ALWAYS',
                        results: [[path: 'allure-results']]
                    ])
                }
            }
        }
    }
}

如果你需要在单个流水线内并行执行多个独立的Job,还可以在并行块的步骤中使用build方法触发其他Job,示例写法如下:

stage('并行触发多个独立Job') {
    parallel {
        stage('触发JobA') {
            steps {
                build job: '替换为你的JobA名称', wait: true
            }
        }
        stage('触发JobB') {
            steps {
                build job: '替换为你的JobB名称', wait: true
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Akshay Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 10:57:07