Writing a Resusable CICD Pipeline using OpenShift Pipelines

Writing and Running a ‘Source to Image to Deploy Pipeline’

Nikhil Thomas
Dec 6, 2019 · 6 min read

Our goal in this article is to build a cloud native CICD pipeline for a ‘Voting Web App’. The application consists of 2 components a frontend service and a backend service. We will be setting up a CICD pipeline which has 2 stages:

  1. Building container images (using respective Dockerfiles)
  2. Deploying application on OpenShift (Deployments, Services)

Prerequisites

Our ‘Voting’ Web-Application

We will setting up a reusable CICD pipeline using OpenShift Pipeline to build and deploy a voting application on OpenShift.

This application has a frontend (Web UI) powered by python and a backend (Datastore) written in Golang. The the source code repositories are:

Fork these repositories so that you can have full control over the application source code. The forks will enable you to push patches to the source code and rerun the CICD pipeline.

CICD Pipeline Definition

The pipeline we are building is there in the ‘Vote-CICD’ repository:

Vote-CICD: https://github.com/nikhil-thomas/vote-cicd

Fork this repository in github and clone your fork on your local machine.

$ git clone https://github.com/<your-github-username>/vote-cicd.git

Go to your Vote-CICD clone location on your local machine and checkout `part-2-cicd-pipeline` branch

$ cd vote-cicd

The directory structure will be:

vote-cicd
├── 02-pipelineresources
│ ├── 01-vote-api-git-resource.yaml
│ ├── 02-vote-api-image-resource.yaml
│ ├── 04-vote-ui-git-resource.yaml
│ └── 04-vote-ui-image-resource.yaml
├── 03-tasks
│ └── 01-oc-apply-deployment-task.yaml
├── 04-pipelines
│ └── 01-build-deploy-pipeline.yaml
├── 05-pipelineruns
│ ├── 01-build-deploy-api-pipelinerun.yaml
│ └── 02-build-deploy-ui-pipelinerun.yaml
├── LICENSE
└── README.md

Create a New Project

Login to your OpenShift 4.x cluster from your local machine and create a new project called vote-cicd.

vote-cicd$ oc new-project cote-cicd

Creating PipelineResource

We need to create 4 Pipeline Resources:

  1. vote-api source code (PipelineResource type: git)
  2. vote-api container image(PipelineResource type: image)
  3. vote-ui source code (PipelineResource type: git)
  4. vote-ui container image (PipelineResource type: image)

The PipelineResource definitions are in the 02-pipelineresources directory. . If you are using your fork of `vote-api` and `vote-ui ` use your github repository urls instead of nikhil-thomas/vote-api, edit the vote-api-git-resource.yaml and vote-ui-git-resource.yaml.

apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
name: vote-api-repo
spec:
type: git
params:
- name: url
value: https://github.com/<your github username>/vote-api.git

Now add the PipelineResources to your cluster using the oc command.

vote-cicd$ oc apply -f 02-pipelineresources/

CICD Pipeline Tasks

Our CICD Pipeline has two stages:

  1. Build Container Image: We will be using the default `buildah-v0–8–0` Task (ClusterTask) shipped with the OpenShift-Pipelines-Operator
  2. Deploy Container Image on OpenShift: We will be writing a custom task to accomplish this.

The Task`oc-apply-deployment` is defined in: 03-tasks . Add it to the cluster.

vote-cicd$ oc apply -f 03-tasks/

Creating Build-Deploy Pipeline

Add the Pipeline defined in 04-pipelines

vote-cicd$ oc apply -f 04-pipelines/

DevConsole and our CICD Pipeline

On the OpenShift 4.x WebConsole switch to Developer Perspective by clicking the drop-down menu in the top left corner. Select project: vote-cicd. Our `build-and-deploy` pipeline will be listed there.

We can see that the pipeline has 2 stages:

  1. build-image: Task-ref: buildah-v0–8–0 (ClusterTask supplied by operator)
  2. create-update-deployment: TaskRef: oc-apply-deployment (Task created by us)

The individual steps within tasks will be shown in a pop up when we hover the mouse pointer on a Task in the Pipeline.

Build and Deploy Voting App

The same build-and-deploy Pipeline can be used to build vote-api and vote-ui. The vote-api and vote-ui are built and deployed by creating PipelineRuns. PipelineRuns bind a Pipeline to a projects PipelineResources (src-repo, image) and necessary input parameters. We do it by creating separate PipelineRuns for vote-api and vote-ui projects as defined in 05-pipelineruns.

Start the Build-and-Deploy PipelineRuns for vote-api and vote-ui

vote-cicd$ oc apply -f 05-pipelineruns/

Using Tekton Command Line Interface tkn we can list Tekton resources. We can fetch well formatted logs from PipelineRuns.

We can look at the PipelineRun logs using tkn pr logs -f <pipelinerun-name> command.

In addition, the PipelineRuns and their logs can be viewed from the DevConsole UI as well in DeveloperPerspective.

Verifying Vote-Application

On successful completion of both PipelineRuns we should see:

  1. Deployments: 1 each for vote-ui and vote-api
  2. Services: 1 each for vote-ui and vote-api
  3. Route: 1 for vote-ui (frontend)

The Vote application will be available on the newly created route.

ReRunning CICD Pipeline

First we have to push updates Vote-UI or Vote-API source code to respective git repositories. The we can just rerun the PipelineRuns. It will fetch the new updates build a new container image and then update the images in the respective (frontend/backend) deployments.

Other Articles in this series

References:

  1. Tekton Pipelines Overview: https://www.youtube.com/watch?v=-ji5Z0qJmJs
  2. Tekton Pipelines Getting Started: https://github.com/tektoncd/pipeline/tree/master/docs#tekton-pipelines
  3. Tekton Pipelines Examples: https://github.com/tektoncd/pipeline/tree/master/examples#examples
  4. TektonCD Task Catalog: https://github.com/tektoncd/catalog
  5. TektonCD Github Org: https://github.com/tektoncd
  6. OpensShift Pipelines Tutorial: https://github.com/openshift/pipelines-tutorial
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade