Archiving artifacts in Jenkins - Time & Space Complexity
When Jenkins archives artifacts, it saves files from a build for later use. Understanding how the time to archive grows helps us plan for bigger projects.
We want to know: how does archiving time change as the number of files or their size increases?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
// build steps here
}
}
stage('Archive') {
steps {
archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
}
}
}
}
This code archives all .jar files found under any target folder after the build finishes.
Look for repeated actions that affect time.
- Primary operation: Scanning the workspace to find matching files and copying each file to the archive storage.
- How many times: Once per file found matching the pattern; each file is processed individually.
As the number of files grows, the time to archive grows too.
| Input Size (n files) | Approx. Operations |
|---|---|
| 10 | About 10 file scans and copies |
| 100 | About 100 file scans and copies |
| 1000 | About 1000 file scans and copies |
Pattern observation: The time grows roughly in direct proportion to the number of files.
Time Complexity: O(n)
This means if you double the number of files, the archiving time roughly doubles too.
[X] Wrong: "Archiving time stays the same no matter how many files there are."
[OK] Correct: Each file must be found and copied, so more files mean more work and more time.
Knowing how archiving scales helps you understand build pipelines better. It shows you how tasks grow with project size, a useful skill in real DevOps work.
"What if we changed the archive pattern to include subfolders with many files? How would the time complexity change?"
Practice
archiveArtifacts in a Jenkins pipeline?Solution
Step 1: Understand the role of
This command is used to save files generated by a build so they can be accessed later.archiveArtifactsStep 2: Compare with other options
Deleting files, triggering builds, or sending emails are different Jenkins features, not related to archiving artifacts.Final Answer:
To save build output files for later use or sharing -> Option BQuick Check:
Archiving = Saving build files [OK]
- Confusing archiving with deleting files
- Thinking it triggers other jobs
- Assuming it sends notifications
.jar files in a Jenkins pipeline script?Solution
Step 1: Recall the syntax of
The correct syntax uses the command followed by the file pattern in quotes without extra characters.archiveArtifactsStep 2: Identify incorrect syntax
Options with semicolon, braces, or parentheses after the pattern are invalid in Jenkins pipeline syntax.Final Answer:
archiveArtifacts '*.jar' -> Option AQuick Check:
Correct syntax = archiveArtifacts 'pattern' [OK]
- Adding semicolons inside the pattern string
- Using braces or parentheses incorrectly
- Missing quotes around the pattern
stage('Build') {
steps {
sh 'echo Hello > output.txt'
archiveArtifacts 'output.txt'
}
}What will Jenkins do after this stage completes?
Solution
Step 1: Understand the shell command
The command creates a file namedoutput.txtwith the text 'Hello'.Step 2: Analyze
This command archives the specified file so it is saved with the build for later access.archiveArtifactsusageFinal Answer:
Save the fileoutput.txtas a build artifact -> Option DQuick Check:
archiveArtifacts saves files [OK]
- Assuming the file is deleted after archiving
- Thinking the build fails if file exists
- Confusing archiving with emailing files
archiveArtifacts '*.log'
But no files are archived after the build. What is the most likely reason?
Solution
Step 1: Check the file pattern and workspace
If no.logfiles exist in the workspace, nothing will be archived.Step 2: Verify syntax and Jenkins support
The syntax is correct and Jenkins supports archiving any file type.Step 3: Consider build status
If the build failed before this step, archiving wouldn't run, but the question implies the step ran.Final Answer:
No.logfiles were created in the workspace -> Option AQuick Check:
Files must exist to be archived [OK]
- Assuming Jenkins can't archive certain file types
- Blaming syntax without checking files
- Ignoring build step execution order
.jar files in the build/libs folder and also keep the build logs named build.log. Which archiveArtifacts usage is correct?Solution
Step 1: Understand multiple file patterns in
To archive multiple patterns, use the named parameterarchiveArtifactsartifactswith a comma-separated string.Step 2: Analyze each option
archiveArtifacts 'build/libs/*.jar', build.log passes two positional arguments (invalid), archiveArtifacts 'build/libs/*.jar build.log' lacks comma separator, archiveArtifacts artifacts: 'build/libs/*.jar', 'build.log' has invalid syntax with two strings.Final Answer:
archiveArtifacts artifacts: 'build/libs/*.jar, build.log' -> Option CQuick Check:
Use artifacts: 'pattern1, pattern2' for multiple files [OK]
- Omitting the artifacts: parameter
- Separating patterns without commas
- Passing multiple strings instead of one
Ready to prove it?
Pass the test and this pattern moves unknown
