Matrix builds for multi-platform in Jenkins - Time & Space Complexity
When using matrix builds in Jenkins, we run the same job on many platforms or configurations.
We want to know how the total work grows as we add more platforms.
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent none
stages {
stage('Build') {
matrix {
axes {
axis {
name 'PLATFORM'
values 'linux', 'windows', 'mac'
}
}
stages {
stage('Compile') {
agent { label "${PLATFORM}" }
steps {
echo "Building on ${PLATFORM}"
}
}
}
}
}
}
}
This pipeline runs the same build on three platforms: linux, windows, and mac.
Look for repeated work in the matrix build.
- Primary operation: Running the build stage for each platform.
- How many times: Once per platform in the matrix (3 times here).
As the number of platforms increases, the total build runs increase too.
| Input Size (n platforms) | Approx. Operations (build runs) |
|---|---|
| 3 | 3 |
| 10 | 10 |
| 100 | 100 |
Pattern observation: The total work grows directly with the number of platforms.
Time Complexity: O(n)
This means if you double the platforms, the total build time roughly doubles.
[X] Wrong: "Matrix builds run all platforms at the same time, so total time stays the same no matter how many platforms."
[OK] Correct: While builds can run in parallel, resource limits and queueing often cause total time to increase as platforms grow.
Understanding how matrix builds scale helps you design efficient pipelines and explain trade-offs clearly.
"What if we added another axis with multiple values? How would that affect the time complexity?"
Practice
matrix build in Jenkins for multi-platform projects?Solution
Step 1: Understand matrix build concept
A matrix build runs the same steps on different combinations of variables like OS and architecture.Step 2: Identify the purpose for multi-platform
This allows testing and building on all required platforms automatically without manual intervention.Final Answer:
To run the same build steps on multiple platform combinations automatically -> Option AQuick Check:
Matrix build = multi-platform automatic runs [OK]
- Thinking matrix builds run only on one platform
- Confusing matrix with manual selection
- Assuming matrix skips tests
os and arch in a Jenkins matrix build?Solution
Step 1: Recall Jenkins matrix axes syntax
Jenkins usesaxes { axis { name '...' ; values '...' } }blocks to define axes.Step 2: Match syntax with options
axes { axis { name 'os'; values 'linux', 'windows' } axis { name 'arch'; values 'x86', 'arm' } } matches the correct syntax with twoaxisblocks insideaxes.Final Answer:
axes { axis { name 'os'; values 'linux', 'windows' } axis { name 'arch'; values 'x86', 'arm' } } -> Option DQuick Check:
Correct axis syntax = axes { axis { name 'os'; values 'linux', 'windows' } axis { name 'arch'; values 'x86', 'arm' } } [OK]
- Using incorrect matrix or axis syntax
- Mixing JSON-like syntax instead of Groovy
- Missing semicolons or braces
axes {
axis {
name 'os'
values 'linux', 'windows'
}
axis {
name 'arch'
values 'x86', 'arm'
}
}
stages {
stage('Build') {
steps {
echo "Building on ${os} for ${arch}"
}
}
}How many total build combinations will Jenkins run?
Solution
Step 1: Count values in each axis
Axis 'os' has 2 values: linux, windows. Axis 'arch' has 2 values: x86, arm.Step 2: Calculate total combinations
Total combinations = 2 (os) * 2 (arch) = 4.Final Answer:
4 -> Option CQuick Check:
2 OS * 2 arch = 4 builds [OK]
- Adding instead of multiplying axis values
- Counting only one axis
- Misreading axis values count
axes {
axis {
name 'os'
values 'linux', 'windows'
}
axis {
name 'arch'
value 'x86', 'arm'
}
}What is the error and how to fix it?
Solution
Step 1: Identify syntax error in axis definition
The second axis uses 'value' instead of the correct 'values' keyword.Step 2: Correct the keyword
Change 'value' to 'values' to fix the syntax error and allow multiple values.Final Answer:
Typo: use 'values' instead of 'value' in second axis -> Option AQuick Check:
Axis keyword must be 'values' [OK]
- Using 'value' instead of 'values'
- Forgetting commas between values
- Misnaming axes block
Solution
Step 1: Understand agent labeling per axis value
You want 'arm' builds to run only on 'arm-node' labeled agents, others on default.Step 2: Use conditional label assignment in agent block
Usingagent { label "${arch == 'arm' ? 'arm-node' : ''}" }assigns 'arm-node' label only for arm builds.Final Answer:
Useagent { label "${arch == 'arm' ? 'arm-node' : ''}" }inside the matrix -> Option BQuick Check:
Conditional agent label = Useagent { label "${arch == 'arm' ? 'arm-node' : ''}" }inside the matrix [OK]
- Setting global agent label affecting all builds
- Skipping arm builds instead of assigning correct agent
- Excluding arm builds instead of targeting agent
Ready to prove it?
Pass the test and this pattern moves unknown
