Featured image of post Adding Email Notifications to Tekton Pipelines

Adding Email Notifications to Tekton Pipelines

Learn how to add email notifications to your Tekton pipelines using a Finally task.

Introduction

Tekton is a powerful Kubernetes-native CI/CD framework that allows developers to create and manage pipelines efficiently. While Tekton excels at automating build and deployment processes, it’s crucial to keep your team informed about the status of these pipelines. In this tutorial, you’ll learn how to add email notifications to your Tekton pipelines using a Finally task. This approach will enable you to automatically send status updates when a pipeline completes or fails, helping you stay on top of your CI/CD processes.

By the end of this tutorial, you’ll have set up a Tekton pipeline that sends email notifications upon completion, providing your team with immediate feedback on build and deployment processes.

Prerequisites

Before you begin this guide, you should have the following:

  • A Kubernetes cluster with Tekton installed. If you haven’t set this up yet, follow the official Tekton installation guide.
  • Basic knowledge of Tekton concepts, including Tasks, Pipelines, and PipelineRuns.
  • The kubectl command-line tool, configured to communicate with your cluster.
  • An SMTP server for sending emails. You can use services like Gmail, SendGrid, or your own SMTP server.

Step 1 — Creating the Email Notification Task

In this step, you’ll create a Tekton Task that sends an email notification. This Task will use the curl command to send emails via an SMTP server.

Create a file named email-notification-task.yaml with the following content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: send-email-notification
spec:
  params:
    - name: SMTP_SERVER
      type: string
    - name: SMTP_PORT
      type: string
    - name: SMTP_USERNAME
      type: string
    - name: SMTP_PASSWORD
      type: string
    - name: FROM_EMAIL
      type: string
    - name: TO_EMAIL
      type: string
    - name: SUBJECT
      type: string
    - name: BODY
      type: string
  steps:
    - name: send-email
      image: curlimages/curl:7.78.0
      script: |
        #!/bin/sh
        echo "smtp://$(params.SMTP_SERVER):$(params.SMTP_PORT)"
        curl --url "smtp://$(params.SMTP_SERVER):$(params.SMTP_PORT)" \
             --ssl-reqd \
             --mail-from "$(params.FROM_EMAIL)" \
             --mail-rcpt "$(params.TO_EMAIL)" \
             --user "$(params.SMTP_USERNAME):$(params.SMTP_PASSWORD)" \
             -T - <<EOF
        From: $(params.FROM_EMAIL)
        To: $(params.TO_EMAIL)
        Subject: $(params.SUBJECT)

        $(params.BODY)
        EOF        

This Task defines several parameters for the SMTP server configuration and email content. Let’s break down the important parts:

  • The params section defines input parameters that can be customized when the Task is used in a Pipeline.
  • The steps section contains a single step named send-email that uses the curlimages/curl:7.78.0 image.
  • The script field contains a shell script that uses curl to send an email via the specified SMTP server.

Now, apply this Task to your cluster using the following command:

1
kubectl apply -f email-notification-task.yaml

This command creates the Task resource in your Kubernetes cluster, making it available for use in Pipelines.

Step 2 — Creating a Sample Pipeline

Next, you’ll create a sample Pipeline that includes your email notification Task as a Finally task. This Pipeline will simulate a build process and then send an email notification about its status.

Create a file named sample-pipeline.yaml with the following content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: sample-build-pipeline
spec:
  tasks:
    - name: my_task
      taskRef:
        name: my_task  # Replace this with your task(s)
  finally: # This task will always trigger no matter if the pipeline fails or succeeds
    - name: send-notification
      taskRef:
        name: send-email-notification
      params:
        - name: SMTP_SERVER
          value: "smtp.example.com" # Consume this from a secret
        - name: SMTP_PORT
          value: "587" # Consume this from a secret
        - name: SMTP_USERNAME
          value: "your-username" # Consume this from a secret
        - name: SMTP_PASSWORD
          value: "your-password" # Consume this from a secret
        - name: FROM_EMAIL
          value: "notifications@example.com" 
        - name: TO_EMAIL
          value: "team@example.com"
        - name: SUBJECT
          value: "Pipeline $(context.pipelineRun.name) Status"
        - name: BODY
          value: |
            The pipeline $(context.pipelineRun.name) has completed.
            Status: $(tasks.status)
            
            For more details, check the logs at: <your-tekton-dashboard-url>            

This Pipeline includes two main sections:

  1. The tasks section, which contains a placeholder my-task. You should replace this with your task list.
  2. The finally section, which includes the send-email-notification Task you created earlier. This task will run regardless of whether the previous tasks in the Pipeline succeed or fail.

The params section of the send-notification task sets values for the email notification, including SMTP server details and email content. You should replace these placeholder values with your actual SMTP server details and desired email content.

Apply this Pipeline to your cluster using the following command:

1
kubectl apply -f sample-pipeline.yaml

This command creates the Pipeline resource in your Kubernetes cluster.

Step 3 — Running the Pipeline

To run the Pipeline, you need to create a PipelineRun resource. This resource triggers the execution of your Pipeline.

Create a file named pipeline-run.yaml with the following content:

1
2
3
4
5
6
7
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: sample-build-pipelinerun
spec:
  pipelineRef:
    name: sample-build-pipeline

This PipelineRun references the sample-build-pipeline you created in the previous step.

Apply the PipelineRun to start the Pipeline:

1
kubectl apply -f pipeline-run.yaml

This command creates a PipelineRun resource, which triggers the execution of your Pipeline.

Step 4 — Verifying the Email Notification

After applying the PipelineRun, Tekton will execute the Pipeline. Once it completes (either successfully or with a failure), you should receive an email notification at the address specified in the TO_EMAIL parameter.

To check the status of your PipelineRun, use the following command:

1
kubectl get pipelinerun sample-build-pipelinerun

This command will display the status of your PipelineRun. You can use this to verify that the Pipeline has completed before checking for the email notification.

Conclusion

In this tutorial, you’ve learned how to add email notifications to your Tekton pipelines using a Finally task. You’ve created a reusable email notification Task, incorporated it into a Pipeline, and triggered that Pipeline using a PipelineRun.

This setup allows you to keep your team informed about the status of your CI/CD processes automatically. By receiving immediate notifications about pipeline completions or failures, your team can respond quickly to any issues that arise in your build and deployment processes.

Here are some ways you can extend and improve this setup:

  1. Use Kubernetes Secrets to store sensitive information like SMTP credentials, enhancing the security of your setup.
  2. Customize the email content to include more detailed information about the pipeline run, such as specific task results or error messages.
    • You can also use Mime types to send HTML emails with rich content, this way you can include links, images, and anything you can do with HTML.
  3. Implement conditional notifications based on the pipeline status (e.g., only notify on failures) to reduce noise and focus on critical issues.
  4. Explore other notification methods like Slack or Microsoft Teams integrations to fit your team’s communication preferences.

Remember to adjust the SMTP server settings and email addresses in the sample code to match your specific environment. With these notifications in place, you’ll have a more responsive and informed CI/CD process.

For more information on Tekton and its features, check out the official Tekton documentation.

Built with Hugo
Theme Stack designed by Jimmy