Complete the code to specify the log rotation days in Jenkins pipeline.
pipeline {
agent any
options {
buildDiscarder(logRotator(daysToKeepStr: '[1]'))
}
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}The daysToKeepStr option sets how many days Jenkins keeps the logs before deleting them. Here, 30 days is a common default.
Complete the code to rotate logs based on the number of builds to keep.
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '[1]'))
}
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
}The numToKeepStr option limits how many builds Jenkins keeps logs for. Keeping 5 builds is a common practice to save space.
Fix the error in the log rotation configuration to correctly discard old builds.
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '[1]', daysToKeepStr: '15'))
}
stages {
stage('Test') {
steps {
echo 'Testing...'
}
}
}
}The numToKeepStr must be a positive number. '10' is valid, while '0', negative numbers, or non-numeric strings cause errors.
Fill both blanks to configure Jenkins to keep 7 days of logs and 3 builds.
pipeline {
agent any
options {
buildDiscarder(logRotator(daysToKeepStr: '[1]', numToKeepStr: '[2]'))
}
stages {
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}Setting daysToKeepStr to '7' keeps logs for one week, and numToKeepStr to '3' keeps logs for the last three builds.
Fill all three blanks to configure Jenkins to keep logs for 14 days, 10 builds, and disable artifact retention.
pipeline {
agent any
options {
buildDiscarder(logRotator(daysToKeepStr: '[1]', numToKeepStr: '[2]', artifactDaysToKeepStr: '[3]'))
}
stages {
stage('Package') {
steps {
echo 'Packaging...'
}
}
}
}Setting daysToKeepStr to '14' keeps logs for two weeks, numToKeepStr to '10' keeps the last ten builds, and artifactDaysToKeepStr to '0' disables artifact retention.