Copying artifacts between jobs in Jenkins - Time & Space Complexity
When copying artifacts between Jenkins jobs, it's important to know how the time to copy grows as the number of artifacts increases.
We want to understand how the copying process scales with more files.
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Copy Artifacts') {
steps {
copyArtifacts(projectName: 'upstream-job', selector: lastSuccessful())
}
}
}
}
This code copies all artifacts from the last successful build of another job.
Look for repeated actions that affect time.
- Primary operation: Copying each artifact file one by one.
- How many times: Once for each artifact file in the source job.
As the number of artifact files grows, the copying time grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 file copy operations |
| 100 | 100 file copy operations |
| 1000 | 1000 file copy operations |
Pattern observation: The time grows directly with the number of files copied.
Time Complexity: O(n)
This means the copying time increases linearly as the number of artifact files increases.
[X] Wrong: "Copying artifacts happens instantly no matter how many files there are."
[OK] Correct: Each file must be copied individually, so more files mean more time.
Understanding how copying artifacts scales helps you design efficient pipelines and manage build times well.
What if we compressed all artifacts into one file before copying? How would the time complexity change?
Practice
copyArtifacts step in Jenkins?Solution
Step 1: Understand the
ThecopyArtifactsstepcopyArtifactsstep is designed to transfer files (artifacts) from one Jenkins job to another.Step 2: Identify the main use case
This step helps share build outputs between jobs without rebuilding, enabling reuse of files.Final Answer:
To copy files from one Jenkins job to another -> Option DQuick Check:
copyArtifacts = copy files between jobs [OK]
- Confusing copyArtifacts with job triggers
- Thinking it deletes files instead of copying
- Assuming it compiles code
BuildJob in a Jenkins pipeline script?Solution
Step 1: Recall the correct syntax for copyArtifacts
The correct syntax uses named parameters, withprojectNamespecifying the source job.Step 2: Match the syntax to the options
copyArtifacts(projectName: 'BuildJob') usescopyArtifacts(projectName: 'BuildJob'), which is the correct form in pipeline scripts.Final Answer:
copyArtifacts(projectName: 'BuildJob') -> Option AQuick Check:
Use named parameter projectName in copyArtifacts [OK]
- Using positional arguments instead of named parameters
- Using incorrect keywords like job or from
- Missing parentheses or quotes
copyArtifacts(projectName: 'BuildJob', filter: '**/*.jar')
Solution
Step 1: Understand the filter parameter
Thefilterparameter uses Ant-style patterns to select files. '**/*.jar' means all .jar files in any folder.Step 2: Determine which files are copied
Only artifact files matching the pattern '*.jar' from the 'BuildJob' are copied, not all files.Final Answer:
Only files with .jar extension from BuildJob artifacts -> Option AQuick Check:
filter '**/*.jar' copies only .jar files [OK]
- Assuming all files copy without filter
- Thinking filter is a filename, not a pattern
- Believing syntax causes error here
copyArtifacts(projectName: BuildJob)What is the likely cause of the failure?
Solution
Step 1: Check syntax for projectName parameter
In pipeline scripts, string values must be quoted. Missing quotes cause syntax errors.Step 2: Identify the error cause
Since BuildJob is unquoted, Jenkins treats it as a variable, causing failure if undefined.Final Answer:
Missing quotes around the job name 'BuildJob' -> Option BQuick Check:
Job names must be quoted strings [OK]
- Forgetting quotes around job names
- Assuming filter is mandatory
- Ignoring existence of source job
output.zip artifact from job PackageJob to your current job. Which snippet correctly does this?Solution
Step 1: Understand selecting the build to copy from
Theselectorparameter chooses which build's artifacts to copy.lastSuccessful()picks the latest successful build.Step 2: Verify filter and selector usage
copyArtifacts(projectName: 'PackageJob', selector: lastSuccessful(), filter: 'output.zip') usesselector: lastSuccessful()and filters for 'output.zip', matching the requirement exactly.Final Answer:
copyArtifacts(projectName: 'PackageJob', selector: lastSuccessful(), filter: 'output.zip') -> Option CQuick Check:
Use selector lastSuccessful() to copy latest successful build [OK]
- Using selector latest() which is invalid
- Omitting selector and copying from last build regardless of status
- Using lastCompleted() which may include failed builds
Ready to prove it?
Pass the test and this pattern moves unknown
