TaskRuns

Use the TaskRun resource object to create and run on-cluster processes to
completion.

To create a TaskRun, you must first create a Task which
specifies one or more container images that you have implemented to perform and
complete a task.

A TaskRun runs until all steps have completed or until a failure occurs.



Syntax

To define a configuration file for a TaskRun resource, you can specify the
following fields:

  • Required:
    • [apiVersion][kubernetes-overview] - Specifies the API version, for example
      tekton.dev/v1alpha1.
    • [kind][kubernetes-overview] - Specify the TaskRun resource object.
    • [metadata][kubernetes-overview] - Specifies data to uniquely identify the
      TaskRun resource object, for example a name.
    • [spec][kubernetes-overview] - Specifies the configuration information for
      your TaskRun resource object.
      • taskRef or taskSpec - Specifies the details of
        the Task you want to run
      • trigger - Provides data about what created this TaskRun. Can be
        manual if you are creating this manually, or has a value of
        PipelineRun if it is created as part of a
        PipelineRun
  • Optional:

[kubernetes-overview]:
https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/#required-fields

Specifying a task

Since a TaskRun is an invocation of a Task, you must specify
what Task to invoke.

You can do this by providing a reference to an existing Task:

  1. spec:
  2. taskRef:
  3. name: read-task

Or you can embed the spec of the Task directly in the TaskRun:

  1. spec:
  2. taskSpec:
  3. inputs:
  4. resources:
  5. - name: workspace
  6. type: git
  7. steps:
  8. - name: build-and-push
  9. image: gcr.io/kaniko-project/executor
  10. command:
  11. - /kaniko/executor
  12. args:
  13. - --destination=gcr.io/my-project/gohelloworld

Input parameters

If a Task has parameters, you can specify values for
them using the input section:

  1. spec:
  2. inputs:
  3. params:
  4. - name: flags
  5. value: -someflag

If a parameter does not have a default value, it must be specified.

Providing resources

If a Task requires input resources or
output resources, they must be provided to run the
Task.

They can be provided via references to existing
PipelineResources:

  1. spec:
  2. inputs:
  3. resources:
  4. - name: workspace
  5. resourceRef:
  6. name: java-git-resource

Or by embedding the specs of the resources directly:

  1. spec:
  2. inputs:
  3. resources:
  4. - name: workspace
  5. resourceSpec:
  6. type: git
  7. params:
  8. - name: url
  9. value: https://github.com/pivotal-nader-ziada/gohelloworld

Service Account

Specifies the name of a ServiceAccount resource object. Use the
serviceAccount field to run your Task with the privileges of the specified
service account. If no serviceAccount field is specified, your Task runs
using the
default service account
that is in the
namespace
of the TaskRun resource object.

For examples and more information about specifying service accounts, see the
ServiceAccount reference topic.

Overriding where resources are copied from

When specifying input and output PipelineResources, you can optionally specify
paths for each resource. paths will be used by TaskRun as the resource’s
new source paths i.e., copy the resource from specified list of paths. TaskRun
expects the folder and contents to be already present in specified paths.
paths feature could be used to provide extra files or altered version of
existing resource before execution of steps.

Output resource includes name and reference to pipeline resource and optionally
paths. paths will be used by TaskRun as the resource’s new destination
paths i.e., copy the resource entirely to specified paths. TaskRun will be
responsible for creating required directories and copying contents over. paths
feature could be used to inspect the results of taskrun after execution of
steps.

paths feature for input and output resource is heavily used to pass same
version of resources across tasks in context of pipelinerun.

In the following example, task and taskrun are defined with input resource,
output resource and step which builds war artifact. After execution of
taskrun(volume-taskrun), custom volume will have entire resource
java-git-resource (including the war artifact) copied to the destination path
/custom/workspace/.

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: Task
  3. metadata:
  4. name: volume-task
  5. namespace: default
  6. spec:
  7. inputs:
  8. resources:
  9. - name: workspace
  10. type: git
  11. steps:
  12. - name: build-war
  13. image: objectuser/run-java-jar #https://hub.docker.com/r/objectuser/run-java-jar/
  14. command: jar
  15. args: ["-cvf", "projectname.war", "*"]
  16. volumeMounts:
  17. - name: custom-volume
  18. mountPath: /custom
  1. apiVersion: tekton.dev/v1alpha1
  2. kind: TaskRun
  3. metadata:
  4. name: volume-taskrun
  5. namespace: default
  6. spec:
  7. taskRef:
  8. name: volume-task
  9. inputs:
  10. resources:
  11. - name: workspace
  12. resourceRef:
  13. name: java-git-resource
  14. outputs:
  15. resources:
  16. - name: workspace
  17. paths:
  18. - /custom/workspace/
  19. resourceRef:
  20. name: java-git-resource
  21. volumes:
  22. - name: custom-volume
  23. emptyDir: {}

Cancelling a TaskRun

In order to cancel a running task (TaskRun), you need to update its spec to
mark it as cancelled. Running Pods will be deleted.

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: TaskRun
  3. metadata:
  4. name: go-example-git
  5. spec:
  6. # […]
  7. status: "TaskRunCancelled"

Examples

Example TaskRun

To run a Task, create a new TaskRun which defines all inputs, outputs that
the Task needs to run. Below is an example where Task read-task is run by
creating read-repo-run. Task read-task has git input resource and TaskRun
read-repo-run includes reference to go-example-git.

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: TaskRun
  3. metadata:
  4. name: read-repo-run
  5. spec:
  6. taskRef:
  7. name: read-task
  8. trigger:
  9. type: manual
  10. inputs:
  11. resources:
  12. - name: workspace
  13. resourceRef:
  14. name: go-example-git
  15. ---
  16. apiVersion: tekton.dev/v1alpha1
  17. kind: PipelineResource
  18. metadata:
  19. name: go-example-git
  20. spec:
  21. type: git
  22. params:
  23. - name: url
  24. value: https://github.com/pivotal-nader-ziada/gohelloworld
  25. ---
  26. apiVersion: tekton.dev/v1alpha1
  27. kind: Task
  28. metadata:
  29. name: read-task
  30. spec:
  31. inputs:
  32. resources:
  33. - name: workspace
  34. type: git
  35. steps:
  36. - name: readme
  37. image: ubuntu
  38. command:
  39. - /bin/bash
  40. args:
  41. - "cat README.md"

Example with embedded specs

Another way of running a Task is embedding the TaskSpec in the taskRun yaml.
This can be useful for “one-shot” style runs, or debugging. TaskRun resource can
include either Task reference or TaskSpec but not both. Below is an example
where build-push-task-run-2 includes TaskSpec and no reference to Task.

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: PipelineResource
  3. metadata:
  4. name: go-example-git
  5. spec:
  6. type: git
  7. params:
  8. - name: url
  9. value: https://github.com/pivotal-nader-ziada/gohelloworld
  10. ---
  11. apiVersion: tekton.dev/v1alpha1
  12. kind: TaskRun
  13. metadata:
  14. name: build-push-task-run-2
  15. spec:
  16. trigger:
  17. type: manual
  18. inputs:
  19. resources:
  20. - name: workspace
  21. resourceRef:
  22. name: go-example-git
  23. taskSpec:
  24. inputs:
  25. resources:
  26. - name: workspace
  27. type: git
  28. steps:
  29. - name: build-and-push
  30. image: gcr.io/kaniko-project/executor
  31. command:
  32. - /kaniko/executor
  33. args:
  34. - --destination=gcr.io/my-project/gohelloworld

Input and output resources can also be embedded without creating Pipeline
Resources. TaskRun resource can include either a Pipeline Resource reference or
a Pipeline Resource Spec but not both. Below is an example where Git Pipeline
Resource Spec is provided as input for TaskRun read-repo.

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: TaskRun
  3. metadata:
  4. name: read-repo
  5. spec:
  6. taskRef:
  7. name: read-task
  8. trigger:
  9. type: manual
  10. inputs:
  11. resources:
  12. - name: workspace
  13. resourceSpec:
  14. type: git
  15. params:
  16. - name: url
  17. value: https://github.com/pivotal-nader-ziada/gohelloworld

Note: TaskRun can embed both TaskSpec and resource spec at the same time.
The TaskRun will also serve as a record of the history of the invocations of
the Task.

Example Task Reuse

For the sake of illustrating re-use, here are several example
TaskRuns (including referenced
PipelineResources) instantiating the
Task (dockerfile-build-and-push) in the Task example docs.

Build mchmarny/rester-tester:

  1. # The PipelineResource
  2. metadata:
  3. name: mchmarny-repo
  4. spec:
  5. type: git
  6. params:
  7. - name: url
  8. value: https://github.com/mchmarny/rester-tester.git
  1. # The TaskRun
  2. spec:
  3. taskRef:
  4. name: dockerfile-build-and-push
  5. inputs:
  6. resources:
  7. - name: workspace
  8. resourceRef:
  9. name: mchmarny-repo
  10. params:
  11. - name: IMAGE
  12. value: gcr.io/my-project/rester-tester

Build googlecloudplatform/cloud-builder‘s wget builder:

  1. # The PipelineResource
  2. metadata:
  3. name: cloud-builder-repo
  4. spec:
  5. type: git
  6. params:
  7. - name: url
  8. value: https://github.com/googlecloudplatform/cloud-builders.git
  1. # The TaskRun
  2. spec:
  3. taskRef:
  4. name: dockerfile-build-and-push
  5. inputs:
  6. resources:
  7. - name: workspace
  8. resourceRef:
  9. name: cloud-builder-repo
  10. params:
  11. - name: IMAGE
  12. value: gcr.io/my-project/wget
  13. # Optional override to specify the subdirectory containing the Dockerfile
  14. - name: DIRECTORY
  15. value: /workspace/wget

Build googlecloudplatform/cloud-builder‘s docker builder with 17.06.1:

  1. # The PipelineResource
  2. metadata:
  3. name: cloud-builder-repo
  4. spec:
  5. type: git
  6. params:
  7. - name: url
  8. value: https://github.com/googlecloudplatform/cloud-builders.git
  1. # The TaskRun
  2. spec:
  3. taskRef:
  4. name: dockerfile-build-and-push
  5. inputs:
  6. resources:
  7. - name: workspace
  8. resourceRef:
  9. name: cloud-builder-repo
  10. params:
  11. - name: IMAGE
  12. value: gcr.io/my-project/docker
  13. # Optional overrides
  14. - name: DIRECTORY
  15. value: /workspace/docker
  16. - name: DOCKERFILE_NAME
  17. value: Dockerfile-17.06.1

Using a ServiceAccount

Specifying a ServiceAccount to access a private git repository:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: TaskRun
  3. metadata:
  4. name: test-task-with-serviceaccount-git-ssh
  5. spec:
  6. serviceAccount: test-task-robot-git-ssh
  7. inputs:
  8. resources:
  9. - name: workspace
  10. type: git
  11. steps:
  12. - name: config
  13. image: ubuntu
  14. command: ["/bin/bash"]
  15. args: ["-c", "cat README.md"]

Where serviceAccount: test-build-robot-git-ssh references the following
ServiceAccount:

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: test-task-robot-git-ssh
  5. secrets:
  6. - name: test-git-ssh

And name: test-git-ssh, references the following Secret:

  1. apiVersion: v1
  2. kind: Secret
  3. metadata:
  4. name: test-git-ssh
  5. annotations:
  6. tekton.dev/git-0: github.com
  7. type: kubernetes.io/ssh-auth
  8. data:
  9. # Generated by:
  10. # cat id_rsa | base64 -w 0
  11. ssh-privatekey: LS0tLS1CRUdJTiBSU0EgUFJJVk.....[example]
  12. # Generated by:
  13. # ssh-keyscan github.com | base64 -w 0
  14. known_hosts: Z2l0aHViLmNvbSBzc2g.....[example]

Specifies the name of a ServiceAccount resource object. Use the
serviceAccount field to run your Task with the privileges of the specified
service account. If no serviceAccount field is specified, your Task runs
using the
default service account
that is in the
namespace
of the Task resource object.

For examples and more information about specifying service accounts, see the
ServiceAccount reference topic.


Except as otherwise noted, the content of this page is licensed under the
Creative Commons Attribution 4.0 License,
and code samples are licensed under the
Apache 2.0 License.