OpenShift Pipelines (Tekton)

Build and deploy with OpenShift Pipelines. Tasks, a pipeline and a GitLab trigger all run as pods on the cluster.

Updated CompTech Team

OpenShift Pipelines is Tekton packaged for OpenShift. A pipeline is a set of pods: each step is a container, the workspace is a volume, and nothing is left installed on a runner when the job ends. You version the pipeline next to the application.

This lab creates a pipeline that clones a repository, builds an image with the cluster's Buildah task, and rolls the deployment. A trigger starts that pipeline when GitLab sends a push hook. Signing and scanning belong in DevSecOps. Promoting the result between environments belongs in GitOps.

Prerequisites

  • An OpenShift 4.x cluster with the OpenShift Pipelines operator installed
  • The oc and tkn CLIs
  • A project payments and a Dockerfile at the root of a Git repository the cluster can clone

The operator installs reusable tasks in the openshift-pipelines project. This lab references git-clone, buildah and openshift-client from there, through the cluster resolver.

A pipeline

apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: build-and-deploy
  namespace: payments
spec:
  params:
    - name: git-url
      type: string
    - name: git-revision
      type: string
      default: main
    - name: image
      type: string
  workspaces:
    - name: source
  tasks:
    - name: fetch
      taskRef:
        resolver: cluster
        params:
          - { name: kind, value: task }
          - { name: name, value: git-clone }
          - { name: namespace, value: openshift-pipelines }
      params:
        - name: url
          value: $(params.git-url)
        - name: revision
          value: $(params.git-revision)
      workspaces:
        - { name: output, workspace: source }
    - name: build
      runAfter: [fetch]
      taskRef:
        resolver: cluster
        params:
          - { name: kind, value: task }
          - { name: name, value: buildah }
          - { name: namespace, value: openshift-pipelines }
      params:
        - name: IMAGE
          value: $(params.image)
      workspaces:
        - { name: source, workspace: source }
    - name: deploy
      runAfter: [build]
      taskRef:
        resolver: cluster
        params:
          - { name: kind, value: task }
          - { name: name, value: openshift-client }
          - { name: namespace, value: openshift-pipelines }
      params:
        - name: SCRIPT
          value: |
            oc set image deployment/payments app=$(params.image)
            oc rollout status deployment/payments
oc apply -f pipeline.yaml
oc create sa pipeline -n payments
oc policy add-role-to-user edit system:serviceaccount:payments:pipeline -n payments
tkn pipeline start build-and-deploy -n payments \
  -p git-url=https://gitlab.apps.example.com/platform/payments.git \
  -p image=image-registry.openshift-image-registry.svc:5000/payments/payments:dev \
  -w name=source,volumeClaimTemplateFile=workspace.yaml \
  --serviceaccount pipeline
tkn pipelinerun logs -f -n payments

workspace.yaml is a PersistentVolumeClaim spec of 1Gi, ReadWriteOnce. Buildah pushes to the internal registry, so the pipeline service account also needs system:image-builder on payments.

Start it from a Git push

A trigger turns a webhook into a PipelineRun. The binding pulls the repository URL and the commit out of GitLab's push payload.

apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
  name: build
  namespace: payments
spec:
  params:
    - name: git-url
    - name: git-revision
  resourcetemplates:
    - apiVersion: tekton.dev/v1
      kind: PipelineRun
      metadata:
        generateName: build-
      spec:
        pipelineRef:
          name: build-and-deploy
        serviceAccountName: pipeline
        params:
          - { name: git-url, value: $(tt.params.git-url) }
          - { name: git-revision, value: $(tt.params.git-revision) }
          - name: image
            value: image-registry.openshift-image-registry.svc:5000/payments/payments:$(tt.params.git-revision)
        workspaces:
          - name: source
            volumeClaimTemplate:
              spec:
                accessModes: [ReadWriteOnce]
                resources:
                  requests:
                    storage: 1Gi

A TriggerBinding named gitlab-push maps $(body.project.git_http_url) to git-url and $(body.checkout_sha) to git-revision. Point an EventListener at that binding and the template, with the gitlab interceptor checking the webhook secret. Expose the listener with a Route, and set that URL as a push webhook on the GitLab project. Give the listener's service account the tekton-triggers-eventlistener-roles role in the project so it can create PipelineRun objects.

oc get pipelinerun -n payments
tkn pipelinerun describe --last -n payments

A step is a container. A task is an ordered list of steps that share a pod. A pipeline is a graph of tasks, and runAfter is the order. A PipelineRun is one execution. The workspace is how fetch hands the clone to build. Keep tasks in Git. A task that pulls a latest image will not build the same way next month.

What's next

  1. The GitLab-native version of this pipeline is GitLab CI.
  2. Stop deploying from the pipeline and let OpenShift GitOps sync the cluster.
  3. Gate the build task on a scan and a signature in DevSecOps.