Multiple Step Tekon Task and Script
In many cases, your tasks will have more than one step. This is why the steps field is a list. You can specify multiple steps using various images.
create a file with the name multiple-step-task.yaml with below content
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: multiple-steps
spec:
steps:
- name: first
image: ubuntu
command:
- /bin/bash
- -c
- echo "First step"
- name: second
image: centos
command:
- /bin/bash
- -c
- echo "Second step"
For the second task, we can use a different image. In this case, we are using ubuntu image in first step and centos image in second task.
now we are ready to apply this task to our cluster and look at the task's output with multiple steps.
learning-ocean:~ gaurav$ kubectl apply -f multiple-step-task.yaml
task.tekton.dev/multiple-steps created
learning-ocean:~ gaurav$
Now that the task is created, now we can list it using below command.
learning-ocean:~ gaurav$ tkn task ls
NAME DESCRIPTION AGE
hello 2 weeks ago
multiple-steps 1 minute ago
learning-ocean:~ gaurav$
Go ahead and run this task to see the outputs of each echo statement:
learning-ocean:~ gaurav$ tkn task start multiple-steps --showlog
TaskRun started: multiple-steps-run-2fgnx
Waiting for logs to be available...
[first] First step
[second] Second step
learning-ocean:~ gaurav$
The log prefix is color-coded to make it easier to distinguish between the various steps. If you have a terminal that does not support ANSI color codes, you can disable this feature with the --no-color argument:
learning-ocean:~ gaurav$ tkn task start multiple-steps --showlog --no-color
Apart from the colors, now using your terminal defaults, the results should be the same.
Using scripts
Sometimes, we want to perform an operation that is more complex in your steps' command field. To do so, you can use a script instead of the command you used previously. You can only have a single command or a single script, but not both.
To do so, start with a new YAML file called task-script.yaml with below content
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: task-script
spec:
steps:
- name: step-with-script
image: ubuntu
script: |
#!/usr/bin/env bash
echo "this is multi line"
echo "shell script"
echo "what we want to execute in single step"
echo "Learning-Ocean Happy Learning"
Here, instead of using a command, a script is used, followed by a pipe (|) symbol. All of the lines at the same indentation level are then executed as part of the script.The script's first line used a shebang (#!) to tell the operating system which interpreter to use to parse the script that followed. this is a Bash script that starts with echo statements.
You can then apply this task to your cluster and run it with the Tekton CLI tool to see the output:
Gauravs-MacBook-Air:~ gaurav$ kubectl apply -f task-script.yaml
task.tekton.dev/task-script created
Gauravs-MacBook-Air:~ gaurav$
let's start the logs and see the logs using the below commands.
learning-ocean:~ gaurav$ tkn task start task-script --showlog
TaskRun started: task-script-run-tpwsb
Waiting for logs to be available...
[step-with-script] this is multi line
[step-with-script] shell script
[step-with-script] what we want to execute in single step
[step-with-script] Learning-Ocean Happy Learning