0
0
Jenkinsdevops~30 mins

Log management and rotation in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Log Management and Rotation with Jenkins
📖 Scenario: You are managing a Jenkins server that generates build logs. Over time, these logs can fill up disk space. To keep the server healthy, you want to set up log rotation to keep only recent logs and delete older ones automatically.
🎯 Goal: Set up a Jenkins job with log rotation configured to keep only the last 5 builds' logs. You will create a Jenkins pipeline script that defines this log rotation policy.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Add a options block inside the pipeline
Configure buildDiscarder with logRotator to keep 5 builds
Print a message in the pipeline to confirm the job runs
💡 Why This Matters
🌍 Real World
Jenkins servers generate many build logs that can fill disk space. Managing logs by rotating and deleting old ones keeps the server healthy and fast.
💼 Career
DevOps engineers and build managers often configure Jenkins jobs to automatically clean up old logs, ensuring stable and efficient CI/CD pipelines.
Progress0 / 4 steps
1
Create a basic Jenkins pipeline script
Create a Jenkins pipeline script with a pipeline block and an empty agent any section.
Jenkins
Need a hint?

Start with the basic Jenkins pipeline structure including pipeline and agent any.

2
Add log rotation configuration
Inside the pipeline block, add an options block that uses buildDiscarder(logRotator(numToKeepStr: '5')) to keep only the last 5 builds' logs.
Jenkins
Need a hint?

Use the options block to add buildDiscarder with logRotator and set numToKeepStr to '5'.

3
Add a simple stage to print a message
Add a stages block with one stage named 'Build'. Inside it, add a steps block that runs echo 'Build started'.
Jenkins
Need a hint?

Add a stages block with a stage named 'Build' that prints 'Build started' using echo.

4
Print confirmation message after pipeline runs
Add a post block inside the pipeline that runs always and prints echo 'Pipeline completed'.
Jenkins
Need a hint?

Use a post block with always to print a confirmation message after the pipeline finishes.