Skip to main content

My First Task

Let's go straight to our hello world! Like everything in kubernetes it's a manifest so let's develop our manifest for a hello world task.

Everything stays as always in kubernetes...

# --- STANDARD BLOCK
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello-task
spec: # Here we will define what we want
# ---
cat <<EOF > taskHW.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello-task
spec:
steps: # Our list of steps
- name: hello
image: ubuntu
script: >
echo "Hello World"
EOF

kubectl create -f taskHW.yaml
task.tekton.dev/hello-task created

kubectl get tasks
NAME AGE
hello-task 4s

kubectl get tasks -n tekton-pipelines
No resources found in tekton-pipelines namespace.

kubectl describe task hello-task
Name: hello-task
Namespace: default
Labels: <none>
Annotations: <none>
API Version: tekton.dev/v1
Kind: Task
Metadata:
Creation Timestamp: 2024-07-31T11:24:23Z
Generation: 1
Resource Version: 1995385
UID: 8f1b52d9-b2a1-4df4-8b7a-1d3ee1c898c1
Spec:
Steps:
Compute Resources:
Image: ubuntu
Name: hello
Script: echo "Hello World"

Events: <none>

As we didn't define the namespace it was created in default and we can observe that Tekton separates these resources by namespace.

This is just the definition of a task but to actually run a task you need a taskRun.

cat <<EOF > taskRunHW.yaml
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
# In this case we are declaring a prefix for it to generate the name
generateName: hello-task-
spec:
taskRef:
name: hello-task # Which task will be executed
EOF

kubectl create -f taskRunHW.yaml
taskrun.tekton.dev/hello-task-46z7v created

kubectl get taskruns
NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
hello-task-46z7v True Succeeded 49s 31s

kubectl logs hello-task-46z7v-pod
Defaulted container "step-hello" out of: step-hello, prepare (init), place-scripts (init)
Hello World

Let's create with one more step to test what we have.

cat <<EOF > taskHW2.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello-task2
spec:
steps:
- name: hello
image: ubuntu
script: >
echo "Hello World"
- name: world
image: ubuntu
script: >
echo "World hello"
EOF

kubectl create -f taskHW2.yaml
task.tekton.dev/hello-task2 created

kubectl get tasks
hello-task 19m
hello-task2 3s

cat <<EOF > taskRunHW2.yaml
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
generateName: hello-task2-
spec:
taskRef:
name: hello-task2
EOF

kubectl create -f taskRunHW2.yaml
taskrun.tekton.dev/hello-task2-fg8gh created

kubectl logs hello-task2-fg8gh-pod
Defaulted container "step-hello" out of: step-hello, step-world, prepare (init), place-scripts (init)
Hello World

# Notice in the output above that it brought the log of the first step because we didn't pass which step.
# Passing the step we want the log from
kubectl logs hello-task2-fg8gh-pod step-hello
Hello World

kubectl logs hello-task2-fg8gh-pod step-world
World hello

The Tekton cli actually comes as a facilitator that applies filters to the kubectl command.

tkn task list
NAME DESCRIPTION AGE
hello-task 1 hour ago
hello-task2 1 hour ago

kubectl get task
NAME AGE
hello-task 103m
hello-task2 83m

tkn task describe hello-task
Name: hello-task
Namespace: default

🦶 Steps

∙ hello

🗂 Taskruns

NAME STARTED DURATION STATUS
hello-pipeline-q5c4f-hello 14 minutes ago 9s Succeeded
hello-task-46z7v 1 hour ago 18s Succeeded

kubectl describe task hello-task
Name: hello-task
Namespace: default
Labels: <none>
Annotations: <none>
API Version: tekton.dev/v1
Kind: Task
Metadata:
Creation Timestamp: 2024-07-31T11:24:23Z
Generation: 1
Resource Version: 1995385
UID: 8f1b52d9-b2a1-4df4-8b7a-1d3ee1c898c1
Spec:
Steps:
Compute Resources:
Image: ubuntu
Name: hello
Script: echo "Hello World"

Events: <none>