📖 Scenario: You are setting up a Jenkins pipeline to run multiple tasks at the same time. This helps finish work faster, like cooking several dishes together instead of one after another.
🎯 Goal: Build a Jenkins pipeline script that runs two stages in parallel: Build and Test. Each stage should print a simple message.
📋 What You'll Learn
Create a Jenkins pipeline with a pipeline block
Add a stages block inside the pipeline
Inside stages, add a parallel block with two stages named Build and Test
Each stage should have a steps block with a echo command printing exactly Building project... or Testing project...
The pipeline should run both stages at the same time
💡 Why This Matters
🌍 Real World
In real software projects, running build and test tasks in parallel speeds up delivery. Jenkins pipelines help automate this process.
💼 Career
DevOps engineers use Jenkins pipelines with parallel stages to optimize CI/CD workflows and improve software release speed.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Write a Jenkins pipeline script starting with pipeline { and inside it add an empty stages { } block.
Jenkins
Hint
Start with pipeline { and add stages { } inside it.
2
Add a parallel block with two stages
Inside the stages block, add a parallel { } block containing two stages named Build and Test. Leave their steps blocks empty for now.
Jenkins
Hint
Use parallel { } inside stages and add two stage blocks named Build and Test.
3
Add echo steps to each parallel stage
Inside the steps block of the Build stage, add echo 'Building project...'. Inside the steps block of the Test stage, add echo 'Testing project...'.
Jenkins
Hint
Use echo inside each steps block to print the messages.
4
Run the pipeline and check output
Run the Jenkins pipeline script and verify that the output contains both Building project... and Testing project... messages, showing the stages ran in parallel.
Jenkins
Hint
Check the Jenkins console output after running the pipeline. Both messages should appear, indicating parallel execution.
Practice
(1/5)
1. What is the main purpose of using the parallel block in a Jenkins pipeline?
easy
A. To run multiple stages at the same time to save time
B. To run stages one after another in a sequence
C. To pause the pipeline until manual approval
D. To restart the pipeline from the beginning
Solution
Step 1: Understand the role of parallel block
The parallel block allows multiple tasks or stages to run simultaneously in Jenkins.
Step 2: Compare with other options
Options A, B, and C describe restart, sequential execution, and manual pause, which are unrelated to parallel execution.
Final Answer:
To run multiple stages at the same time to save time -> Option A
Quick Check:
Parallel stages = run together [OK]
Hint: Parallel means tasks run at the same time, not one by one [OK]
Common Mistakes:
Thinking parallel means sequential execution
Confusing parallel with manual approval
Assuming parallel restarts the pipeline
2. Which of the following is the correct syntax to define parallel stages inside a Jenkins pipeline?
A. stage names inside parallel must be unique; rename stages
B. parallel block cannot contain nested stage blocks directly; use map syntax with branches
C. parallel block requires agent declaration inside each stage
D. steps block cannot contain echo commands
Solution
Step 1: Identify parallel block syntax rules
The parallel directive expects a map of branch names to closures containing steps, not loose stage blocks inside {}.
Step 2: Fix the pipeline structure
Change to parallel( "Unit": { echo 'Unit tests' }, "Integration": { echo 'Integration tests' } ) to fix the syntax error.
Final Answer:
parallel block cannot contain nested stage blocks directly; use map syntax with branches -> Option B
Quick Check:
parallel uses branch map syntax [OK]
Hint: parallel must be outside steps block, directly under stage [OK]
Common Mistakes:
Placing parallel inside steps block
Ignoring pipeline syntax hierarchy
Assuming echo commands cause error
5. You want to run three tasks in parallel: 'Build', 'Test', and 'Deploy'. The 'Deploy' stage should only run if both 'Build' and 'Test' succeed. How do you structure the Jenkins pipeline to achieve this?
hard
A. Use parallel for 'Build' and 'Test' stages, then a separate 'Deploy' stage after parallel block
B. Put all three stages inside a single parallel block
C. Run 'Deploy' inside parallel block with 'Build' and 'Test' but add a condition to skip if others fail
D. Run 'Deploy' before 'Build' and 'Test' in parallel block
Solution
Step 1: Understand dependency requirements
'Deploy' must wait for 'Build' and 'Test' to finish successfully, so it cannot run in parallel with them.
Step 2: Structure pipeline accordingly
Run 'Build' and 'Test' inside a parallel block, then add a separate 'Deploy' stage after that block to run only if previous succeed.
Final Answer:
Use parallel for 'Build' and 'Test' stages, then a separate 'Deploy' stage after parallel block -> Option A
Quick Check:
Dependent stage runs after parallel block [OK]
Hint: Run dependent stages after parallel block, not inside it [OK]
Common Mistakes:
Putting dependent stage inside parallel block
Running deploy before build and test
Trying to conditionally skip stages inside parallel
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.