Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How OpenShift GitOps notifications can trigger pipelines

January 17, 2023
Gerald Nunn
Related topics:
CI/CDGitOps
Related products:
Red Hat OpenShift Container Platform

Share:

    Red Hat OpenShift GitOps and Red Hat OpenShift Pipelines are flexible tools for automating continuous integration and continuous development (CI/CD). But until now, it was cumbersome to trigger a pipeline based on specific changes in GitOps, a frequent requirement. In OpenShift GitOps 1.6, notifications is a new feature (in Tech Preview) that can help automate such transitions.

    A typical CI/CD requirement arises when you merge a pull request in GitHub, which triggers an application synchronization in OpenShift GitOps. At that point, you might want certain automated processes to occur, such as running tests. But before OpenShift GitOps 1.6, this process had to be triggered manually via a combination of post-synchronization hooks and Kubernetes jobs, requiring additional effort.

    The new notifications feature

    The new OpenShift GitOps 1.6 notifications feature enables it to send messages based on various events (aka triggers) to various sources such as Slack or email. While notifications are often used to send notices or alerts to messaging systems, such as when an application has failed to sync, the notifications can also drive use cases such as the aforementioned testing.

    This article explores that use case to provide integration or end-to-end (E2E) testing each time an application has been synchronized. Figure 1 shows the overall flow for this use case. We cover the last steps in this sequence. Specifically, we use notifications in OpenShift GitOps to trigger a pipeline by sending a webhook (a REST message in JSON format) to a Pipeline event listener.

    OpenShift Pipelines begin a cycle that leads to automated testing.
    Figure 1: OpenShift Pipelines begin a cycle that leads to automated testing.

    The processes used in this article are based on upstream open source tools invoked by the Red Hat services. Red Hat OpenShift GitOps relies on Argo CD to orchestrate activities, and Red Hat OpenShift Pipelines relies on Tekton to define pipelines. You'll see the upstream tools in the examples in this article.

    How to configure notifications in OpenShift GitOps

    Notifications in GitOps work by defining triggers, templates, and services. The Argo CD Notifications documentation goes into these in great detail. But as a quick introduction, here is a brief description of each:

    • Service: The destination application to which the notification is sent. Out of the box, notifications supports various services, including email, Slack, and GitHub. In our use case, we use the webhook service to send a payload to a Tekton EventListener.
    • Trigger: The event causing the notification will be sent: When an application is created, when it is deleted, when synchronization succeeds, when synchronization fails, etc. You can view the catalog of available triggers in the Argo CD documentation.
    • Template: Format of the message. For a webhook, we define the HTTP method that sends the message (POST in our case) as well as the body of the message, which is a JSON payload.

    Notifications can be enabled to the Argo CD custom resource (CR) through the use of a simple flag in the configuration:

    apiVersion: argoproj.io/v1alpha1
    kind: ArgoCD
    metadata:
      name: argocd
    spec:
      …
      notifications:
        enabled: true
      …

    Enabling the notifications property causes the GitOps Operator to deploy the notifications controller. The operator also creates a default ConfigMap named argocd-notifications-cm and a secret named argocd-notifications-secret. The ConfigMap contains a default configuration with many triggers and templates for common services. The secret can be used to protect sensitive data, such as authentication tokens and passwords.

    This article focuses on the argocd-notifications-cm ConfigMap, because we are not using any authentication in our Tekton EventListener. Authentication isn't needed in this use case because traffic is routed internally in OpenShift between the GitOps and Pipelines services.

    To send a notification to Tekton, you need to define a service, template, subscription as well as update the desired trigger. These are shown in the following subsections.

    Defining the notification's service

    The service is very straightforward because it is just a URL pointing to the Tekton EventListener to which you want to send the message. Call the service server-post-prod and define it as follows:

    service.webhook.server-post-prod: |-
      url: http://el-server-post-prod.product-catalog-cicd.svc:8080

    The URL here lists the service name, el-server-post-prod, the namespace, and product-catalog-cicd. In my case, I am calling this service from a different namespace.

    Defining the notification's template

    With the service in place, you can now define the template that determines how the message is sent and the formatting of the message. Here is the template we are using:

    template.server-post-prod-app-sync: |-
      webhook:
        server-post-prod:
          method: POST
          path: /
          body: |
          {
            {{if eq .app.status.operationState.phase "Running"}} "state": "pending"{{end}}
    	{{if eq .app.status.operationState.phase "Succeeded"}} "state": "success"{{end}}
    	{{if eq .app.status.operationState.phase "Error"}} "state": "error"{{end}}
    	{{if eq .app.status.operationState.phase "Failed"}} "state": "error"{{end}},
    	"description": "ArgoCD",
    	"application": "{{.app.metadata.name}}"
          }
    

    This template is mostly cribbed from the webhook example in the Argo CD documentation. The template includes three items: the application's synchronization state, a description noting that the message is coming from Argo CD, and the name of the application to be synchronized.

    Updating the desired trigger

    In order for the trigger to send the message to the appropriate service with the right template, we need to either define a trigger or add our template to an existing trigger. When notifications is enabled, the operator will create a default configuration map with a number of templates and triggers in it. For the purposes of this example we will simply add our template to the existing on-sync-succeeded trigger as per below, note the new server-post-prod-app-sync that was added which matches our template name.

      trigger.on-sync-succeeded: |-
        - description: Application syncing has succeeded
          send:
          - app-sync-succeeded
          - server-post-prod-app-sync
          when: app.status.operationState.phase in ['Succeeded']

    Defining the notification's subscription

    The final step is to create a subscription to the notification. Although you can create default subscriptions in the ConfigMap, you simply add an annotation to the Argo CD application object that will use the notification. An example of this annotation follows:

    apiVersion: argoproj.io/v1alpha1
    kind: Application
    metadata:
      annotations:
        notifications.argoproj.io/subscribe.on-sync-succeeded.server-post-prod: ""
      name: product-catalog-prod
      namespace: product-catalog-gitops
    spec:
    …

    Receiving notifications in pipelines

    OpenShift Pipelines can receive webhook requests by defining triggers (an overloaded term since we also have triggers in notifications). In this case, you need to define an EventListener, which creates a service to listen on a specific port, as well as a TriggerBinding object, which defines the format for the payload to be received. Let's look at the TriggerBinding first:

    apiVersion: triggers.tekton.dev/v1beta1
    kind: TriggerBinding
    metadata:
      name: argocd-notification
    spec:
      params:
      - name: state
        value: $(body.state)
      - name: application
        value: $(body.application)
      - name: description
        value: $(body.description)

    This TriggerBinding defines the three fields you will receive from the notification, identical to the items in the template you created in the previous section. As the name TriggerBinding implies, this object enables Pipelines to bind elements from the JSON body to specific fields that you can retrieve later.

    Now let us have a look at the EventListener:

    apiVersion: triggers.tekton.dev/v1beta1
    kind: EventListener
    metadata:
      name: server-post-prod
    spec:
      serviceAccountName: pipeline
      triggers:
        - name: server-post-prod-webhook
          interceptors:
            - name: "Only accept sync succeeded"
              ref:
                name: "cel"
              params:
              - name: "filter"
                value: "body.state in ['success']"
          bindings:
            - kind: TriggerBinding
              ref: argocd-notification
          template:
            ref: server-post-prod

    This EventListener specifies the TriggerBinding to use, which you defined previously. The configuration also defines an Interceptor using a Common Expression Language (CEL) filter. This interceptor ensures that the pipeline is triggered only when the application synchronization is a success. Failed or degraded states after synchronization is ignored.

    Testing the flow

    At this point, you can test the flow by simply navigating to your application in OpenShift GitOps and clicking the SYNC button highlighted in Figure 2.

    Click the SYNC button for the application.
    Figure 2: Click the SYNC button for the application.

    The notification should trigger, and your pipeline will run to test the application (Figure 3).

    The topology view shows that a test is running.
    Figure 3: The topology view shows that a test is running.

    The new GitOps feature triggers automated pipelines

    We have seen that using OpenShift GitOps notifications to trigger pipelines to execute after an operation is a straightforward and easy process. Notifications can be beneficial in a large variety of use cases. Although we looked at a specific example involving automated testing, the same basic activities can be useful anytime there is a need to trigger more complex processes in response to changes in OpenShift GitOps.

    Last updated: October 31, 2023

    Related Posts

    • An introduction to cloud-native CI/CD with Red Hat OpenShift Pipelines

    • Modern web applications on OpenShift, Part 4: Openshift Pipelines

    • The present and future of CI/CD with GitOps on Red Hat OpenShift

    Recent Posts

    • Integrate Red Hat AI Inference Server & LangChain in agentic workflows

    • Streamline multi-cloud operations with Ansible and ServiceNow

    • Automate dynamic application security testing with RapiDAST

    • Assessing AI for OpenShift operations: Advanced configurations

    • OpenShift Lightspeed: Assessing AI for OpenShift operations

    What’s up next?

    Getting GitOps e-book card

    Learn how to navigate the complex world of modern container-based software development and distribution with Getting GitOps: A Practical Platform with OpenShift, Argo CD, and Tekton.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue