Tekton triggers with GitHub integration
Continuous Integration (CI) is a cornerstone of modern software development, ensuring that code changes are tested and integrated into the main codebase frequently and reliably. Tekton Triggers, coupled with GitHub integration, offers a powerful solution for automating CI workflows in Kubernetes environments. In this tutorial, we’ll walk through the process of setting up Tekton Triggers with GitHub integration, enabling seamless automation of CI tasks in response to GitHub events. Let’s dive in to the tutorial.
Prerequisites
Before you begin, ensure that you have the following:
- A Kubernetes cluster with Tekton installed
- Kubectl
- Tekton Pipelines
- A GitHub account
Install Triggers
Now that we have the prerequisites in place, let’s install Tekton Triggers. To do this, we’ll use the following command:
1 | ## Install the triggers |
This command installs the Tekton Triggers and Interceptors, which are essential for setting up the CI workflow. Once the installation is complete, we can proceed to the next step. Now you can monitor the installation by running the following command:
1 | kubectl get pods --namespace tekton-pipelines --watch |
Set up RBAC
Event Listeners need a proper Kubernetes Service Account for role access control (RBAC), so the first step is creating the RBAC objects. You can create Service Account, Role, RoleBinding, ClusterRole and ClusterRoleBinding using the following YAML:
1 | apiVersion: v1 |
The RBAC yaml can be found here. Once you have created the RBAC objects, you can proceed to the next step.
Create an Event Listener
The next step is to create an Event Listener, which will listen for GitHub events and trigger the CI workflow. You can create an Event Listener using the following YAML:
1 | apiVersion: triggers.tekton.dev/v1beta1 |
Create a Trigger Binding
The next step is to create a Trigger Binding, which will define the parameters for the GitHub event. You can create a Trigger Binding using the following YAML:
1 | apiVersion: triggers.tekton.dev/v1beta1 |
In here we are using the head_commit.id
as the gitrevision
parameter. You can use any other parameter as per your requirement.
Create a Trigger Template
The next step is to create a Trigger Template, which will define the CI workflow to be triggered by the GitHub event. You can create a Trigger Template using the following YAML:
1 | apiVersion: triggers.tekton.dev/v1beta1 |
TriggerTemplate is similar to PipelineRun
, except that the latter is triggered manually as we’ve seen in the previous posts. Then, the TriggerTemplate “sends” the needed params to the PipelineRef, along with the definition of Workspace volume claim templates.
Exposing the Event Listener
Now we need to expose the Event Listener to the outside world so that GitHub can send events to it. But before it you can check the Event Listener by running the following command:
1 | kubectl get el |
To expose the Event Listener, we can use the kubectl port-forward
command to forward traffic from a local port to the Event Listener’s service port. You can do this using the following command:
1 | kubectl port-forward svc/el-github-listener 8080:8080 |
Great, our host is receiving in the port 8080 (localhost:8080), but how do we tunnel Github to our port using some service in the cloud? We can use a service like ngrok to expose our local port to the internet. You can download and install ngrok from here. Once you have ngrok installed, you can expose your local port to the internet using the following command:
1 | ngrok http http://localhost:8080 |
Now you can see the public URL that ngrok has generated for you. You can use this URL to configure the GitHub webhook.
Configure the GitHub Webhook
In the repository settings, go to Webhooks and then you need to fillout the following details:
- Payload URL: The public URL generated by ngrok
- Content type: application/json
- Secret: Leave it empty
- Which events would you like to trigger this webhook?: Just the push event
Conclusion
In this tutorial, we’ve walked through the process of setting up Tekton Triggers with GitHub integration, enabling seamless automation of CI tasks in response to GitHub events. We’ve installed Tekton Triggers, set up RBAC, created an Event Listener, Trigger Binding, and Trigger Template, and exposed the Event Listener to the outside world. We’ve also configured the GitHub webhook to send events to the Event Listener. With this setup in place, we can now automate CI workflows in response to GitHub events, ensuring that code changes are tested and integrated into the main codebase frequently and reliably.
You can find the all the related scripts and code for this tutorial from here. If you have any issue regarding this tutorial, mention your issue in the comment section or reach me through my E-mail.