Introduction To Tekton Tasks And TaskRuns

Puneet Punamiya
3 min readApr 6, 2021

Tekton is an OpenSource project under CD foundation which provides a framework for creating CI/CD systems in cloud native way. In simple terms, you can define your entire CI/CD pipeline as Kubernetes resources.

Tekton Pipelines have its core reusable component known as Task which can be easily shared. Before understanding Task let’s understand what Steps are

  • Steps is a reference to container image
  • Runs command or script in a container on specific input and produces specific output
  • To add Steps to a Task you define a steps field (required) containing a list of desired Steps
  • The order in which the Steps appear in this list is the order in which they will execute.
  • Kubernetes container spec
    * Env vars
    * Volumes
    * Config maps
    * Secrets
Block Manifest for Steps

Next let’s look what a Task is and how it is defined and structured

  • Collection of steps to be executed in a specific order
  • Task defines a unit of work to be executed
  • A Task executes as a Pod on a cluster
  • The steps defined in the container run in the task pod
  • Task is independent of Pipeline and can be executed
Block Manifest For Tasks

Now let’s create a simple hello-world Task which echos hello-world

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: echo-hello-world
spec:
steps:
- name: echo
image: ubuntu
command:
- echo
args:
- "hello world"

To install a Tekton resoure first tekton-pipelines should be installed. You can find installation docs here

Let’s install this task using kubectl

╰─ kubectl apply -f hello-task.yaml                                                                                                                           
task.tekton.dev/echo-hello-world configured

You can also list the task using tkn list command. To know more about tkn see here

╰─ tkn task ls                    
NAME DESCRIPTION AGE
buildah Buildah task builds... 2 days ago
echo-hello-world 2 days ago

To run this Task we can either create a Taskrun or we can use tkn start command to start the task. First let’s run this task using a TaskRun.

Before that let’s knkow what a TaskRun is:

  • TaskRun allows to instantiate and execute a Task with specific inputs and execution parameters
  • A Task specifies one or more Steps that execute container images and each container image performs a specific piece of build work.
  • TaskRun executes the Steps in the Task in the order they are specified until all Steps have executed successfully or a failure occurs.
Block Manifest For TaskRun

Manifest for TaskRun for echo-hello-world-run will look as follows:

apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
name: echo-hello-world-run
spec:
taskRef:
name: echo-hello-world

Let’s apply the TaskRun using kubectl

╰─ k apply -f hello-task-run.yaml
taskrun.tekton.dev/echo-hello-world-run created

You can list the TaskRun using tkn

╰─ tkn taskrun list                                  
NAME STARTED DURATION STATUS
echo-hello-world-run 5 minutes ago 7 seconds Succeeded

You can also see the logs of the TaskRun using tkn and you will see the desired output

╰─ tkn taskrun logs echo-hello-world-run 
[echo] hello world

You can find more about Tekton Tasks here

For any queries please reach out to me on Github (PuneetPunamiya)

--

--