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

声明式Pipeline并行阶段优先级设置:如何确保Stage C优先启动?

How to Prioritize Stage C in Jenkins Declarative Pipeline Parallel Blocks

Great question! Let's break this down. The core goal is to make sure Stage C locks in a node first before Stages A and B can claim available resources—this way, we keep the total runtime capped at 4 hours (since A and B finish within C's 4-hour window) instead of letting A/B start first and pushing total time to 6 hours. Here are two solid approaches:

Approach 1: Use Stage Priority (Requires Pipeline: Job Plugin)

If you have the Pipeline: Job Plugin (version 2.28 or newer) installed, you can directly set priority levels for individual stages in your parallel block. This tells Jenkins to prioritize scheduling Stage C over A and B whenever a node becomes available.

pipeline {
    agent none
    stages {
        stage('Parallel Execution') {
            parallel {
                stage('Stage C') {
                    // Set highest priority for C
                    options {
                        priority(10)
                    }
                    agent { node { label 'your-worker-nodes' } }
                    steps {
                        echo "Starting Stage C (4-hour task) first!"
                        sh "sleep 14400" // Simulate 4-hour workload
                    }
                }
                stage('Stage A') {
                    // Lower priority for A
                    options {
                        priority(1)
                    }
                    agent { node { label 'your-worker-nodes' } }
                    steps {
                        echo "Starting Stage A (2-hour task)"
                        sh "sleep 7200" // Simulate 2-hour workload
                    }
                }
                stage('Stage B') {
                    // Lower priority for B
                    options {
                        priority(1)
                    }
                    agent { node { label 'your-worker-nodes' } }
                    steps {
                        echo "Starting Stage B (2-hour task)"
                        sh "sleep 7200" // Simulate 2-hour workload
                    }
                }
            }
        }
    }
}

Why this works:

Jenkins will prioritize scheduling the high-priority Stage C first. Once C takes one node, the second node will go to either A or B. When that first short-running stage finishes, the freed node will take the remaining stage—all within the 4-hour window of Stage C.

Approach 2: Scripted Node Preemption (No Plugins Needed)

If you can't use plugins, you can embed a scripted pipeline snippet inside your declarative pipeline to explicitly start Stage C first, ensuring it grabs a node before A and B are scheduled.

pipeline {
    agent none
    stages {
        stage('Parallel Execution with C Priority') {
            steps {
                script {
                    // Define Stage C first to grab a node immediately
                    def runStageC = {
                        node('your-worker-nodes') {
                            echo "Preempting node for Stage C (4-hour task)!"
                            sh "sleep 14400" // Simulate 4-hour workload
                        }
                    }

                    // Define Stages A and B
                    def runStageA = {
                        node('your-worker-nodes') {
                            echo "Starting Stage A (2-hour task)"
                            sh "sleep 7200" // Simulate 2-hour workload
                        }
                    }

                    def runStageB = {
                        node('your-worker-nodes') {
                            echo "Starting Stage B (2-hour task)"
                            sh "sleep 7200" // Simulate 2-hour workload
                        }
                    }

                    // Launch Stage C first, then parallelize A and B
                    def cFuture = runStageC.start()
                    parallel(runStageA, runStageB)
                    // Wait for Stage C to finish before marking the pipeline complete
                    cFuture.join()
                }
            }
        }
    }
}

Why this works:

By explicitly starting Stage C first with start(), we force it to claim a node right away. We then launch A and B in parallel—they'll take the second available node, and the remaining one will run once the first short stage finishes. Since all three stages run concurrently (C runs the full 4 hours, A/B finish in 2), total runtime stays at 4 hours.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:47:05