📖 Scenario: You are working on a Jenkins pipeline that builds a simple project. After the build, you want to save the build output files so they can be accessed later. This is called archiving artifacts.Archiving artifacts helps keep important files safe and shareable after the build finishes.
🎯 Goal: Build a Jenkins pipeline script that archives the output.log file after the build step.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Add a stage named Build that creates a file output.log
Add a stage named Archive that archives the output.log file using archiveArtifacts
💡 Why This Matters
🌍 Real World
Archiving artifacts in Jenkins pipelines is essential for saving build outputs like logs, binaries, or reports. These files can be downloaded later or used in deployment steps.
💼 Career
Understanding artifact archiving is a key skill for DevOps engineers and CI/CD pipeline developers to ensure build results are preserved and traceable.
Progress0 / 4 steps
1
Create the Jenkins pipeline skeleton
Write a Jenkins pipeline script starting with pipeline and agent any. Add a stage named Build with a steps block that creates a file called output.log containing the text Build successful using the shell command sh.
Jenkins
Hint
Use sh 'echo Build successful > output.log' inside the steps block to create the file.
2
Add the Archive stage
Add a new stage named Archive after the Build stage. Inside its steps block, prepare to archive the file output.log using the archiveArtifacts step.
Jenkins
Hint
Add a new stage('Archive') with a steps block inside stages.
3
Archive the output.log file
Inside the Archive stage's steps block, write the command archiveArtifacts artifacts: 'output.log' to archive the file output.log.
Jenkins
Hint
Use archiveArtifacts artifacts: 'output.log' to save the file as an artifact.
4
Print confirmation message
Add a stage named Confirm after Archive. Inside its steps block, write echo 'Artifacts archived successfully.' using the sh step to print a confirmation message.
Jenkins
Hint
Use sh 'echo Artifacts archived successfully.' inside the Confirm stage to print the message.
Practice
(1/5)
1. What is the main purpose of using archiveArtifacts in a Jenkins pipeline?
easy
A. To delete old build files automatically
B. To save build output files for later use or sharing
C. To trigger another build job
D. To send email notifications after a build
Solution
Step 1: Understand the role of archiveArtifacts
This command is used to save files generated by a build so they can be accessed later.
Step 2: Compare with other options
Deleting files, triggering builds, or sending emails are different Jenkins features, not related to archiving artifacts.
Final Answer:
To save build output files for later use or sharing -> Option B
Quick Check:
Archiving = Saving build files [OK]
Hint: Archiving means saving build files for future use [OK]
Common Mistakes:
Confusing archiving with deleting files
Thinking it triggers other jobs
Assuming it sends notifications
2. Which of the following is the correct syntax to archive all .jar files in a Jenkins pipeline script?
easy
A. archiveArtifacts '*.jar'
B. archiveArtifacts '*.jar;'
C. archiveArtifacts '*.jar' {}
D. archiveArtifacts '*.jar'()
Solution
Step 1: Recall the syntax of archiveArtifacts
The correct syntax uses the command followed by the file pattern in quotes without extra characters.
Step 2: Identify incorrect syntax
Options with semicolon, braces, or parentheses after the pattern are invalid in Jenkins pipeline syntax.
Final Answer:
archiveArtifacts '*.jar' -> Option A
Quick Check:
Correct syntax = archiveArtifacts 'pattern' [OK]
Hint: Use single quotes and no extra symbols after pattern [OK]