配置多层级并行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 Pluginsif needed) - Optional: For visual flow tracking, install the Pipeline Stage View Plugin (adds a nice graphical breakdown of your pipeline stages)
1. Recommended: Declarative Pipeline as Code
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
parallelblock 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 Jobstage 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
- Go to your Jenkins dashboard → click
New Item - Name your job (e.g., "Main Pipeline Workflow"), select Pipeline, then click
OK - Scroll to the Pipeline section:
- Choose
Pipeline scriptif you want to paste the code directly, orPipeline script from SCMif you're storing theJenkinsfilein your repository - Paste the Groovy code above into the script box (if using
Pipeline script)
- Choose
- Save the job, then click
Build Nowto 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
- 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) - Configure Job A:
- Add a build step:
Trigger/call builds on other projects - Select
Job A1, checkBlock until the triggered projects finish their builds
- Add a build step:
- Configure Job B:
- Add build steps in this order:
- Trigger
Job B1(block until finish) - Trigger
Job B2(block until finish)
- Trigger
- Add build steps in this order:
- Configure Job B2:
- Add a build step:
Trigger/call builds on other projects - Select both
Job B3andJob B4, checkTrigger builds concurrentlyandBlock until finish
- Add a build step:
- Configure Main Job:
- Add a build step:
Trigger/call builds on other projects - Select
Job AandJob B, checkTrigger builds concurrentlyandBlock until finish - Add a second build step:
Join(from the Join Plugin), then configure it to triggerLast Jobonce the parallel triggers complete
- Add a build step:
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 Jobonly starts after A1, B3, and B4 have all successfully completed
内容的提问来源于stack exchange,提问作者Shivam Seth

