Build, test, deploy stages concept in Jenkins - Time & Space Complexity
We want to understand how the time taken by a Jenkins pipeline grows as we add more build, test, and deploy steps.
How does the total work change when we increase the number of tasks in each stage?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
for (int i = 0; i < buildTasks.size(); i++) {
echo "Building task ${i}"
}
}
}
}
stage('Test') {
steps {
script {
for (int j = 0; j < testTasks.size(); j++) {
echo "Testing task ${j}"
}
}
}
}
stage('Deploy') {
steps {
script {
for (int k = 0; k < deployTasks.size(); k++) {
echo "Deploying task ${k}"
}
}
}
}
}
}
This pipeline runs three stages: build, test, and deploy. Each stage loops over a list of tasks and performs an action for each.
Look at the loops that repeat work in each stage.
- Primary operation: Looping over tasks in build, test, and deploy stages.
- How many times: Each loop runs once per task in its list (buildTasks, testTasks, deployTasks).
As the number of tasks grows, the total work grows by adding the work of each stage.
| Input Size (tasks per stage) | Approx. Operations (total loops) |
|---|---|
| 10 | 10 + 10 + 10 = 30 |
| 100 | 100 + 100 + 100 = 300 |
| 1000 | 1000 + 1000 + 1000 = 3000 |
Pattern observation: The total work grows linearly with the number of tasks in each stage combined.
Time Complexity: O(n)
This means the total time grows in a straight line as we add more tasks to build, test, and deploy.
[X] Wrong: "Because there are three stages, the time grows like n cubed (n³)."
[OK] Correct: The stages run one after another, so their times add up, not multiply. Each stage loops over tasks once, so total time grows linearly, not exponentially.
Understanding how pipeline stages add up in time helps you design efficient workflows and explain your choices clearly in interviews.
What if the test stage ran tests in parallel instead of one by one? How would the time complexity change?
Practice
build, test, and deploy stages?Solution
Step 1: Understand the purpose of stages
Stages separate the pipeline into logical parts: building, testing, and deploying.Step 2: Recognize benefits of stages
This separation helps find errors early and keeps the process organized.Final Answer:
To organize the process into clear steps and catch errors early -> Option AQuick Check:
Stages = clear steps + early error detection [OK]
- Thinking stages run in parallel by default
- Skipping test stage to save time
- Confusing build with deploy
build stage in a Jenkins declarative pipeline?Solution
Step 1: Recall Jenkins declarative syntax
A stage is defined asstage('name') { steps { ... } }.Step 2: Match syntax with options
stage('build') { steps { sh 'make build' } } correctly usesstage('build') { steps { sh 'make build' } }.Final Answer:
stage('build') { steps { sh 'make build' } } -> Option AQuick Check:
Stage syntax = stage('name') + steps block [OK]
- Placing steps outside stage block
- Using build as a block instead of stage
- Missing steps keyword
pipeline {
agent any
stages {
stage('build') {
steps {
echo 'Building...'
}
}
stage('test') {
steps {
error 'Test failed!'
}
}
stage('deploy') {
steps {
echo 'Deploying...'
}
}
}
}What will be the output when this pipeline runs?
Solution
Step 1: Analyze build stage
The build stage prints 'Building...' successfully.Step 2: Analyze test stage
The test stage runserror 'Test failed!'which stops the pipeline with failure.Step 3: Analyze deploy stage
Because test failed, deploy stage does not run.Final Answer:
Build stage runs, then test stage fails and stops pipeline; deploy does not run -> Option CQuick Check:
Failure in test stops pipeline before deploy [OK]
- Assuming deploy runs despite test failure
- Thinking error only logs message without stopping
- Ignoring pipeline stop on error
pipeline {
agent any
stages {
stage('build') {
steps {
sh 'make build'
}
}
stage('test') {
sh 'make test'
}
stage('deploy') {
steps {
sh 'make deploy'
}
}
}
}Why does this pipeline fail to run correctly?
Solution
Step 1: Check syntax of each stage
Build and deploy stages havesteps { ... }blocks, but test stage does not.Step 2: Understand Jenkins syntax rules
Each stage must have astepsblock to run commands; missing it causes failure.Final Answer:
Theteststage is missing thestepsblock -> Option BQuick Check:
Missing steps block in stage = syntax error [OK]
- Forgetting steps block inside a stage
- Mixing shell commands syntax
- Misplacing agent keyword
Solution
Step 1: Understand pipeline flow
Build runs first, then test, then deploy only if test passes (pipeline success).Step 2: Analyze post blocks
pipeline { agent any stages { stage('build') { steps { sh 'build.sh' } } stage('test') { steps { sh 'test.sh' } } stage('deploy') { steps { sh 'deploy.sh' } } } post { success { echo 'Deployment complete' } } } usespost { success { ... } }to confirm deployment success message, matching requirement.Step 3: Check other options
pipeline { agent any stages { stage('build') { steps { sh 'build.sh' } } stage('test') { steps { sh 'test.sh' } } stage('deploy') { steps { sh 'deploy.sh' } } } } lacks error handling; B prints message on failure but does not prevent deploy; C deploys always.Final Answer:
Option D correctly runs deploy only on success and confirms deployment -> Option DQuick Check:
Deploy runs only if tests pass = post success block [OK]
- Deploy running even if tests fail
- Missing post blocks for error handling
- Confusing post always with success
Ready to prove it?
Pass the test and this pattern moves unknown
