Tekton: Triggering the Pipeline (Part 5)

Shivam Mukhade
3 min readFeb 18, 2022

--

This article is part of Series World of Tekton and illustrate how to automate triggering of Pipeline based on GitHub Events.

This is Part 5 of the series, you can find the introduction here

In previous part “Tekton: Build a Pipeline (Part 4)”, we built and executed a Pipeline. In this we will continue and setup an automated trigger for the pipeline based on the Repository Event.

Make sure you have forked https://github.com/sm43/news-demo repository as you will need admin access to setup triggers.

We can install the trigger resources using the below command

kubectl apply -f trigger/

This will install following resources

  • TriggerBinding
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerBinding
metadata:
name: news-demo-pr-binding
namespace: news-demo
spec:
params:
- name: gitrevision
value: $(body.head_commit.id)
- name: gitrepositoryurl
value: $(body.repository.url)
- name: createdat
value: $(body.repository.created_at)

here we define the fields we wanna to fetch from the event payload and use them in the pipelineRun.

gitrepositoryurl -> repo of the url
gitrevision -> commit on which the event occurred and which will be clone
createdat -> time at which event occurred, we will use this as tag for the image, so each time a new event occurred we push a new image with new tag

  • TriggerTemplate
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
name: news-demo-template
namespace: news-demo
spec:
params:
- name: gitrevision
- name: gitrepositoryurl
- name: createdat
resourcetemplates:
- apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
generateName: news-demo-deploy-
namespace: news-demo
spec:
serviceAccountName: news-demo
params:
- name: REPO
value: $(tt.params.gitrepositoryurl)
- name: REVISION
value: $(tt.params.gitrevision)
- name: IMAGE
value: "quay.io/sm43/news-demo"
- name: TAG
value: $(tt.params.createdat)
- name: NAMESPACE
value: "news-demo"
pipelineRef:
name: news-demo-deploy
workspaces:
- name: shared-workspace
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

Here we define the PipelineRun which must be created if the event occurs. Notice that we have used the fields defined in TriggerBinding as params to the PipelineRun.

  • EventListener
apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:
name: news-demo-listener
namespace: news-demo
spec:
triggers:
- name: news-demo-listener
interceptors:
- ref:
name: "github"
params:
- name: "eventTypes"
value: ["push"]
bindings:
- ref: news-demo-pr-binding
template:
ref: news-demo-template

Here we define a EventListener which will listen for events from our repo and we have added a interceptor which will filter out push events from all types of events coming from the repository.

So, when a pull request is created or a new commit is pushed the EventListener will receive a payload from which it checks if it is a push event and if it is then triggers the pipelineRun.

You can check the EventListener status using

tkn el ls -n news-demo

or

kubectl get eventlisteners -n news-demo

# Configuring EventListener with Repository

First, we need to expose the EventListener Service. It is created when we create EventListener resource.

You can check it using following command

kubectl get service -n news-demo

It must be starting with prefix el-.

If you are using OpenShift then you can expose service using oc command

oc expose svc el-news-demo-listener -n news-demo

and the fetch the route using

echo "http://$(kubectl get routes el-news-demo-listener  -n news-demo -ojsonpath='{.spec.host}')"

NOTE: If you are using any other kubernetes cluster, then you need expose using Ingress and use it for configuring webhook.

configuring triggers

Now that we have everything setup, It’s show time… !!!!

We have configured our EventListener for push events so let’s push a commit and see whether a pipelineRun is created or not..

We can see a pipelineRun was created when we pushed a commit

But going to cluster and checking the status of pipelineRun whether it is successful or it failed? That isn’t too productive right? we need the status to be reported on our repostiory … when we create pull request!
And we have PipelinesAsCode for the same…

Reference:
https://tekton.dev
https://github.com/tektoncd/pipeline
https://github.com/tektoncd/triggers

--

--

Shivam Mukhade
Shivam Mukhade

Written by Shivam Mukhade

Open-Source | Software Engineer | Kubernetes | OpenShift | Tekton | PipelinesAsCode

No responses yet