What are Tekton Workspaces and how are they used?
Quick Answer
Tekton Workspaces are declared volumes that provide shared filesystem access to steps within a Task or across Tasks in a Pipeline. They abstract the underlying storage mechanism, allowing the same Task definition to work with PersistentVolumeClaims, ConfigMaps, Secrets, or emptyDir volumes depending on what the TaskRun or PipelineRun provides.
Detailed Answer
Think of a workspace as a shared locker at a gym. The locker specification (workspace declaration) says you need a place to store your gear, but it does not specify whether the locker is a small one, a large one, or a temporary bag check. When you actually arrive (create a Run), you choose which locker to use (PVC, emptyDir, ConfigMap, or Secret). This abstraction lets the same workout routine (Task) work regardless of which locker you are assigned.
A Tekton Workspace is a volume declaration in a Task or Pipeline spec that provides a shared filesystem mount point for steps. In the Task definition, you declare the workspace with a name and an optional description, and then reference it in steps using the expression $(workspaces.<name>.path). When creating a TaskRun or PipelineRun, you bind the workspace to an actual volume source. The most common binding is a PersistentVolumeClaim (PVC), which provides durable storage that persists across steps and even across Tasks in a Pipeline. For example, the order-processing-service team declares a workspace called source in their build Task, and when they create a TaskRun, they bind it to a PVC that contains the cloned Git repository.
The workspace abstraction is critical because it decouples the Task definition from the storage implementation. The same git-clone Task can be used with a PVC when running in a production Pipeline (where data must persist across multiple Tasks), with an emptyDir when running a quick one-off test (where data can be discarded after the pod terminates), or with a ConfigMap when you need to inject static configuration files into the Task's filesystem. Tekton also supports volumeClaimTemplate, which automatically creates a temporary PVC for the duration of a PipelineRun and deletes it when the run completes. This is particularly useful for CI builds where you need persistent storage during the pipeline but do not want leftover PVCs cluttering the cluster.
In production workflows, the inventory-sync team uses workspaces to pass data through their deployment Pipeline. The first Task clones the repository into a PVC-backed workspace. The second Task reads the source code from that same workspace, builds a Docker image, and writes the image digest to a file in the workspace. The third Task reads the digest file and deploys the application. Without workspaces, each Task would run in its own pod with an isolated filesystem, and there would be no way to pass the built artifacts from the build Task to the deploy Task. The workspace backed by a PVC provides the shared filesystem bridge between these separate pods. Additionally, the user-auth-service team mounts a Kubernetes Secret as a read-only workspace to inject private SSH keys needed for cloning private Git repositories, keeping the credentials out of the Task definition.
A common beginner mistake is using emptyDir workspaces in a Pipeline with multiple Tasks. An emptyDir volume is scoped to a single pod, so data written by one Task in an emptyDir workspace is not visible to the next Task, which runs in a different pod. For multi-Task Pipelines, you must use a PVC or volumeClaimTemplate. Another gotcha is the access mode of the PVC: if you want multiple Tasks to access the same workspace concurrently (parallel tasks), the PVC must support ReadWriteMany (RWX) access mode, which not all storage providers support. Check your cluster's StorageClass capabilities before designing parallel workflows that share a workspace.