Hello World Tutorial

Welcome to the Tekton Pipeline tutorial!

This tutorial will walk you through creating and running some simple
Task, Pipeline and running them by creating
TaskRuns and PipelineRuns.

For more details on using Pipelines, see our usage docs.

Note: This tutorial can be run on a local workstation

Task

The main objective of Tekton Pipelines is to run your Task individually or as a
part of a Pipeline. Every task runs as a Pod on your Kubernetes cluster with
each step as its own container.

A Task defines the work that needs to be executed, for example the
following is a simple task that will echo hello world:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: Task
  3. metadata:
  4. name: echo-hello-world
  5. spec:
  6. steps:
  7. - name: echo
  8. image: ubuntu
  9. command:
  10. - echo
  11. args:
  12. - "hello world"

The steps are a series of commands to be sequentially executed by the task.

A TaskRun runs the Task you defined. Here is a simple example
of a TaskRun you can use to execute your task:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: TaskRun
  3. metadata:
  4. name: echo-hello-world-task-run
  5. spec:
  6. taskRef:
  7. name: echo-hello-world
  8. trigger:
  9. type: manual

To apply the yaml files use the following command:

  1. kubectl apply -f <name-of-file.yaml>

To see the output of the TaskRun, use the following command:

  1. kubectl get taskruns/echo-hello-world-task-run -o yaml

You will get an output similar to the following:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: TaskRun
  3. metadata:
  4. creationTimestamp: 2018-12-11T15:49:13Z
  5. generation: 1
  6. name: echo-hello-world-task-run
  7. namespace: default
  8. resourceVersion: "6706789"
  9. selfLink: /apis/tekton.dev/v1alpha1/namespaces/default/taskruns/echo-hello-world-task-run
  10. uid: 4e96e9c6-fd5c-11e8-9129-42010a8a0fdc
  11. spec:
  12. generation: 1
  13. inputs: {}
  14. outputs: {}
  15. taskRef:
  16. name: echo-hello-world
  17. taskSpec: null
  18. trigger:
  19. type: manual
  20. status:
  21. conditions:
  22. - lastTransitionTime: 2018-12-11T15:50:09Z
  23. status: "True"
  24. type: Succeeded
  25. podName: echo-hello-world-task-run-pod-85ca51
  26. startTime: 2018-12-11T15:49:39Z
  27. steps:
  28. - terminated:
  29. containerID: docker://fcfe4a004...6729d6d2ad53faff41
  30. exitCode: 0
  31. finishedAt: 2018-12-11T15:50:01Z
  32. reason: Completed
  33. startedAt: 2018-12-11T15:50:01Z
  34. - terminated:
  35. containerID: docker://fe86fc5f7...eb429697b44ce4a5b
  36. exitCode: 0
  37. finishedAt: 2018-12-11T15:50:02Z
  38. reason: Completed
  39. startedAt: 2018-12-11T15:50:02Z

The status of type Succeeded = True shows the task ran successfully.

Task Inputs and Outputs

In more common scenarios, a Task needs multiple steps with input and output
resources to process. For example a Task could fetch source code from a GitHub
repository and build a Docker image from it.

PipelinesResources are used to define the artifacts that can
be passed in and out of a task. There are a few system defined resource types
ready to use, and the following are two examples of the resources commonly
needed.

The git resource represents a git repository with
a specific revision:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: PipelineResource
  3. metadata:
  4. name: skaffold-git
  5. spec:
  6. type: git
  7. params:
  8. - name: revision
  9. value: master
  10. - name: url
  11. value: https://github.com/GoogleContainerTools/skaffold #configure: change if you want to build something else, perhaps from your own local GitLab

The image resource represents the image to be
built by the task:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: PipelineResource
  3. metadata:
  4. name: skaffold-image-leeroy-web
  5. spec:
  6. type: image
  7. params:
  8. - name: url
  9. value: gcr.io/<use your project>/leeroy-web #configure: replace with where the image should go: perhaps your local registry or Dockerhub with a secret and configured service account

The following is a Task with inputs and outputs. The input resource is a
GitHub repository and the output is the image produced from that source. The
args of the task command support templating so that the definition of task is
constant and the value of parameters can change in runtime.

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: Task
  3. metadata:
  4. name: build-docker-image-from-git-source
  5. spec:
  6. inputs:
  7. resources:
  8. - name: docker-source
  9. type: git
  10. params:
  11. - name: pathToDockerFile
  12. description: The path to the dockerfile to build
  13. default: /workspace/docker-source/Dockerfile
  14. - name: pathToContext
  15. description:
  16. The build context used by Kaniko
  17. (https://github.com/GoogleContainerTools/kaniko#kaniko-build-contexts)
  18. default: /workspace/docker-source
  19. outputs:
  20. resources:
  21. - name: builtImage
  22. type: image
  23. steps:
  24. - name: build-and-push
  25. image: gcr.io/kaniko-project/executor
  26. command:
  27. - /kaniko/executor
  28. args:
  29. - --dockerfile=${inputs.params.pathToDockerFile}
  30. - --destination=${outputs.resources.builtImage.url}
  31. - --context=${inputs.params.pathToContext}

TaskRun binds the inputs and outputs to already defined PipelineResources,
sets values to the parameters used for templating in addition to executing the
task steps.

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: TaskRun
  3. metadata:
  4. name: build-docker-image-from-git-source-task-run
  5. spec:
  6. taskRef:
  7. name: build-docker-image-from-git-source
  8. trigger:
  9. type: manual
  10. inputs:
  11. resources:
  12. - name: docker-source
  13. resourceRef:
  14. name: skaffold-git
  15. params:
  16. - name: pathToDockerFile
  17. value: Dockerfile
  18. - name: pathToContext
  19. value: /workspace/docker-source/examples/microservices/leeroy-web #configure: may change according to your source
  20. outputs:
  21. resources:
  22. - name: builtImage
  23. resourceRef:
  24. name: skaffold-image-leeroy-web

To apply the yaml files use the following command, you need to apply the two
resources, the task and taskrun.

  1. kubectl apply -f <name-of-file.yaml>

To see all the resource created so far as part of Tekton Pipelines, run the
command:

  1. kubectl get build-pipeline

You will get an output similar to the following:

  1. NAME AGE
  2. taskruns/build-docker-image-from-git-source-task-run 30s
  3. NAME AGE
  4. pipelineresources/skaffold-git 6m
  5. pipelineresources/skaffold-image-leeroy-web 7m
  6. NAME AGE
  7. tasks/build-docker-image-from-git-source 7m

To see the output of the TaskRun, use the following command:

  1. kubectl get taskruns/build-docker-image-from-git-source-task-run -o yaml

You will get an output similar to the following:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: TaskRun
  3. metadata:
  4. creationTimestamp: 2018-12-11T18:14:29Z
  5. generation: 1
  6. name: build-docker-image-from-git-source-task-run
  7. namespace: default
  8. resourceVersion: "6733537"
  9. selfLink: /apis/tekton.dev/v1alpha1/namespaces/default/taskruns/build-docker-image-from-git-source-task-run
  10. uid: 99d297fd-fd70-11e8-9129-42010a8a0fdc
  11. spec:
  12. generation: 1
  13. inputs:
  14. params:
  15. - name: pathToDockerFile
  16. value: Dockerfile
  17. - name: pathToContext
  18. value: /workspace/git-source/examples/microservices/leeroy-web #configure: may change depending on your source
  19. resources:
  20. - name: git-source
  21. paths: null
  22. resourceRef:
  23. name: skaffold-git
  24. outputs:
  25. resources:
  26. - name: builtImage
  27. paths: null
  28. resourceRef:
  29. name: skaffold-image-leeroy-web
  30. taskRef:
  31. name: build-docker-image-from-git-source
  32. taskSpec: null
  33. trigger:
  34. type: manual
  35. status:
  36. conditions:
  37. - lastTransitionTime: 2018-12-11T18:15:09Z
  38. status: "True"
  39. type: Succeeded
  40. podName: build-docker-image-from-git-source-task-run-pod-24d414
  41. startTime: 2018-12-11T18:14:29Z
  42. steps:
  43. - terminated:
  44. containerID: docker://138ce30c722eed....c830c9d9005a0542
  45. exitCode: 0
  46. finishedAt: 2018-12-11T18:14:47Z
  47. reason: Completed
  48. startedAt: 2018-12-11T18:14:47Z
  49. - terminated:
  50. containerID: docker://4a75136c029fb1....4c94b348d4f67744
  51. exitCode: 0
  52. finishedAt: 2018-12-11T18:14:48Z
  53. reason: Completed
  54. startedAt: 2018-12-11T18:14:48Z

The status of type Succeeded = True shows the Task ran successfully and you
can also validate the Docker image is created in the location specified in the
resource definition.

Pipeline

A Pipeline defines a list of tasks to execute in order, while
also indicating if any outputs should be used as inputs of a following task by
using the from field and also indicating
the order of executing (using the runAfter and from fields).
The same templating you used in tasks is also available in pipeline.

For example:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: Pipeline
  3. metadata:
  4. name: tutorial-pipeline
  5. spec:
  6. resources:
  7. - name: source-repo
  8. type: git
  9. - name: web-image
  10. type: image
  11. tasks:
  12. - name: build-skaffold-web
  13. taskRef:
  14. name: build-docker-image-from-git-source
  15. params:
  16. - name: pathToDockerFile
  17. value: Dockerfile
  18. - name: pathToContext
  19. value: /workspace/examples/microservices/leeroy-web #configure: may change according to your source
  20. resources:
  21. inputs:
  22. - name: workspace
  23. resource: source-repo
  24. outputs:
  25. - name: image
  26. resource: web-image
  27. - name: deploy-web
  28. taskRef:
  29. name: deploy-using-kubectl
  30. resources:
  31. inputs:
  32. - name: workspace
  33. resource: source-repo
  34. - name: image
  35. resource: web-image
  36. from:
  37. - build-skaffold-web
  38. params:
  39. - name: path
  40. value: /workspace/examples/microservices/leeroy-web/kubernetes/deployment.yaml #configure: may change according to your source
  41. - name: yqArg
  42. value: "-d1"
  43. - name: yamlPathToImage
  44. value: "spec.template.spec.containers[0].image"

The above Pipeline is referencing a Task called deploy-using-kubectl which
can be found here:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: Task
  3. metadata:
  4. name: deploy-using-kubectl
  5. spec:
  6. inputs:
  7. resources:
  8. - name: workspace
  9. type: git
  10. - name: image
  11. type: image
  12. params:
  13. - name: path
  14. description: Path to the manifest to apply
  15. - name: yqArg
  16. description:
  17. Okay this is a hack, but I didn't feel right hard-coding `-d1` down
  18. below
  19. - name: yamlPathToImage
  20. description:
  21. The path to the image to replace in the yaml manifest (arg to yq)
  22. steps:
  23. - name: replace-image
  24. image: mikefarah/yq
  25. command: ["yq"]
  26. args:
  27. - "w"
  28. - "-i"
  29. - "${inputs.params.yqArg}"
  30. - "${inputs.params.path}"
  31. - "${inputs.params.yamlPathToImage}"
  32. - "${inputs.resources.image.url}"
  33. - name: run-kubectl
  34. image: lachlanevenson/k8s-kubectl
  35. command: ["kubectl"]
  36. args:
  37. - "apply"
  38. - "-f"
  39. - "${inputs.params.path}"

To run the Pipeline, create a PipelineRun as follows:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: PipelineRun
  3. metadata:
  4. name: tutorial-pipeline-run-1
  5. spec:
  6. pipelineRef:
  7. name: tutorial-pipeline
  8. trigger:
  9. type: manual
  10. resources:
  11. - name: source-repo
  12. resourceRef:
  13. name: skaffold-git
  14. - name: web-image
  15. resourceRef:
  16. name: skaffold-image-leeroy-web

The PipelineRun will create the TaskRuns corresponding to each Task and
collect the results.

To apply the yaml files use the following command, you will need to apply the
deploy-task if you want to run the Pipeline.

  1. kubectl apply -f <name-of-file.yaml>

To see the output of the PipelineRun, use the following command:

  1. kubectl get pipelineruns/tutorial-pipeline-run-1 -o yaml

You will get an output similar to the following:

  1. apiVersion: tekton.dev/v1alpha1
  2. kind: PipelineRun
  3. metadata:
  4. annotations:
  5. creationTimestamp: 2018-12-11T20:30:19Z
  6. generation: 1
  7. name: tutorial-pipeline-run-1
  8. namespace: default
  9. resourceVersion: "6760151"
  10. selfLink: /apis/tekton.dev/v1alpha1/namespaces/default/pipelineruns/tutorial-pipeline-run-1
  11. uid: 93acb0ea-fd83-11e8-9129-42010a8a0fdc
  12. spec:
  13. generation: 1
  14. pipelineRef:
  15. name: tutorial-pipeline
  16. resources:
  17. - name: source-repo
  18. paths: null
  19. resourceRef:
  20. name: skaffold-git
  21. - name: web-image
  22. paths: null
  23. resourceRef:
  24. name: skaffold-image-leeroy-web
  25. serviceAccount: ""
  26. trigger:
  27. type: manual
  28. status:
  29. conditions:
  30. - lastTransitionTime: 2018-12-11T20:32:41Z
  31. message: All Tasks have completed executing
  32. reason: Succeeded
  33. status: "True"
  34. type: Succeeded
  35. taskRuns:
  36. tutorial-pipeline-run-1-build-skaffold-web:
  37. conditions:
  38. - lastTransitionTime: 2018-12-11T20:31:41Z
  39. status: "True"
  40. type: Succeeded
  41. podName: tutorial-pipeline-run-1-build-skaffold-web-pod-21ddf0
  42. startTime: 2018-12-11T20:30:19Z
  43. steps:
  44. - terminated:
  45. containerID: docker://c699fcba94....f96108ac9f4db22b94e0c
  46. exitCode: 0
  47. finishedAt: 2018-12-11T20:30:36Z
  48. reason: Completed
  49. startedAt: 2018-12-11T20:30:36Z
  50. - terminated:
  51. containerID: docker://f5f752d....824262ad6ce7675
  52. exitCode: 0
  53. finishedAt: 2018-12-11T20:31:17Z
  54. reason: Completed
  55. startedAt: 2018-12-11T20:30:37Z
  56. tutorial-pipeline-run-1-deploy-web:
  57. conditions:
  58. - lastTransitionTime: 2018-12-11T20:32:41Z
  59. status: "True"
  60. type: Succeeded
  61. podName: tutorial-pipeline-run-1-deploy-web-pod-7a796b
  62. startTime: 2018-12-11T20:32:11Z
  63. steps:
  64. - terminated:
  65. containerID: docker://eaefb7b6d685....f001f895430f71374
  66. exitCode: 0
  67. finishedAt: 2018-12-11T20:32:28Z
  68. reason: Completed
  69. startedAt: 2018-12-11T20:32:28Z
  70. - terminated:
  71. containerID: docker://4cfc6eba47a7a....dcaef1e9b1eee3661b8a85f
  72. exitCode: 0
  73. finishedAt: 2018-12-11T20:32:31Z
  74. reason: Completed
  75. startedAt: 2018-12-11T20:32:31Z
  76. - terminated:
  77. containerID: docker://01b376b92....dce4ccec9641d77
  78. exitCode: 0
  79. finishedAt: 2018-12-11T20:32:35Z
  80. reason: Completed
  81. startedAt: 2018-12-11T20:32:34Z

The status of type Succeeded = True shows the pipeline ran successfully, also
the status of individual Task runs are shown.

Local development

Known good configuration

Tekton Pipelines is known to work with:

  • Docker for Desktop: a
    version that uses Kubernetes 1.11 or higher. At the time of this document,
    this requires the edge version of Docker to be installed. A known good
    configuration specifies six CPUs, 10 GB of memory and 2 GB of swap space
  • The following
    prerequisites
  • Setting host.docker.local:5000 as an insecure registry with Docker for
    Desktop (set via preferences or configuration, see the
    Docker insecure registry documentation
    for details)
  • Passing --insecure as an argument to Kaniko tasks lets us push to an
    insecure registry
  • Running a local (insecure) Docker registry: this can be run with

docker run -d -p 5000:5000 --name registry-srv -e REGISTRY_STORAGE_DELETE_ENABLED=true registry:2

  • Optionally, a Docker registry viewer so we can check our pushed images are
    present:

docker run -it -p 8080:8080 --name registry-web --link registry-srv -e REGISTRY_URL=http://registry-srv:5000/v2 -e REGISTRY_NAME=localhost:5000 hyper/docker-registry-web

Images

  • Any PipelineResource definitions of image type should be updated to use the
    local registry by setting the url to
    host.docker.internal:5000/myregistry/<image name> equivalents
  • The KO_DOCKER_REPO variable should be set to localhost:5000/myregistry
    before using ko
  • You are able to push to host.docker.internal:5000/myregistry/<image name>
    but your applications (e.g any deployment definitions) should reference
    localhost:5000/myregistry/<image name>

Logging

  • Logs can remain in-memory only as opposed to sent to a service such as
    Stackdriver.

Elasticsearch, Beats and Kibana can be deployed locally as a means to view logs:
an example is provided at
https://github.com/mgreau/tekton-pipelines-elastic-tutorials.

Experimentation

Lines of code you may want to configure have the #configure annotation. This
annotation applies to subjects such as Docker registries, log output locations
and other nuances that may be specific to particular cloud providers or
services.

The TaskRuns have been created in the following
order:

  1. tutorial-pipeline-run-1-build-skaffold-web - This runs the
    Pipeline Task build-skaffold-web first,
    because it has no from or runAfter clauses
  2. tutorial-pipeline-run-1-deploy-web - This runs deploy-web second, because
    its input web-image comes from
    build-skaffold-web (therefore build-skaffold-web must run before
    deploy-web).

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.