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

配置多层级并行Jenkins任务流:指定任务完成后触发Last Job

Got it, let's walk through exactly how to set up this Jenkins job flow to match your requirements. The most robust and maintainable way to handle this kind of complex parallel/sequential dependency is using Jenkins Declarative Pipeline—it lets you define the entire flow as code, which is easy to tweak and version-control. I'll cover both the pipeline code approach and a traditional plugin-based alternative, plus break down what the UI looks like since I can't share direct screenshots.

Prerequisites

  • Ensure the Pipeline Plugin is installed (it's included with most modern Jenkins instances, but verify via Manage Jenkins > Plugins > Installed Plugins if needed)
  • Optional: For visual flow tracking, install the Pipeline Stage View Plugin (adds a nice graphical breakdown of your pipeline stages)

This is the cleanest approach—you can either paste the code directly into a pipeline job or check it into your repo as a Jenkinsfile.

Full Pipeline Code

pipeline {
    agent any
    stages {
        stage('Main Job: Parallel A & B') {
            parallel {
                stage('Job A Workflow') {
                    stages {
                        stage('Run Job A1') {
                            steps {
                                // Trigger Job A1 and wait for it to finish
                                build job: 'Job A1', wait: true
                            }
                        }
                    }
                }
                stage('Job B Workflow') {
                    stages {
                        stage('Run Job B1') {
                            steps {
                                build job: 'Job B1', wait: true
                            }
                        }
                        stage('Run Job B2') {
                            steps {
                                build job: 'Job B2', wait: true
                            }
                        }
                        stage('Run B3 & B4 in Parallel') {
                            parallel {
                                stage('Run Job B3') {
                                    steps {
                                        build job: 'Job B3', wait: true
                                    }
                                }
                                stage('Run Job B4') {
                                    steps {
                                        build job: 'Job B4', wait: true
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        stage('Run Last Job') {
            steps {
                echo "All prerequisite jobs (A1, B3, B4) completed successfully"
                build job: 'Last Job', wait: true
            }
        }
    }
    post {
        success {
            echo "Entire workflow finished without issues!"
        }
        failure {
            echo "One or more jobs failed in the workflow—check logs for details"
        }
    }
}

How This Flow Works

  • The top-level parallel block launches Job A Workflow and Job B Workflow at the same time (matches your "Main job parallel triggers Job A and Job B" requirement)
  • Job A Workflow runs only Job A1 (since Job A's sole task is to trigger A1)
  • Job B Workflow runs sequentially: B1 → B2 → then B3 and B4 in parallel
  • The final Run Last Job stage only starts after both parallel workflows finish—this guarantees A1, B3, and B4 are all complete (because Job B's workflow doesn't finish until B3/B4 are done)

UI Setup Steps for Pipeline Job

  1. Go to your Jenkins dashboard → click New Item
  2. Name your job (e.g., "Main Pipeline Workflow"), select Pipeline, then click OK
  3. Scroll to the Pipeline section:
    • Choose Pipeline script if you want to paste the code directly, or Pipeline script from SCM if you're storing the Jenkinsfile in your repository
    • Paste the Groovy code above into the script box (if using Pipeline script)
  4. Save the job, then click Build Now to test the flow

Visual Stage View

When the job runs, click the Stage View link in the left sidebar to see the flow graphically:

  • A top-level stage with two parallel boxes: "Job A Workflow" and "Job B Workflow"
  • "Job A Workflow" has one sub-stage: "Run Job A1"
  • "Job B Workflow" has sequential sub-stages: "Run Job B1" → "Run Job B2" → then two parallel boxes for "Run Job B3" and "Run Job B4"
  • Once all those stages complete, the final "Run Last Job" stage executes

2. Alternative: Traditional Freestyle Jobs with Plugins

If you prefer a non-code approach, use these plugins to wire up dependencies:

  • Parameterized Trigger Plugin: To trigger other jobs
  • Join Plugin: To wait for parallel jobs to finish before launching the last job

Step-by-Step Setup

  1. First, create all your base jobs: Job A1, Job B1, Job B2, Job B3, Job B4, Last Job (as freestyle jobs or your preferred type)
  2. Configure Job A:
    • Add a build step: Trigger/call builds on other projects
    • Select Job A1, check Block until the triggered projects finish their builds
  3. Configure Job B:
    • Add build steps in this order:
      1. Trigger Job B1 (block until finish)
      2. Trigger Job B2 (block until finish)
  4. Configure Job B2:
    • Add a build step: Trigger/call builds on other projects
    • Select both Job B3 and Job B4, check Trigger builds concurrently and Block until finish
  5. Configure Main Job:
    • Add a build step: Trigger/call builds on other projects
    • Select Job A and Job B, check Trigger builds concurrently and Block until finish
    • Add a second build step: Join (from the Join Plugin), then configure it to trigger Last Job once the parallel triggers complete

Notes on This Approach

  • This works but is less maintainable than pipeline as code—any changes to the flow require updating multiple job configurations instead of a single script
  • Make sure both plugins are installed via Manage Jenkins > Plugins

3. Verification

After setting up either method, run the Main Job and confirm:

  • Job A and Job B start simultaneously
  • Job A1 runs immediately after Job A launches
  • Job B1 → B2 → B3/B4 run in the correct sequence (B3 and B4 in parallel)
  • Last Job only starts after A1, B3, and B4 have all successfully completed

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:42:00