Getting Started with Tekton Pipelines

Julian Augusto Ruiz Cuervo
Globant
Published in
6 min readJul 25, 2023
Photo by Stephen Dawson on Unsplash

In the DevOps world, there are many licensed and paid CI/CD tools, like Azure DevOps, GitLab CI/CD, Bitbucket, and many others, that can be used to create our pipelines to help us improve the quality of our applications and to deliver them faster; there are other open source tools that we can use to achieve the same goals offered by those paid tools.

This will be the first of a series of articles talking about Tekton, an open source and cloud-native CI/CD tool that can be used to create different kinds of pipelines to automate, verify and deploy our applications in a Kubernetes cluster.

Tekton pipeline flow

Tekton features

As well as with any other CI/CD platform, Tekton offers some benefits, which we mention below:

  • Pipeline-centric: The pipeline-centric nature of Tekton allows you to define and manage CI/CD pipelines with ease. Pipelines provide a clear structure for organizing and sequencing the steps involved in your software delivery process.
  • Kubernetes-native: Being built on Kubernetes, Tekton leverages the power and scalability of the Kubernetes ecosystem. It seamlessly integrates with Kubernetes clusters, enabling you to take advantage of container orchestration and management capabilities.
  • Declarative and version-controlled: Tekton’s declarative approach enables you to define pipelines and tasks using YAML files, making it easier to maintain and reproduce your CI/CD configurations. Additionally, version control support ensures that changes to your pipelines are tracked and managed effectively.
  • Reusable components: The ability to create and reuse tasks in Tekton greatly enhances efficiency and maintainability. By encapsulating specific actions or operations within tasks, you can build modular pipelines and promote code reuse across projects.
  • Extensible and pluggable: Tekton’s extensible architecture allows you to customize and extend its functionality according to your specific requirements. You can develop custom tasks, add new resources, and integrate with external tools, empowering you to tailor Tekton to your unique CI/CD workflows.

These features collectively make Tekton a robust and flexible CI/CD framework, providing the necessary tools and capabilities to streamline and automate your software delivery pipeline.

Tekton’s resources

These are Tekton’s custom resources:

  • Pipeline: Set of tasks, running sequentially and/or in parallel.
  • Task: A set of steps, such as cloning repo, running unit tests, building image and deploying image.
  • PipelineRun: The pipeline instance to run.
  • TaskRun: The task instance to run with specific input, output and/or execution parameters.

In this article, we are only going to showcase the pipeline resource.

What is a Tekton Pipeline?

A Tekton pipeline is one of Tekton’s resources that allows us to build, test and deploy applications in a Kubernetes cluster on a cloud provider or on-premises. Tekton uses Custom Resource Definitions (CRDs) to extend Kubernetes functionality and add additional features.

Installing Tekton pipelines

To properly install the custom resources of Tekton pipelines, we need to have a Kubernetes cluster, be it one with a cloud provider or a local installation like minikube or k3s. We will assume that you already have access to one.

To install the custom resources, run the following command:

kubectl apply --filename \
https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml

This command will install the Tekton pipelines resources inside a new namespace called tekton-pipelines.

To monitor the installation run:

kubectl get pods --namespace tekton-pipelines --watch

Creating Your First Task

We are going to create our first task to run a simple task outside a pipeline. Open your favorite editor and paste the following content:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: welcome
spec:
steps:
- name: echo
image: alpine
script: |
#!/bin/sh
echo "Welcome to tekton pipelines"

Apply your changes:

kubectl apply -f welcome-task.yaml

The output will show you that the task was created successfully:

task.tekton.dev/welcome created

Now, create a taskrun object which will run the task previously created:

apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
name: welcome-task-run
spec:
taskRef:
name: welcome

Apply your changes:

kubectl apply -f welcome-run.yaml

The output will show you that the taskrun was created:

taskrun.tekton.dev/welcome-task-run created

To check the task status, run the following command:

kubectl get taskrun welcome-task-run

The output will show you the task status:

NAME                     SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
welcome-task-run True Succeeded 83s 74s

If you want to check the task log, run the following command:

kubectl logs --selector=tekton.dev/taskRun=welcome-task-run

The output will show you the task logs:

Welcome to tekton pipelines

Adding parameters to your task

Parameters allow you to customize tasks as needed. Edit the previous task file and add a new parameter. The task yaml file should look like this:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: welcome
spec:
params:
- name: user
description: user name.
default: john.doe
type: string
steps:
- name: echo
image: alpine
script: |
#!/bin/sh
echo "Welcome $(params.user) to tekton pipelines"

Apply your changes:

kubectl apply -f welcome-task.yaml

The output will show you that the task was modified successfully:

task.tekton.dev/welcome configured

Change your taskrun object by editing only the name parameter and run the following command:

kubectl apply -f welcome-run-2.yaml

The output will show you that the taskrun was created:

taskrun.tekton.dev/welcome-task-run-2 created

Check the task logs by running the following command:

kubectl logs --selector=tekton.dev/taskRun=welcome-task-run-2

The output will show you the task logs:

Welcome john.doe to tekton pipelines

Creating and Running Your First Pipeline

Now that we have explained the task concept, we can continue to learn how to create the pipeline resource. A pipeline can run different tasks to automate manual operations. First, we are going to create a new task to have a total of two tasks in one single pipeline.

Open your favorite editor and paste the following content:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: testing
spec:
params:
- name: user
type: string
steps:
- name: echo-testing
image: ubuntu
script: |
#!/bin/bash
echo "User '$(params.user)' is running some tekton tests!"

Apply your changes:

kubectl apply -f testing-task.yaml

The output will show you that the task was created successfully:

task.tekton.dev/testing created

Now we are going to create our pipeline object referring to all the tasks that we want to run. Open your favorite editor and paste the following content:

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: demo-pipeline
spec:
params:
- name: username
type: string
tasks:
- name: welcome
taskRef:
name: welcome
params:
- name: user
value: $(params.username)
- name: testing
runAfter:
- welcome
taskRef:
name: testing
params:
- name: user
value: $(params.username)

Apply your changes:

kubectl apply -f pipeline.yaml

The output will show you that the pipeline was created:

pipeline.tekton.dev/demo-pipeline created

To execute the pipeline that was created in the previous step, we have to create a pipelinerun resource, adding all the required parameters for the pipeline. Open your favorite editor and paste the following content:

apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: demo-pipeline-run
spec:
pipelineRef:
name: demo-pipeline
params:
- name: username
value: "Tekton"

Apply your changes:

kubectl apply -f pipeline-run.yaml

The output will show you that the pipelinerun was created:

pipelinerun.tekton.dev/demo-pipeline-run created

To check the pipeline status, run the following command:

kubectl get pipelinerun

The output will show you the pipeline status:

NAME                      SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
demo-pipeline-run True Succeeded 26m 26m

Also, if you want to check the task status, run the following command:

kubectl get taskrun

The output will show you the task status:

NAME                                     SUCCEEDED    REASON      STARTTIME   COMPLETIONTIME
demo-pipeline-run-testing True Succeeded 37s 26s
demo-pipeline-run-welcome True Succeeded 44s 37s

Now you have run your first pipeline!

Tasks running mode: sequential or parallel

In the previous pipeline example, we ran the tasks in sequential order, meaning that the second task ran after the first task finished its execution. We can parametrize this behavior depending on our requirements. To edit this configuration, go to the task section in the pipeline resource and remove the runAfter section.

If you don’t add this parameter, your task will run in parallel, otherwise your task will run sequentially after the specified task.

Conclusions

In this article, we presented an introduction to Tekton and explained how to install it and run a basic pipeline. When it comes to working with pipelines, Tekton is an excellent choice. This open source and cloud-native CI/CD tool offers a multitude of advantages, including ease of installation, licensing, pricing, access to the Tekton Hub, a thriving community, and scalability.

Alternatively, if you opt for other tools, it is essential to perform benchmarking to compare their features and evaluate the associated benefits and drawbacks.

References

--

--