Tekton: Concepts of Pipelines (Part 2)
This article is part of Series World of Tekton and discusses the concepts of Tekton Pipelines.
This is Part 2 of the series, you can find the introduction here
Before diving into the concepts, let’s take an example so that we can relate..
Let’s say we want to build a CI/CD pipeline to deploy the an application on a K8s cluster so the pipeline would consist of following tasks:
- Fetch the code
- Test the code
- Unit Test
- Build Test
- Lint Test - Build an Image
- Build Image & Push to Docker Registry - Deploy the Image
- Deploy the application on K8s using the build image
Let’s look into the Pipeline Concepts.
> Task <
A Task
is a collection of Steps
that you define and arrange in a specific order of execution as part of your continuous integration flow.
So, in our example there could be multiple Tasks, one for each operation -> one could fetch the code, one could run the test, one could build the image and so on.
Q. Can we write just one task for the whole pipeline?
Yes, but Task should be written keeping in mind that it could be reused. So, when we have a Task to fetch code from a git repository, same task could be used in any other pipeline where we want to fetch the code.
You can find list of reusable Tasks in the tektoncd/catalog which you can view at hub.tekton.dev. You can write your own or use from the collection.
Let’s write a Task
which prints hello world
.
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello
spec:
steps:
- name: say-hello
image: registry.access.redhat.com/ubi8/ubi
command:
- /bin/bash
args: ['-c', 'echo Hello World']
In the above example, you can see a resource with kind
as Task
. The task would do one thing which is print Hello World
. It is similar to how we create a pod to echo Hello World.
If you apply this on a cluster, what do you think what will happen?
Nothing, Task is just like a blue print where we tell what should happen if someone is using the Task defined.
So, to run an instance of the Task we create a TaskRun
.
> TaskRun <
A TaskRun
allows you to instantiate and execute a Task
on-cluster.
A Task
specifies one or more Steps
that execute container images and each container image performs a specific piece of build work. A TaskRun
executes the Steps
in the Task
in the order they are specified until all Steps
have executed successfully or a failure occurs.
To run the above Task
, we create the following TaskRun
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
generateName: hello-
spec:
taskRef:
name: hello
Now, when you create TaskRun
, Tekton Pipelines will read the definition from Task
: hello
which we created earlier and perform all the steps defined inside it in a pod.
NOTE: There are many features such as passing params from TaskRun to Task while creating an instance, or specifying a Timeout, and other. To know more you can refer the documentation here.
Now, that we know what is a Task, let’s look into Pipeline.
> Pipeline <
A Pipeline
is a collection of Tasks
that you define and arrange in a specific order of execution as part of your continuous integration flow.
For our example, a pipeline will help use to define an order in which we want to run the different Tasks. To run the tests in our pipeline, we need to run the task to fetch the code first, then only run the tests. A Pipeline
allows us to define the order for this.
Let’s update our hello
Task to accept params
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello
spec:
params:
- name: text
type: string
steps:
- name: say-hello-to
image: registry.access.redhat.com/ubi8/ubi
command:
- /bin/bash
args: ['-c', 'echo Hello $(params.text)!']
Above Task now accepts a name which will be printed as Hello <name>
.
Let’s see a Pipelines which uses the hello Task.
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: hello
spec:
tasks:
- name: hello-k8s
taskRef:
name: hello
params:
- name: text
value: "Kubernetes"
- name: hello-tekton
taskRef:
name: hello
params:
- name: text
value: "Tekton"
runAfter:
- hello-k8s
Notice in the Pipeline that we are using hello
Task twice, once we are passing Kubernetes
and in second we are passing Tekton
. We have used runAfter
which tells Tekton Pipelines to run the hello-tekton
after completing hello-k8s
.
If you apply the above Pipeline
on the cluster, nothing would happen as it is just like a blue print similar to Task where we tell what should happen if someone is using the Pipeline.
So, to run an instance of Pipeline
we need PipelineRun
.
> PipelineRun <
A PipelineRun
allows you to instantiate and execute a Pipeline
on-cluster.
A Pipeline
specifies one or more Tasks
in the desired order of execution. A PipelineRun
executes the Tasks
in the Pipeline
in the order they are specified until all Tasks
have executed successfully or a failure occurs.
To run the above Pipeline
create the following PipelineRun
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
generateName: hello-run-
spec:
pipelineRef:
name: hello
NOTE: There are many features such as passing params from PipelineRun to Pipeline while creating an instance, or specifying a Timeout, and other. To know more you can refer the documentation here.
> Workspace
<
Workspace
allows the Task step and Pipeline task to share a common filesystem. This helps Pipelines to pick up changes form one task to another task in the Pipeline workflow.
In our example, we have multiple Task such as build test, unit test , lint test and other which are executed on the code. So, we clone the code in a workspace and use the same workspace in all Task so that they can access it to run their Task.
We will see how to use workspace in our demo article further.
In this we have discussed the basic resource of Tekton Pipelines. Now we know how to create a Pipeline. In next part, let’s see how we can trigger the Pipeline by an external event.
And then we will build an end to end Pipeline for a Web App.
NEXT:
PREV: