Bird
Raised Fist0
Jenkinsdevops~5 mins

Copying artifacts between jobs in Jenkins - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you build something in one Jenkins job and want to use the result in another job. Copying artifacts between jobs lets you share files like compiled programs or reports easily.
When you build a software package in one job and want to test it in another job.
When you generate documentation in one job and want to publish it in a separate job.
When you run a build on one server and deploy from another job on a different server.
When you want to separate build and deployment steps into different jobs for clarity.
When you want to keep your pipeline modular by passing files between jobs.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the app'
        sh 'echo Hello World > output.txt'
        archiveArtifacts artifacts: 'output.txt', fingerprint: true
      }
    }
    stage('Use Artifact') {
      steps {
        copyArtifacts(projectName: 'example-build-job', selector: lastSuccessful())
        sh 'cat output.txt'
      }
    }
  }
}

This Jenkinsfile defines two stages: Build and Use Artifact.

In the Build stage, it creates a file output.txt and archives it as an artifact.

In the Use Artifact stage, it copies the artifact from the job named example-build-job using the copyArtifacts step and then prints its content.

Commands
This command creates a simple text file named output.txt with the content 'Hello World'. It simulates a build artifact.
Terminal
echo Hello World > output.txt
Expected OutputExpected
No output (command runs silently)
This Jenkins pipeline step archives the output.txt file so it can be shared or downloaded later. Fingerprinting helps track the artifact.
Terminal
archiveArtifacts artifacts: 'output.txt', fingerprint: true
Expected OutputExpected
[Pipeline] archiveArtifacts Archiving artifacts Finished: SUCCESS
→
artifacts - Specifies which files to archive
→
fingerprint - Enables tracking of the artifact
This step copies artifacts from the last successful build of the job named example-build-job into the current workspace.
Terminal
copyArtifacts(projectName: 'example-build-job', selector: lastSuccessful())
Expected OutputExpected
[Pipeline] copyArtifacts Copying artifacts from example-build-job #42 Finished: SUCCESS
→
projectName - Specifies the source job name
→
selector - Chooses which build's artifacts to copy
This command displays the content of the copied output.txt file to verify the artifact was copied correctly.
Terminal
cat output.txt
Expected OutputExpected
Hello World
Key Concept

If you remember nothing else from this pattern, remember: archiving artifacts in one job and using copyArtifacts in another lets you share files easily between Jenkins jobs.

Common Mistakes
Not archiving artifacts in the source job before trying to copy them.
If artifacts are not archived, there is nothing to copy, so the copyArtifacts step will fail or find no files.
Always use archiveArtifacts in the producing job to save files you want to share.
Using the wrong projectName in copyArtifacts step.
If the projectName does not match the source job exactly, Jenkins cannot find the artifacts to copy.
Double-check the source job name spelling and case in the copyArtifacts step.
Trying to copy artifacts before the source job has a successful build.
copyArtifacts with lastSuccessful selector requires at least one successful build to copy from.
Ensure the source job has completed successfully at least once before copying artifacts.
Summary
Use archiveArtifacts in the source job to save files you want to share.
Use copyArtifacts in the target job to copy those files from the source job.
Verify the copied files by checking their content or existence in the target job.

Practice

(1/5)
1. What is the main purpose of using the copyArtifacts step in Jenkins?
easy
A. To compile source code in a Jenkins job
B. To delete artifacts after a build completes
C. To trigger a new Jenkins job automatically
D. To copy files from one Jenkins job to another

Solution

  1. Step 1: Understand the copyArtifacts step

    The copyArtifacts step is designed to transfer files (artifacts) from one Jenkins job to another.
  2. Step 2: Identify the main use case

    This step helps share build outputs between jobs without rebuilding, enabling reuse of files.
  3. Final Answer:

    To copy files from one Jenkins job to another -> Option D
  4. Quick Check:

    copyArtifacts = copy files between jobs [OK]
Hint: copyArtifacts copies files between jobs, not triggers or deletes [OK]
Common Mistakes:
  • Confusing copyArtifacts with job triggers
  • Thinking it deletes files instead of copying
  • Assuming it compiles code
2. Which of the following is the correct syntax to copy artifacts from a job named BuildJob in a Jenkins pipeline script?
easy
A. copyArtifacts(projectName: 'BuildJob')
B. copyArtifacts('BuildJob')
C. copyArtifacts job: 'BuildJob'
D. copyArtifacts from: 'BuildJob'

Solution

  1. Step 1: Recall the correct syntax for copyArtifacts

    The correct syntax uses named parameters, with projectName specifying the source job.
  2. Step 2: Match the syntax to the options

    copyArtifacts(projectName: 'BuildJob') uses copyArtifacts(projectName: 'BuildJob'), which is the correct form in pipeline scripts.
  3. Final Answer:

    copyArtifacts(projectName: 'BuildJob') -> Option A
  4. Quick Check:

    Use named parameter projectName in copyArtifacts [OK]
Hint: Use named parameter projectName: 'JobName' in copyArtifacts [OK]
Common Mistakes:
  • Using positional arguments instead of named parameters
  • Using incorrect keywords like job or from
  • Missing parentheses or quotes
3. Given the following Jenkins pipeline snippet, what files will be copied?
copyArtifacts(projectName: 'BuildJob', filter: '**/*.jar')
medium
A. Only files with .jar extension from BuildJob artifacts
B. All files from BuildJob workspace
C. Only files named exactly 'filter' from BuildJob
D. No files will be copied due to syntax error

Solution

  1. Step 1: Understand the filter parameter

    The filter parameter uses Ant-style patterns to select files. '**/*.jar' means all .jar files in any folder.
  2. Step 2: Determine which files are copied

    Only artifact files matching the pattern '*.jar' from the 'BuildJob' are copied, not all files.
  3. Final Answer:

    Only files with .jar extension from BuildJob artifacts -> Option A
  4. Quick Check:

    filter '**/*.jar' copies only .jar files [OK]
Hint: filter '**/*.ext' copies only files with that extension [OK]
Common Mistakes:
  • Assuming all files copy without filter
  • Thinking filter is a filename, not a pattern
  • Believing syntax causes error here
4. You wrote this snippet to copy artifacts but it fails:
copyArtifacts(projectName: BuildJob)
What is the likely cause of the failure?
medium
A. copyArtifacts does not accept projectName parameter
B. Missing quotes around the job name 'BuildJob'
C. The job BuildJob does not exist
D. copyArtifacts requires a filter parameter

Solution

  1. Step 1: Check syntax for projectName parameter

    In pipeline scripts, string values must be quoted. Missing quotes cause syntax errors.
  2. Step 2: Identify the error cause

    Since BuildJob is unquoted, Jenkins treats it as a variable, causing failure if undefined.
  3. Final Answer:

    Missing quotes around the job name 'BuildJob' -> Option B
  4. Quick Check:

    Job names must be quoted strings [OK]
Hint: Always quote job names in copyArtifacts [OK]
Common Mistakes:
  • Forgetting quotes around job names
  • Assuming filter is mandatory
  • Ignoring existence of source job
5. You want to copy only the latest successful build's output.zip artifact from job PackageJob to your current job. Which snippet correctly does this?
hard
A. copyArtifacts(projectName: 'PackageJob', selector: latest(), filter: 'output.zip')
B. copyArtifacts(projectName: 'PackageJob', filter: 'output.zip')
C. copyArtifacts(projectName: 'PackageJob', selector: lastSuccessful(), filter: 'output.zip')
D. copyArtifacts(projectName: 'PackageJob', selector: lastCompleted(), filter: 'output.zip')

Solution

  1. Step 1: Understand selecting the build to copy from

    The selector parameter chooses which build's artifacts to copy. lastSuccessful() picks the latest successful build.
  2. Step 2: Verify filter and selector usage

    copyArtifacts(projectName: 'PackageJob', selector: lastSuccessful(), filter: 'output.zip') uses selector: lastSuccessful() and filters for 'output.zip', matching the requirement exactly.
  3. Final Answer:

    copyArtifacts(projectName: 'PackageJob', selector: lastSuccessful(), filter: 'output.zip') -> Option C
  4. Quick Check:

    Use selector lastSuccessful() to copy latest successful build [OK]
Hint: Use selector lastSuccessful() to get latest successful build artifacts [OK]
Common Mistakes:
  • Using selector latest() which is invalid
  • Omitting selector and copying from last build regardless of status
  • Using lastCompleted() which may include failed builds
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard