0
0
Jenkinsdevops~5 mins

Copying artifacts between jobs in Jenkins - Commands & Configuration

Choose your learning style9 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.