Tekton Debugging: How to List Files in a Task
In Tekton, your Tasks run in short-lived containers. You join Tasks together into a CI/CD pipeline. Running tasks in containers is all rather nice and “cloud-native”, but how are you meant to debug them?
A Task in Tekton runs in a container. It’s a good use for containers because the container pops up, executes commands in an isolated environment, and then disappears quickly.
But what if you want to run any sort of debug command, to see what files were created or used by your Tasks? Then you can add debug commands into your Pipeline.
Find out what’s inside the box
When a Pipeline doesn’t seem to be working, one of the first things I try to do is find out what’s inside the container. (To see how the magic trick works, you’ve got to look inside the box, right?)
You can add custom commands into a Task to aid with debugging
Normally in a Linux terminal you would use a command like find
or ls
for this.
In Tekton, you don’t get a terminal. But you can add extra steps to a Task
, to run commands.
So you can add the find
or ls
command as another step
to the beginning or the end of a Task that you refer to in your Pipeline.
Here’s one example of this. Using the alpine
Docker image to run a shell (sh
) and run the find
command:
apiVersion: tekton.dev/v1beta1
kind: Task
...
steps:
# Add an extra step to list the contents of /workspace
# You might add this before or after another Task
- name: dump-directory
image: alpine
command:
- /bin/sh
args:
- '-c'
- |
set -ex
find /workspace
resources: {}
This step becomes useful when you look at the logs from your pipeline run.
To see the logs
I added the task above, and then I ran my Pipeline. Notice a new TaskRun:
$ kubectl get taskruns
NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
hello-java-jlt4t-compile-and-build-image-qhk2j False Failed 20s 8s
hello-java-jlt4t-fetch-repository-fdkjc True Succeeded 29s 20s
Then I get the logs for the TaskRun. It’s the TaskRun with a status of Failed
:
$ kubectl logs $(kubectl get taskrun hello-java-jlt4t-compile-and-build-image-qhk2j -ojsonpath={.status.podName}) \
--all-containers
And I see this in the logs:
+ find /workspace
/workspace
/workspace/source
/workspace/source/hello-java-sigstore
/workspace/.cache
/workspace/.m2
/workspace/.docker
/workspace/.docker/config.json
That’s the output from the find
command. It might not be obvious to you, but those directories are empty! I was expecting my code to be in that workspace
directory. So I’ve clearly got some bug-fixing to do. 😪
But at least now I’m armed with information which will help me debug the problem.
And so the cloud-native journey continues….