📖 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
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
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
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
Hint
Use a post block with always to print a confirmation message after the pipeline finishes.
Practice
(1/5)
1. What is the main purpose of log rotation in Jenkins?
easy
A. To move old logs and save disk space
B. To delete all Jenkins jobs automatically
C. To increase Jenkins build speed
D. To backup Jenkins configuration files
Solution
Step 1: Understand what logs do in Jenkins
Logs record Jenkins activity and can grow large over time.
Step 2: Purpose of log rotation
Log rotation moves or archives old logs to save disk space and keep the system tidy.
Final Answer:
To move old logs and save disk space -> Option A
Quick Check:
Log rotation = save space [OK]
Hint: Log rotation helps keep disk space free by moving old logs [OK]
Common Mistakes:
Thinking log rotation deletes Jenkins jobs
Confusing log rotation with backup
Believing log rotation speeds up builds
2. Which of the following is the correct syntax to configure log rotation using copytruncate in a Linux logrotate config for Jenkins logs?
easy
A. /var/log/jenkins/jenkins.log {
truncatecopy
weekly
rotate 5
}
B. /var/log/jenkins/jenkins.log {
copytruncate
monthly
rotate seven
}
C. /var/log/jenkins/jenkins.log {
copytruncate
daily
rotate 7
}
D. /var/log/jenkins/jenkins.log {
copytruncate
daily
rotate seven
}
Solution
Step 1: Identify correct logrotate syntax
The copytruncate option is valid and used to copy then truncate the log file without stopping Jenkins.
Step 2: Check rotation frequency and count
Using daily and rotate 7 is correct syntax for daily rotation keeping 7 files.
Final Answer:
Correct syntax with copytruncate, daily rotation, and rotate 7 -> Option C
Quick Check:
copytruncate + daily + rotate 7 = correct [OK]
Hint: copytruncate keeps Jenkins running during log rotation [OK]
Common Mistakes:
Using 'truncatecopy' instead of 'copytruncate'
Writing 'rotate seven' instead of 'rotate 7'
Using invalid rotation frequency like 'monthly' with wrong number
3. Given this logrotate config snippet for Jenkins logs: