Skip to main content
Jenkins Pipeline
CHAPTER 15

Monitoring and Logging Jenkins

Updated: May 15, 2026
20 min read

# CHAPTER 15

Monitoring and Logging Jenkins

1. Introduction

Automation is only valuable if you trust it. When a developer pushes code, they should be able to close their laptop and walk away, confident that the system will handle the deployment or alert them if something breaks. If developers constantly have to log into the Jenkins dashboard to check if their build passed, you haven't built automation; you've built a chore. In this chapter, we will focus on Observability. We will learn how to configure proactive notifications (like Slack and Email), monitor the performance of the Jenkins server itself, and manage the massive amount of log data generated by CI/CD pipelines.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the necessity of proactive pipeline notifications.
  • Configure Jenkins to send alerts to external platforms (Slack/Teams).
  • Utilize the post block to trigger conditional notifications.
  • Monitor Jenkins system performance and disk space.
  • Understand the importance of Log Rotation and Build History management.

3. Beginner-Friendly Explanation

Imagine a home security system.
  • The Bad System (No Monitoring): The alarm goes off, but it only flashes a light inside the house. If you are at work, you have no idea a burglar is in your home. You only find out when you return.
  • The Good System (Proactive Alerts): The alarm goes off, and it instantly sends a text message to your phone and calls the police.
Jenkins must be the good system. If a deployment fails and the website goes down, Jenkins must instantly message the engineering team. Waiting for a customer to complain is a DevOps failure.

4. Configuring Proactive Notifications

The most common integration for developer notifications is Slack (or Microsoft Teams).
  1. 1. The Plugin: Install the "Slack Notification Plugin" in Jenkins.
  1. 2. The Webhook: Create an incoming webhook integration in your Slack workspace. It will give you a unique URL/Token.
  1. 3. Global Config: In *Manage Jenkins* -> *System*, enter your Slack Token.
  1. 4. The Pipeline: Use the slackSend command in your Jenkinsfile.

5. Mini Project: Configure Build Notifications

Let's update our pipeline to send beautifully formatted Slack messages based on the build outcome. We will use the post block we learned in Chapter 6.

Step-by-Step Pipeline Concept:

groovy
12345678910111213141516171819202122232425262728293031323334
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
            }
        }
    }
    // The Post block triggers notifications based on the result
    post {
        success {
            // Sends a green message to the #deployments channel
            slackSend(
                color: 'good', 
                message: "✅ SUCCESS: Build ${env.BUILD_NUMBER} of ${env.JOB_NAME} completed successfully. \nView details: ${env.BUILD_URL}"
            )
        }
        failure {
            // Sends a red message to the #alerts channel, pinging the channel
            slackSend(
                color: 'danger', 
                message: "🚨 FAILED: Build ${env.BUILD_NUMBER} of ${env.JOB_NAME} failed! \nCheck logs immediately: ${env.BUILD_URL}"
            )
        }
        fixed {
            // Only runs if the PREVIOUS build failed, but THIS build succeeded (The bug is fixed)
            slackSend(
                color: 'good', 
                message: "🛠️ RESOLVED: The build is back to normal!"
            )
        }
    }
}

6. Managing Disk Space and Log Rotation

Jenkins saves a complete log of every build (the Console Output) and the final artifact (the .zip or .jar file). If a project runs 50 times a day, and saves a 100MB file each time, Jenkins will quickly consume terabytes of hard drive space. When the hard drive hits 100%, Jenkins crashes completely.

The Defense: Discard Old Builds You must instruct Jenkins to throw away old data. In a Declarative Pipeline, you define this in the options block:

groovy
12345678
pipeline {
    agent any
    options {
        // Only keep the last 10 builds, and delete anything older than 7 days
        buildDiscarder(logRotator(numToKeepStr: '10', daysToKeepStr: '7'))
    }
    // ... stages
}

7. Real-World Scenarios

A data analytics team had a nightly Jenkins job that processed large CSV files. The job usually took 10 minutes. One night, an external API went offline, causing the script to hang infinitely. Because they had no performance monitoring or timeouts configured, the job ran for 72 hours, consuming 100% of the Jenkins server's CPU and blocking all other company deployments. The DevOps engineer implemented a pipeline timeout block (options { timeout(time: 1, unit: 'HOURS') }) and configured a Slack alert to trigger if the timeout was reached, instantly resolving the blind spot.

8. Best Practices

  • Don't Spam: If you send a Slack message for every successful build, developers will get "Alert Fatigue" and start ignoring the channel. The channel becomes white noise. Only send alerts for Failures, Recoveries (the fixed condition), and major Production Deployments.

9. Security Recommendations

  • Log Forging/Sanitization: If your pipeline prints user-submitted data to the Console Output, an attacker might inject malicious formatting characters to try and exploit the Jenkins log viewer. Always sanitize variables before echoing them, or avoid logging unvalidated external data entirely.

10. Troubleshooting Tips

  • Email Configuration: If mail steps are failing, the issue is almost always in *Manage Jenkins* -> *System* under the "Extended E-mail Notification" section. You must configure the correct SMTP server (like SendGrid or AWS SES) and provide the correct authentication credentials for Jenkins to send outgoing mail.

11. Exercises

  1. 1. Why is relying on developers to manually check the Jenkins dashboard considered a failure of the DevOps philosophy?
  1. 2. Explain the purpose of the buildDiscarder option in a pipeline. What catastrophic server failure does it prevent?

12. FAQs

Q: Can Jenkins monitor the health of my live website? A: While you *could* write a Jenkins script that curls your website every 5 minutes, Jenkins is an automation server, not a monitoring tool. You should use dedicated monitoring tools like Datadog, New Relic, or Prometheus to monitor live applications.

13. Interview Questions

  • Q: A critical production deployment fails at 2:00 AM, but the engineering team is not aware of it until 9:00 AM. Architect a comprehensive notification strategy within a Jenkinsfile to ensure immediate incident response.
  • Q: The Jenkins Controller frequently crashes due to running out of disk space. Describe your strategy for implementing log rotation, artifact management, and cleanup stages to maintain server stability.

14. Summary

In Chapter 15, we gave Jenkins a voice. We recognized that silent automation breeds anxiety, so we integrated proactive notification systems like Slack to provide immediate, contextual feedback to the engineering team. We utilized the post block to conditionally alert on failures and recoveries, minimizing alert fatigue. Finally, we addressed the physical limitations of the server, implementing aggressive log rotation and artifact discarding policies to guarantee long-term system stability.

15. Next Chapter Recommendation

As our company grows, we have 50 different microservices, which means 50 different Jenkinsfiles. Copying and pasting the Slack notification code into 50 files is terrible practice. How do we share code between pipelines? Proceed to Chapter 16: Jenkins Shared Libraries and Reusable Pipelines.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·