📖 Scenario: You are managing a Jenkins pipeline that builds software projects. Each build produces artifacts like compiled files or reports. To save disk space, you want to keep only a limited number of recent artifacts and discard older ones automatically.
🎯 Goal: Set up a Jenkins pipeline script that defines artifact retention policies to keep only the last 3 builds' artifacts and discard older ones.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Add a post section with always condition
Use archiveArtifacts to save build artifacts
Use buildDiscarder with logRotator to keep only last 3 builds
💡 Why This Matters
🌍 Real World
In real projects, managing disk space by cleaning old build artifacts is important to keep Jenkins servers healthy and fast.
💼 Career
Understanding artifact retention policies is essential for DevOps engineers to maintain CI/CD pipelines efficiently.
Progress0 / 4 steps
1
Create basic Jenkins pipeline
Create a Jenkins pipeline script with a pipeline block and a stages section containing one stage named Build. Inside the stage, add a steps block with a shell command echo "Building project".
Jenkins
Hint
Use pipeline and stage blocks. Use sh to run shell commands.
2
Add artifact archiving step
Inside the Build stage's steps block, add an archiveArtifacts step to archive all files matching **/*.jar.
Jenkins
Hint
Use archiveArtifacts with the artifacts parameter set to '**/*.jar'.
3
Add artifact retention policy
Add a options block inside the pipeline block. Inside options, add a buildDiscarder with a logRotator that keeps only the last 3 builds by setting numToKeepStr: '3'.
Jenkins
Hint
Use options block with buildDiscarder(logRotator(numToKeepStr: '3')).
4
Print confirmation message
Add a post section inside the pipeline block with an always condition. Inside always, add a script block that prints "Artifact retention policy applied" using echo.
Jenkins
Hint
Use post { always { script { echo 'Artifact retention policy applied' } } } inside the pipeline.
Practice
(1/5)
1. What is the main purpose of artifact retention policies in Jenkins?
easy
A. To manage disk space by deleting old build files
B. To speed up the build process by caching dependencies
C. To increase the number of builds stored indefinitely
D. To automatically trigger builds on code changes
Solution
Step 1: Understand artifact retention policies
Artifact retention policies control how long build files are kept to avoid using too much disk space.
Step 2: Identify the main goal
The main goal is to delete old build files to free up disk space.
Final Answer:
To manage disk space by deleting old build files -> Option A
Quick Check:
Artifact retention = disk space management [OK]
Hint: Retention policies delete old builds to save space [OK]
Common Mistakes:
Confusing retention with build triggering
Thinking retention speeds up builds
Assuming retention keeps all builds forever
2. Which Jenkins pipeline syntax correctly sets a retention policy to keep only the last 5 builds?
easy
A. archiveArtifacts(numToKeepStr: '5')
B. buildDiscarder(keepLast: 5)
C. buildDiscarder(logRotator(numToKeepStr: '5'))
D. archiveArtifacts(logRotator(numToKeepStr: '5'))
Solution
Step 1: Identify correct syntax for retention
Retention uses buildDiscarder with logRotator to specify how many builds to keep.
But artifacts are not deleted after 10 days. What is the likely problem?
medium
A. logRotator does not support artifact retention
B. The buildDiscarder option must be inside stages
C. You must use archiveArtifacts to delete old artifacts
D. The parameter name should be artifactDaysToKeepStr, not artifactDaysToKeep
Solution
Step 1: Check logRotator parameter names
logRotator parameters for strings end with 'Str', like artifactDaysToKeepStr.
Step 2: Identify the syntax issue
Missing 'Str' means the parameter is not recognized, so default retention applies and artifacts aren't deleted after 10 days.
Final Answer:
The parameter name should be artifactDaysToKeepStr, not artifactDaysToKeep -> Option D
Quick Check:
logRotator params end with 'Str' [OK]
Hint: logRotator params like artifactDaysToKeepStr end with Str [OK]
Common Mistakes:
Using wrong parameter names
Expecting logRotator to delete artifacts
Placing buildDiscarder outside options block
5. You want to keep artifacts only for the last 4 builds and delete older artifacts automatically, while keeping older builds. Which Jenkins pipeline snippet correctly applies this retention policy?
D. steps { archiveArtifacts artifacts: '**/*', keepOnlyLast: 4 }
Solution
Step 1: Understand roles for artifact-specific retention
To keep artifacts from only the last 4 builds while retaining builds, use artifactNumToKeepStr.
Step 2: Check option correctness
options { buildDiscarder(logRotator(artifactNumToKeepStr: '4')) }: buildDiscarder(logRotator(artifactNumToKeepStr: '4')) keeps artifacts from last 4 builds, deletes older artifacts, keeps all builds. C deletes old builds too. B/D use invalid params.
Final Answer:
options { buildDiscarder(logRotator(artifactNumToKeepStr: '4')) } -> Option B
Quick Check:
artifactNumToKeepStr for artifacts only [OK]
Hint: artifactNumToKeepStr keeps artifacts from last N builds [OK]
Common Mistakes:
Using invalid parameters like retentionDays or keepOnlyLast
Trying to control retention only via archiveArtifacts
Misplacing buildDiscarder outside options block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.