0
0
Jenkinsdevops~30 mins

Artifact retention policies in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Artifact retention policies
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use post { always { script { echo 'Artifact retention policy applied' } } } inside the pipeline.