Discover how a simple step can save hours of manual work and prevent costly mistakes in your builds!
Why Copying artifacts between jobs in Jenkins? - Purpose & Use Cases
Imagine you have two separate Jenkins jobs: one builds your software, and the other runs tests. You need to move the build files from the first job to the second manually.
Manually copying files between jobs is slow and risky. You might forget files, copy wrong versions, or waste time searching for the right artifacts. This causes delays and errors in your pipeline.
Using artifact copying between jobs automates this transfer. Jenkins securely and reliably passes the exact files needed from one job to another without manual steps.
scp build/output.zip user@server:/test-job/input/
ssh user@server 'cd /test-job && ./run-tests.sh'copyArtifacts(projectName: 'BuildJob', filter: 'output.zip') sh './run-tests.sh'
This lets your pipeline flow smoothly, saving time and avoiding mistakes by automatically sharing build results between jobs.
When building a mobile app, the build job creates an APK file. The test job then automatically gets this APK to run tests, ensuring only the latest version is tested.
Manual file copying is slow and error-prone.
Copying artifacts automates file sharing between Jenkins jobs.
This improves pipeline speed and reliability.
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
