Science and technology

Write your first CI/CD pipeline in Kubernetes with Tekton

Tekton is a Kubernetes-native open supply framework for creating steady integration and steady supply (CI/CD) methods. It additionally helps to do end-to-end (construct, take a look at, deploy) software improvement throughout a number of cloud suppliers or on-premises methods by abstracting away the underlying implementation particulars.

Introduction to Tekton

Tekton, recognized initially as Knative Build, later acquired restructured as its personal open supply undertaking with its personal governance organization and is now a Linux Foundation undertaking. Tekton gives an in-cluster container picture construct and deployment workflow—in different phrases, it’s a steady integration (CI) and steady supply (CD) service. It consists of Tekton Pipelines and a number of other supporting parts, similar to Tekton CLI, Triggers, and Catalog.

Tekton is a Kubernetes native software. It installs and runs as an extension on a Kubernetes cluster and includes a set of Kubernetes Custom Resources that outline the constructing blocks you possibly can create and reuse on your pipelines. Because it is a Ok-native expertise, Tekton is remarkably simple to scale. When it’s good to improve your workload, you possibly can simply add nodes to your cluster. It’s additionally simple to customise due to its extensible design and because of a neighborhood repository of contributed parts.

Tekton is right for builders who want CI/CD methods to do their work and platform engineers who construct CI/CD methods for builders of their group.

Tekton parts

Building CI/CD pipelines is a far-reaching endeavor, so Tekton gives instruments for each step of the way in which. Here are the key parts you get with Tekton:

  • Pipeline: Pipeline defines a set of Kubernetes Custom Resources that act as constructing blocks you employ to assemble your CI/CD pipelines.
  • Triggers: Triggers is a Kubernetes Custom Resource that permits you to create pipelines based mostly on info extracted from occasion payloads. For instance, you possibly can set off the instantiation and execution of a pipeline each time a merge request will get opened towards a Git repository.
  • CLI: CLI gives a command-line interface known as tkn that permits you to work together with Tekton out of your terminal.
  • Dashboard: Dashboard is a web-based graphical interface for Tekton pipelines that shows details about the execution of your pipelines.
  • Catalog: Catalog is a repository of high-quality, community-contributed Tekton constructing blocks (duties, pipelines, and so forth) prepared to be used in your individual pipelines.
  • Hub: Hub is a web-based graphical interface for accessing the Tekton catalog.
  • Operator: Operator is a Kubernetes Operator pattern that permits you to set up, replace, improve, and take away Tekton initiatives on a Kubernetes cluster.
  • Chains: Chains is a Kubernetes Custom Resource Definition (CRD) controller that permits you to handle your provide chain safety in Tekton. It is at the moment a work-in-progress.
  • Results: Results goals to assist customers logically group CI/CD workload historical past and separate out long-term outcome storage away from the pipeline controller.

Tekton terminology

  • Step: A step is probably the most fundamental entity in a CI/CD workflow, similar to working some unit checks for a Python internet app or compiling a Java program. Tekton performs every step with a supplied container picture.
  • Task: A job is a group of steps in a particular order. Tekton runs a job within the type of a Kubernetes pod, the place every step turns into a working container within the pod.
  • Pipelines: A pipeline is a group of duties in a particular order. Tekton collects all duties, connects them in a directed acyclic graph (DAG), and executes the graph in sequence. In different phrases, it creates various Kubernetes pods and ensures that every pod completes working efficiently as desired.
  • PipelineRun: A PipelineRun, as its title implies, is a particular execution of a pipeline.
  • TaskRun: A TaskRun is a particular execution of a job. TaskRuns are additionally obtainable once you select to run a job exterior a pipeline, with which you will view the specifics of every step execution in a job.

Create your individual CI/CD pipeline

The best approach to get began with Tekton is to jot down a easy pipeline of your individual. If you employ Kubernetes day-after-day, you are in all probability comfy with YAML, which is exactly how Tekton pipelines are outlined. Here’s an instance of a easy pipeline that clones a code repository.

First, create a file known as job.yaml and open it in your favourite textual content editor. This file defines the steps you wish to carry out. In this instance, that is cloning a repository, so I’ve named the step clone. The file units some setting variables after which gives a easy shell script to carry out the clone.

Next comes the duty. You can consider a step as a operate that will get known as by the duty, and the duty units parameters and workspaces required for steps.

apiVersion: tekton.dev/v1beta1
form
: Task
metadata
:
 title
: git-clone
spec
:
 workspaces
:
   - title
: output
     description
: The git repo will probably be cloned onto the quantity backing this Workspace.
 params
:
   - title
: url
     description
: Repository URL to clone from.
     kind
: string
   - title
: revision
     description
: Revision to checkout. (department, tag, sha, ref, and so on...)
     kind
: string
     default
: ""
 steps
:
   - title
: clone
     picture
: "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:v0.21.0"
     env
:
       - title
: PARAM_URL
         worth
: $(params.url)
       - title
: PARAM_REVISION
         worth
: $(params.revision)
       - title
: WORKSPACE_OUTPUT_PATH
         worth
: $(workspaces.output.path)
     script
: |
      #!/usr/bin/env sh
       set -eu

       CHECKOUT_DIR="${WORKSPACE_OUTPUT_PATH}"

       /ko-app/git-init
         -url="${PARAM_URL}"
         -revision="${PARAM_REVISION}"
         -path="${CHECKOUT_DIR}"
       cd "${CHECKOUT_DIR}"
       EXIT_CODE="$?"
       if [ "${EXIT_CODE}" != 0 ] ; then
         exit "${EXIT_CODE}"
       fi
       # Verify clone is success by studying readme file.
       cat ${CHECKOUT_DIR}/README.md

Create a second file known as pipeline.yaml, and open it in your favourite textual content editor. This file defines the pipeline by setting vital parameters, similar to a workspace the place the duty could be run and processed.

apiVersion: tekton.dev/v1beta1
form
: Pipeline
metadata
:
 title
: cat-branch-readme
spec
:
 params
:
   - title
: repo-url
     kind
: string
     description
: The git repository URL to clone from.
   - title
: branch-name
     kind
: string
     description
: The git department to clone.
 workspaces
:
   - title
: shared-data
     description
: |
      This workspace will obtain the cloned git repo and be handed
       to the following Task for the repo's README.md file to be learn.

 duties
:
   - title
: fetch-repo
     taskRef
:
       title
: git-clone
     workspaces
:
       - title
: output
         workspace
: shared-data
     params
:
       - title
: url
         worth
: $(params.repo-url)
       - title
: revision
         worth
: $(params.branch-name)

Finally, create a file known as pipelinerun.yaml and open it in your favourite textual content editor. This file really runs the pipeline. It invokes parameters outlined within the pipeline (which, in flip, invokes the duty outlined by the duty file.)

apiVersion: tekton.dev/v1beta1
form
: PipelineRun
metadata
:
 title
: git-clone-checking-out-a-branch
spec
:
 pipelineRef
:
   title
: cat-branch-readme
 workspaces
:
   - title
: shared-data
     volumeClaimTemplate
:
       spec
:
         accessModes
:
          - ReadWriteOnce
         assets
:
           requests
:
             storage
: 1Gi
 params
:
   - title
: repo-url
     worth
: https://github.com/tektoncd/pipeline.git
   - title
: branch-name
     worth
: release-v0.12.x

The benefit of structuring your work in separate recordsdata is that the git-clone job is reusable for a number of pipelines.

For instance, suppose you wish to do end-to-end testing for a pipeline undertaking. You can use the git-clone job to make sure that you’ve gotten a recent copy of the code it’s good to take a look at.

Wrap up

As lengthy as you are conversant in Kubernetes, getting began with Tekton is as simple as adopting every other Ok-native software. It has loads of instruments that can assist you create pipelines and to interface along with your pipelines. If you’re keen on automation, strive Tekton!

Most Popular

To Top