📖 Scenario: You are working on a Jenkins pipeline where you need to build a project in one job and then use the build output (artifact) in another job. This is common when you want to separate build and deployment steps into different jobs.
🎯 Goal: Build a Jenkins pipeline script that copies an artifact from one job to another using the copyArtifacts plugin step.
📋 What You'll Learn
Create a Jenkins pipeline script with two stages: Build and Deploy
In the Build stage, create a file named app.jar as a dummy artifact
Archive the app.jar file as an artifact in the Build stage
In the Deploy stage, copy the artifact app.jar from the Build stage using copyArtifacts
Print a message confirming the artifact was copied
💡 Why This Matters
🌍 Real World
In real projects, build and deployment are often separated into different jobs. Copying artifacts lets you reuse build outputs without rebuilding.
💼 Career
DevOps engineers and Jenkins administrators frequently use artifact copying to create efficient CI/CD pipelines.
Progress0 / 4 steps
1
Create the Build stage with artifact
Create a Jenkins pipeline script with a Build stage that creates a file named app.jar containing the text "dummy artifact" and archives it using archiveArtifacts.
Jenkins
Hint
Use writeFile to create the file and archiveArtifacts to save it as an artifact.
2
Add Deploy stage with copyArtifacts configuration
Add a Deploy stage to the pipeline that uses copyArtifacts to copy the artifact app.jar from the Build stage.
Jenkins
Hint
Use copyArtifacts with projectName: env.JOB_NAME and selector: lastSuccessful() to copy from the same job's last successful build.
3
Add a step to confirm artifact copy
Inside the Deploy stage, add a sh step to list the files in the workspace to confirm that app.jar was copied.
Jenkins
Hint
Use sh 'ls -l' to list files in the workspace.
4
Print confirmation message after copying artifact
Add a echo step inside the Deploy stage after copying the artifact to print "Artifact app.jar copied successfully".
Jenkins
Hint
Use echo 'Artifact app.jar copied successfully' to print the confirmation message.
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
Step 1: Understand the copyArtifacts step
The copyArtifacts step 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 D
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
Step 1: Recall the correct syntax for copyArtifacts
The correct syntax uses named parameters, with projectName specifying the source job.
Step 2: Match the syntax to the options
copyArtifacts(projectName: 'BuildJob') uses copyArtifacts(projectName: 'BuildJob'), which is the correct form in pipeline scripts.
Final Answer:
copyArtifacts(projectName: 'BuildJob') -> Option A
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?