📖 Scenario: You are working on a Jenkins pipeline to speed up your software testing process. You want to run multiple test suites at the same time instead of one after another. This will save time and give faster feedback on your code changes.
🎯 Goal: Create a Jenkins pipeline script that runs two test stages in parallel and then shows the combined result.
📋 What You'll Learn
Create a Jenkins pipeline with a stages block
Add two test stages named Test Suite 1 and Test Suite 2
Run these two test stages in parallel using the parallel directive
Print a message after both tests complete
💡 Why This Matters
🌍 Real World
Running tests in parallel in Jenkins pipelines saves time and speeds up software delivery.
💼 Career
DevOps engineers often create Jenkins pipelines that run tests in parallel to improve CI/CD efficiency.
Progress0 / 4 steps
1
Create basic Jenkins pipeline structure
Write a Jenkins pipeline script that starts with pipeline { and includes an empty stages {} block.
Jenkins
Hint
Start with pipeline {, add agent any to run on any available agent, and add an empty stages {} block.
2
Add two test stages inside a parallel block
Inside the stages block, add a stage named Parallel Tests that uses the parallel directive to run two stages named Test Suite 1 and Test Suite 2. Each test stage should have a steps block with a echo command printing Running Test Suite 1 or Running Test Suite 2 respectively.
Jenkins
Hint
Use parallel inside a stage to run multiple stages at the same time. Each parallel branch needs a name and a steps block.
3
Add a final stage to print completion message
After the Parallel Tests stage, add another stage named Summary with a steps block that uses echo to print All tests completed successfully.
Jenkins
Hint
Add a new stage after the parallel tests to show a message when all tests finish.
4
Print the final pipeline script
Print the entire Jenkins pipeline script to verify it runs the two test suites in parallel and then prints the completion message.
Jenkins
Hint
Review the full pipeline script to ensure it matches the expected structure for parallel test execution.
Practice
(1/5)
1. What is the main purpose of using the parallel block in a Jenkins pipeline?
easy
A. To stop the pipeline if any test fails
B. To run multiple test groups at the same time to save time
C. To run tests one after another in a sequence
D. To deploy the application automatically after tests
Solution
Step 1: Understand the parallel block function
The parallel block in Jenkins allows running multiple tasks simultaneously.
Step 2: Identify the benefit of parallel execution
Running tests in parallel saves time by executing multiple test groups at once instead of sequentially.
Final Answer:
To run multiple test groups at the same time to save time -> Option B
Quick Check:
Parallel execution = run tests simultaneously [OK]
Hint: Parallel means running tasks at the same time [OK]
Common Mistakes:
Thinking parallel runs tests one by one
Confusing parallel with deployment steps
Assuming parallel stops on first failure
2. Which of the following is the correct syntax to run two test stages in parallel in a Jenkins declarative pipeline?
Forgetting commas between map entries (not the main error here)
Assuming indentation causes syntax errors
5. You want to run three test suites in parallel in a Jenkins declarative pipeline and fail the entire build if any test fails. Which snippet correctly achieves this?
hard
A. stages {
stage('Parallel Tests') {
parallel {
Test1: { steps { sh 'run_test1.sh' } }
Test2: { steps { sh 'run_test2.sh' } }
Test3: { steps { sh 'run_test3.sh' } }
}
}
}
B. parallel {
stage('Test1') { steps { sh 'run_test1.sh' } }
stage('Test2') { steps { sh 'run_test2.sh' } }
stage('Test3') { steps { sh 'run_test3.sh' } }
}
C. stages {
parallel {
stage('Test1') { steps { sh 'run_test1.sh' } }
stage('Test2') { steps { sh 'run_test2.sh' } }
stage('Test3') { steps { sh 'run_test3.sh' } }
}
}
D. stage('Parallel Tests') {
parallel(
Test1: { sh 'run_test1.sh' },
Test2: { sh 'run_test2.sh' },
Test3: { sh 'run_test3.sh' }
)
}
Solution
Step 1: Recall declarative pipeline structure for parallel stages
Declarative pipelines require a stages block containing stage blocks; parallel tests go inside a single stage with a parallel block.
Step 2: Identify correct syntax for parallel tests with failure propagation
stages {
stage('Parallel Tests') {
parallel {
Test1: { steps { sh 'run_test1.sh' } }
Test2: { steps { sh 'run_test2.sh' } }
Test3: { steps { sh 'run_test3.sh' } }
}
}
} correctly uses stages block with a stage named 'Parallel Tests' containing a parallel block where each parallel branch is a closure with steps and sh commands. This is the proper declarative syntax and ensures the build fails if any test fails.
Final Answer:
stages {
stage('Parallel Tests') {
parallel {
Test1: { steps { sh 'run_test1.sh' } }
Test2: { steps { sh 'run_test2.sh' } }
Test3: { steps { sh 'run_test3.sh' } }
}
}
} -> Option A
Quick Check:
Declarative parallel inside a stage within stages block with steps closures [OK]
Hint: Use parallel inside a stage within stages block with steps closures [OK]
Common Mistakes:
Placing parallel outside stages block
Using parallel without wrapping in a stage
Mixing scripted and declarative syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.