0
0
Jenkinsdevops~15 mins

Custom notification logic in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Custom notification logic
📖 Scenario: You are managing a Jenkins pipeline that builds software projects. You want to send notifications only when the build fails or is unstable, to avoid spamming your team with success messages.
🎯 Goal: Create a Jenkins pipeline script that defines a custom notification logic. It should check the build status and send a notification only if the build result is FAILURE or UNSTABLE.
📋 What You'll Learn
Create a variable buildStatus with the current build result.
Define a list notifyStatuses containing FAILURE and UNSTABLE.
Write a conditional block that sends a notification only if buildStatus is in notifyStatuses.
Print a message indicating whether a notification was sent or skipped.
💡 Why This Matters
🌍 Real World
In real Jenkins pipelines, you often want to notify your team only when something goes wrong or needs attention, not on every successful build.
💼 Career
Understanding custom notification logic helps DevOps engineers reduce noise and improve communication in continuous integration workflows.
Progress0 / 4 steps
1
Set the build status variable
Create a variable called buildStatus and set it to the string FAILURE to simulate a failed build.
Jenkins
Need a hint?

Use buildStatus = 'FAILURE' to assign the string.

2
Define notification statuses
Create a list called notifyStatuses containing the strings 'FAILURE' and 'UNSTABLE'.
Jenkins
Need a hint?

Use square brackets to create the list: ['FAILURE', 'UNSTABLE'].

3
Add custom notification logic
Write an if statement that checks if buildStatus is in notifyStatuses. Inside the block, assign notificationSent the value True. Otherwise, assign notificationSent the value False.
Jenkins
Need a hint?

Use if buildStatus in notifyStatuses: to check membership.

4
Print notification result
Write a print statement that outputs "Notification sent" if notificationSent is True, otherwise print "Notification skipped".
Jenkins
Need a hint?

Use if notificationSent: to decide what to print.