Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 37 ยท Builds and Pipelines

OpenShift Pipelines (Tekton)

Build CI/CD pipelines on OpenShift with Tekton: Tasks, steps, Pipelines, PipelineRuns, workspaces, results, triggers and the tkn CLI, with a runnable pipeline stand-in.

OpenShift Pipelines is Red Hat's supported distribution of Tekton, a Kubernetes-native CI/CD framework. Each step of a pipeline runs in a container, inside your cluster, and the pipeline itself is described with Kubernetes objects. You install it as an Operator from OperatorHub (the "Red Hat OpenShift Pipelines" Operator); after that, the console shows a Pipelines section.

The Tekton vocabulary

Object Meaning
Step One container command, for example npm test
Task An ordered list of steps that run in one Pod
Pipeline A graph of Tasks with inputs, outputs and ordering
TaskRun One execution of a Task
PipelineRun One execution of a Pipeline (this is what you start)
Workspace A volume shared between Tasks (source code, caches)
Params Inputs such as a Git URL or an image name
Results Small values a Task passes to later Tasks (a commit ID, an image digest)

A Task is like a function; a Pipeline composes functions; a PipelineRun is a call with arguments.

flowchart LR
  A["git-clone"] --> B["unit-tests"]
  B --> C["build-image"]
  C --> D["deploy"]

Figure: a simple pipeline: clone, test, build, deploy.

A Task

YAML
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: run-tests
spec:
  params:
    - name: command
      type: string
      default: "npm test"
  workspaces:
    - name: source
  steps:
    - name: install
      image: registry.access.redhat.com/ubi9/nodejs-20
      workingDir: $(workspaces.source.path)
      script: |
        npm ci
    - name: test
      image: registry.access.redhat.com/ubi9/nodejs-20
      workingDir: $(workspaces.source.path)
      script: |
        $(params.command)

Steps share the Task's workspaces and run one after another in the same Pod. $(params.command) is substituted by Tekton before the Pod starts.

A Pipeline

YAML
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: build-and-deploy
spec:
  params:
    - name: git-url
      type: string
    - name: image
      type: string
  workspaces:
    - name: shared
  tasks:
    - name: fetch
      taskRef:
        name: git-clone
        kind: ClusterTask
      workspaces:
        - name: output
          workspace: shared
      params:
        - name: url
          value: $(params.git-url)
    - name: test
      runAfter: ["fetch"]
      taskRef:
        name: run-tests
      workspaces:
        - name: source
          workspace: shared
    - name: build
      runAfter: ["test"]
      taskRef:
        name: buildah
        kind: ClusterTask
      params:
        - name: IMAGE
          value: $(params.image)
      workspaces:
        - name: source
          workspace: shared

runAfter creates ordering. Tasks without a dependency run in parallel. Reusable Tasks such as git-clone, buildah and openshift-client are provided by the Operator or the Tekton catalog; the way they are published (ClusterTask, a cluster resolver, or Tekton Hub resolver) depends on your Operator version, so check the names in the console under Pipelines, Tasks.

Running and watching

Terminal
oc apply -f task.yaml -f pipeline.yaml
tkn pipeline start build-and-deploy \
  -p git-url=https://github.com/example/web-app.git \
  -p image=image-registry.openshift-image-registry.svc:5000/shop-dev/web:latest \
  -w name=shared,volumeClaimTemplateFile=pvc.yaml \
  --showlog
tkn pipelinerun list
tkn pipelinerun logs -f
oc get pipelinerun

tkn is the Tekton CLI. A volumeClaimTemplate workspace asks Tekton to create a temporary PersistentVolumeClaim for each run. In the console, Pipelines shows a graph of the Tasks and their live status, and a run's log per step.

You can also generate a PipelineRun as a manifest and store it in Git:

YAML
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  generateName: build-and-deploy-run-
spec:
  pipelineRef:
    name: build-and-deploy
  params:
    - name: git-url
      value: https://github.com/example/web-app.git
    - name: image
      value: image-registry.openshift-image-registry.svc:5000/shop-dev/web:latest
  workspaces:
    - name: shared
      volumeClaimTemplate:
        spec:
          accessModes: ["ReadWriteOnce"]
          resources:
            requests:
              storage: 1Gi

Triggers: start a pipeline from a push

Tekton Triggers receive an HTTP webhook (from GitHub, GitLab or another system), extract values from the payload and create a PipelineRun. Three pieces:

  • EventListener: a Service and Pod that listens for events.
  • TriggerBinding: maps payload fields (repository URL, commit ID) to parameters.
  • TriggerTemplate: the PipelineRun to create with those parameters.

In the console's Developer view you can also add triggers when you create a pipeline, and the Pipelines as Code feature can read .tekton/ files from your repository and run them for each pull request.

Pipeline design tips

  1. Keep Tasks small and reusable; parameterize everything.
  2. Use a workspace for source and caches; use results only for small strings.
  3. Run tests before building images; fail fast.
  4. Give pipelines a service account with only the permissions they need (pipeline is the default one the Operator creates).
  5. Scan images and sign artifacts as separate Tasks.
  6. Deploy by updating Git (GitOps) or by oc in a final Task, but prefer Git for production.

Stand-in: a pipeline runner

Tekton itself needs a cluster. The stand-in below shows how a Pipeline's runAfter graph is ordered and how a failed Task stops the downstream ones. It is a toy scheduler written for this lesson.

Python Stand-in: run tasks in dependency order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in: parameter substitution

Python Stand-in: substitute $(params.x) in steps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which object do you create to actually run a Pipeline?

    A Pipeline is a definition. A PipelineRun is one execution of it.
  2. 2. What is a Workspace used for?

    Workspaces mount volumes that Tasks share, such as the cloned repository.
  3. 3. How do you make a Task wait for another Task?

    runAfter (or using another task's result) orders Tasks. Tasks without dependencies can run in parallel.
  4. 4. Which Tekton Triggers piece maps webhook payload fields to parameters?

    A TriggerBinding extracts values; a TriggerTemplate uses them to create a PipelineRun; the EventListener receives the webhook.

Summary

  • OpenShift Pipelines is Tekton installed by an Operator; pipelines are Kubernetes objects.
  • Steps form Tasks, Tasks form Pipelines, and a PipelineRun executes a Pipeline.
  • Workspaces share files, params pass inputs, results pass small values.
  • Triggers (EventListener, TriggerBinding, TriggerTemplate) start runs from webhooks.
  • Keep Tasks small, fail fast and give pipelines least-privilege service accounts.

Next: GitOps with Argo CD