How to Send Email Notifications from GitLab CI/CD Pipelines

In modern DevOps practices, automating notifications is crucial for keeping teams informed about the status of CI/CD pipelines. Whether it’s a pipeline success, failure, or a password reset notification, sending emails directly from your GitLab pipeline can save time and improve collaboration. In this blog post, we’ll explore two methods to send email notifications from a GitLab CI/CD pipeline, using Python and ssmtp. Both methods are generic, secure, and easy to implement.


Why Send Email Notifications from GitLab Pipelines?

Email notifications are a simple yet effective way to keep stakeholders informed about pipeline events. For example:

  • Notifying team members about pipeline failures.
  • Sending password reset confirmations.
  • Alerting about successful deployments or builds.

By automating these notifications, you can reduce manual effort and ensure timely communication.


Method 1: Sending Emails Using Python in GitLab CI/CD

This method uses Python’s smtplib library to send emails. It’s ideal for teams familiar with Python scripting.

Step-by-Step Implementation

  1. Define the Pipeline Job:
    Create a job in your .gitlab-ci.yml file to handle email notifications. Use an oraclelinux:8 image and install Python and the required libraries.
   notify:
     image: oraclelinux:8
     variables:
       OUTLOOK_EMAIL: "${OUTLOOK_EMAIL}"
       OUTLOOK_PASSWORD: "${OUTLOOK_PASSWORD}"
       NOTIFICATION_EMAIL: "[email protected]"
     script: |
       dnf install -y python3 python3-pip
       pip3 install secure-smtplib
       python3 -c '
       import smtplib
       from email.message import EmailMessage
       import os

       msg = EmailMessage()
       msg.set_content("Pipeline notification")
       msg["Subject"] = "GitLab Pipeline"
       msg["From"] = os.environ.get("OUTLOOK_EMAIL")
       msg["To"] = os.environ.get("NOTIFICATION_EMAIL")

       server = smtplib.SMTP("smtp.office365.com", 587)
       server.set_debuglevel(1)
       server.starttls()
       server.login(os.environ.get("OUTLOOK_EMAIL"), os.environ.get("OUTLOOK_PASSWORD"))
       server.send_message(msg)
       server.quit()'
  1. Set Up Environment Variables:
    Store sensitive information like OUTLOOK_EMAIL and OUTLOOK_PASSWORD in GitLab’s CI/CD settings under Settings > CI/CD > Variables. This ensures security and avoids hardcoding credentials.
  2. Customize the Email Content:
    Modify the msg.set_content() and msg["Subject"] fields to include relevant pipeline details, such as job status, logs, or error messages.
  3. Run the Pipeline:
    Trigger the pipeline, and the email will be sent to the specified recipient.

Method 2: Sending Emails Using ssmtp in GitLab CI/CD

This method uses ssmtp, a lightweight SMTP client, to send emails. It’s perfect for teams looking for a simple, non-Python-based solution.

Step-by-Step Implementation

  1. Define the Pipeline Job:
    Create a job in your .gitlab-ci.yml file to handle email notifications. Use a debian:latest image and install ssmtp.
   stages:
     - reset_password
     - notify_user

   reset_password:
     stage: reset_password
     script:
       - echo "oci iam user change-password ..."
       - echo "NEW_PASSWORD=$generated_password" >> variables.env
     artifacts:
       reports:
         dotenv: variables.env

   notify_user:
     stage: notify_user
     image: debian:latest
     dependencies:
       - reset_password
     before_script:
       - apt-get update && apt-get install -y ssmtp
     script:
       - |
         echo "To: $USER_EMAIL
         From: $OUTLOOK_EMAIL
         Subject: Password Reset Notification
         Your password has been reset to: $NEW_PASSWORD" | ssmtp -v $USER_EMAIL
     variables:
       SSMTP_ROOT: $OUTLOOK_EMAIL
       SSMTP_MAILHUB: smtp.office365.com:587
       SSMTP_AUTHUSER: $OUTLOOK_EMAIL
       SSMTP_AUTHPASS: $OUTLOOK_PASSWORD
       SSMTP_USETLS: "YES"
       SSMTP_USESTARTTLS: "YES"
  1. Set Up Environment Variables:
    Store OUTLOOK_EMAIL, OUTLOOK_PASSWORD, and USER_EMAIL in GitLab’s CI/CD settings.
  2. Customize the Email Content:
    Modify the echo command in the script section to include the desired email content.
  3. Run the Pipeline:
    Trigger the pipeline, and the email will be sent to the specified recipient.

Best Practices for Sending Emails from GitLab Pipelines

  1. Secure Your Credentials:
    Always use GitLab’s CI/CD variables to store sensitive information like email credentials. Avoid hardcoding them in your pipeline configuration.
  2. Use TLS Encryption:
    Ensure your SMTP server uses TLS encryption (as shown in both methods) to protect email content during transmission.
  3. Test Email Notifications:
    Before deploying to production, test your email notifications in a staging environment to ensure they work as expected.
  4. Keep Emails Concise:
    Include only essential information in your emails, such as pipeline status, error messages, or reset passwords.
  5. Monitor Email Delivery:
    Set up logging or monitoring to track email delivery failures and troubleshoot issues promptly.

Conclusion

Sending email notifications from GitLab CI/CD pipelines is a powerful way to keep your team informed and automate communication. Whether you prefer using Python or ssmtp, both methods are easy to implement and highly customizable. By following the steps and best practices outlined in this guide, you can streamline your DevOps workflows and ensure timely notifications for critical pipeline events.

Start integrating email notifications into your GitLab pipelines today and experience the benefits of automated communication!


Leave a Reply