
Using OpenShift Pipelines
Automated builds and deployments – known as CI/CD – of container-based applications can reduce mistakes, improve productivity, and promote more thorough testing. This sandbox activity introduces OpenShift Pipelines for automated builds and deployment.
OpenShift Pipelines is added to a Red Hat OpenShift cluster by the OpenShift Pipelines operator. This operator is already installed in the Developer Sandbox. So all we need to do is create objects and run our pipelines.
Introduction
Automated builds and deployments – known as CI/CD – of container-based applications can reduce mistakes, improve productivity, and promote more thorough testing. This sandbox activity introduces OpenShift Pipelines for automated builds and deployment.
OpenShift Pipelines is added to a Red Hat OpenShift cluster by the OpenShift Pipelines operator. This operator is already installed in the Developer Sandbox for Red Hat OpenShift. So all we need to do is create objects and run our pipelines.
What you will do
Our pipeline will pull source code from a GitHub repository, build the application, and deploy the image to the image registry inside our sandbox cluster. You will do the work from the command line, but observe the results in the browser.
The resulting application, qotd, is a web service that returns Quote Of The Day information. I have the following endpoints, accessed using the HTTP GET operation:
- /
- /version
- /writtenin
- /quotes
- /quotes/{id}
- /quotes/random
Here are the steps:
- Clone the Git repo.
- Create the pipeline.
- Create the workspace.
- Create necessary tasks.
- Run the pipeline.
- View the results.
How long will this activity take?
You should budget about 30 minutes to complete this sandbox activity.
What will you learn?
When you've finish this activity, you'll have create an OpenShift Pipeline and used it to deploy code into your cluster.
Prerequisites
You will need the following to complete this activity:
- A Developer Sandbox account.
- The
oc
command-line interface needs to be installed on your computer, following these installation instructions. - The
tkn
command-line interface needs to be installed on your computer, following these installation instructions. - A web browser to access your sandbox dashboard.
Programming languages
You don’t need to know any programming languages to complete this task.
If you need help
If you get stuck, something isn’t working, or you simply have questions, you can easily contact us via email at devsandbox@redhat.com.
If you’re unsure how to log in to your sandbox from the command line, you can find instructions at: Access your Developer Sandbox for Red Hat OpenShift from the command line | Red Hat Developer.
Step 1: Clone the Git repo
Where you put this on your PC will be your working directory. You will change some of the code based on your sandbox.
Run the following command to clone the Git repo to your PC:
git clone
https://github.com/redhat-developer-demos/openshift-pipelines-workshop
Preparing for Step 2
In Step 2, we’ll create the pipeline object from the command line. The only tool needed is the oc
command-line interface.
The contents of the qotd-pipeline.yaml
file are as follows:
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: qotd-build-and-deploy
spec:
workspaces:
- name: shared-workspace
params:
- name: git-url
type: string
description: url of the git repo for the code of deployment
default: https://github.com/redhat-developer-demos/qotd.git
- name: IMAGE
type: string
description: image to be built from the code
default: image-registry.openshift-image-registry.svc:5000/<<project_name>>/qotd:latest
tasks:
- name: fetch-repository
taskRef:
name: git-clone
kind: ClusterTask
workspaces:
- name: output
workspace: shared-workspace
params:
- name: url
value: $(params.git-url)
- name: subdirectory
value: ""
- name: deleteExisting
value: "true"
- name: build-image
taskRef:
name: buildah
kind: ClusterTask
params:
- name: TLSVERIFY
value: "false"
- name: IMAGE
value: $(params.IMAGE)
workspaces:
- name: source
workspace: shared-workspace
runAfter:
- fetch-repository
- name: apply-manifests
taskRef:
name: apply-manifests
workspaces:
- name: source
workspace: shared-workspace
runAfter:
- build-image
Notice the IMAGE parameter and, specifically, its default value. That value will not work; it must be changed. There are two ways to change the value: Change the value in this file using a text editor, or override the value when you use the tkn
command-line tool to run this pipeline.
Given this knowledge, we realize we can also change the value of the git-url parameter at the command line. This means this pipeline can be reused. Take a GitHub repo — that we specify at the command line — build a Linux image, and deposit that image into an image registry, specified at the command line.
Step 2: Create the pipeline
Run the following command to create the qotd-build-and-deploy pipeline object:
oc apply -f qotd-pipeline.yaml
Preparing for Step 3
The git-clone
ClusterTask in our pipeline needs a workspace to place the source code. To build our Linux image, the buildah
ClusterTask will read the source from that same workspace. A Persistent Volume Claim (PVC) is what we use in Kubernetes.
Step 3: Create the workspace
Run the following command to build the PVC to be used as our workspace:
oc create -f workspace.yaml
Preparing for Step 4
When you look at the qotd-pipeline.yaml file, you will see three tasks referenced (the “TaskRef” dictionaries within the “tasks” dictionary: git-clone, buildah, and apply-manifests.
The first two, git-clone and buildah
, are known as ClusterTasks; Tasks built into the OpenShift Pipeline operator. You can see a list of ClusterTask objects by running the command tkn clustertask list
. This is entirely optional.
Further, you can inspect a ClusterTask by running the tkn clustertask describe
<<taskname>>
command, such as tkn clustertask describe buildah
. But, again, this is entirely optional.
However, the third and final task in our pipeline is apply-manifests
, which is not a ClusterTask. In this case, we need to create this task object.
The contents of the apply_manifest_task.yaml file are as follows:
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: apply-manifests
spec:
workspaces:
- name: source
params:
- name: manifest_dir
description: The directory in source that contains yaml manifests
type: string
default: "k8s"
steps:
- name: apply
image: quay.io/openshift/origin-cli:latest
workingDir: /workspace/source
command: ["/bin/bash", "-c"]
args:
- |-
oc apply -f $(inputs.params.manifest_dir)
What does this do? How does it work? The apply-manifests task will look in the directory specified by the parameter manifest_dir — which has a default value of k8s” — and run the oc apply -f …
command for every file found in that directory. In the case of our source code, the “k8s” directory includes three files. Note that this is the source code as specified by the git-url parameter value supplied to the Pipeline at runtime, and not the source code of the pipeline:
- deploymentconfig.yaml
- route.yaml
- Service.yaml
You can view the contents of these files if you wish to see what they do. The important point is that this one pipeline task performs multiple operations based on the source code supplied. This yields a lot of power and flexibility.
Step 4: Create necessary tasks
Run the following command to create the “apply-manifests” Task:
oc create -f apply_manifest_task.yaml
Preparing for Step 5
Now you have all the pieces in place: A pipeline, the workspace, a task, and the knowledge needed, (e.g., the URL of the source code). It’s time to run the pipeline next.
Note: You will need the name of your OpenShift project to run the pipeline (Figure 1).
Before running the command, set up your command line and browser to view the activity as the pipeline runs. This is entirely optional.
In the browser, select the Developer mode, then Pipelines. You will see your pipeline displayed (Figure 2).
Next, position your command line such that you can see both the command line in the foreground and the pipeline in the background. When you run the pipeline, you can view the action in both contexts. Figure 3 is a screenshot of my setup.
Step 5: Run the pipeline
Run the following command to run the pipeline. Note: You will need to replace <<yourworkspacename>> with the name of your OpenShift Sandbox project name that you noted in the “Preparing for step 5” section of this activity:
tkn pipeline start qotd-build-and-deploy -w name=shared-workspace,claimName=source-pvc -p git-url=https://github.com/redhat-developer-demos/qotd.git -p IMAGE=image-registry.openshift-image-registry.svc:5000/<<yourworkspacename>>/qotd:latest
Here’s an example of the command that I used:
tkn pipeline start qotd-build-and-deploy -w name=shared-workspace,claimName=source-pvc -p git-url=https://github.com/redhat-developer-demos/qotd.git -p IMAGE=image-registry.openshift-image-registry.svc:5000/rhn-engineering-dschenck-dev/qotd:latest
Two other GitHub repos can be used in the command. You can substitute them if you wish to work with C# or Nodejs.
The pipeline run will be visible in the OpenShift dashboard (Figure 4).
When you started the run at the command line, it immediately returned with a command that you can use to observe your pipeline run from the command line. The command will be very similar to this (my own results). In order to track the PipelineRun progress run:
tkn pipelinerun logs qotd-build-and-deploy-run-7wl4v -f -n rhn-engineering-dschenck-dev
Paste your command into your command line and press Enter to observe the pipeline run from your command line. You should see a fair amount of activity, while in the background, in the OpenShift dashboard, the progress is displayed at a less granular level.
Step 6: View the results
Run the following command to get the URL of the service:
oc get route qotd
Using the URL from the previous command, use the curl
command to get a random quote. Run the following command to see the results — substituting your URL as needed:
PowerShell
(curl <<route_URL>>/quotes/random).Content
Bash
curl <<route_URL>>/quotes/random
Here are examples:
$ oc get routes qotd
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD
qotd qotd-rhn-engineering-dschenck-dev.apps.sandbox-m2.ll9k.p1.openshiftapps.com qotd 10000-tcp None
$ (curl http://qotd-rhn-engineering-dschenck-dev.apps.sandbox-m2.ll9k.p1.openshiftapps.com/quotes/random).Content
{"id":1,"quotation":"Yeah, well, that's just like, your opinion ... man.","author":"The Dude"}
Bonus
Use a curl
endless loop to view your results.
PowerShell
While($true) { $(curl http://qotd-rhn-engineering-dschenck-dev.apps.sandbox-m2.ll9k.p1.openshiftapps.com/quotes/random).Content;sleep 1 }
Bash
while $true; do curl http://qotd-rhn-engineering-dschenck-dev.apps.sandbox-m2.ll9k.p1.openshiftapps.com/quotes/random; sleep 1; done;
Developer notes
- The qotd application is written in Go.
Move it, remove it, improve it
Move
If you have another cluster, such as a Red Hat OpenShift Service on AWS instance, you can install the OpenShift Pipelines operator and run this activity there. If you do this, consider creating a project for this activity.
Remove
You can remove all or part of this activity by using one of the following commands:
- To remove only the qotd app:
oc delete all -l learn-pipelines=qotd
- To remove only the pipeline:
oc delete all -l learn-pipelines=pipeline
- To remove only the task:
oc delete all -l learn-pipelines-task
- To remove only the workspace:
oc delete all -l learn-pipelines=workspace
- To remove all of the objects associated with this activity:
oc delete all -l sandbox=learn-pipelines
Improve
The following are ideas to improve this activity:
- Write your qotd app in a different language.
- After building the app using your source code, create an endless loop using
curl
at the command line to get random quotes. Next, change the source code and re-run the pipeline to see the new version implemented. This is a rolling update. - Add a health check to the deployment.
- Read the quotes from the text file that is stored in a PVC.