Building a CD process using the OpenShift Pipelines
The sample application for deployment is written in Python, the only task that will not be taken from the OpenShift Pipelines' catalog and written manually instead is the task named "nose" for unit-testing the code.
1. Preparation
To install the "nose" task, just copy the following code block for the task, save it into a file named tasks.yaml
---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: nose
spec:
workspaces:
- name: source
params:
- name: args
description: Arguments to pass to nose
type: string
default: "-v"
steps:
- name: nosetests
image: python:3.9-slim
workingDir: $(workspaces.source.path)
script: |
#!/bin/bash
set -e
python -m pip install --upgrade pip wheel
pip install -r requirements.txt
nosetests $(params.args)
kubectl apply -f tasks.yaml
oc get tasks
2. Creating PersistentVolumeClaim
The PersistentVolumeClaim (PVC) will be use as a workspace. Use the OpenShift Administrator section to create the PVC.In the main menu:
- switch from Developer to Administrator
- go to Storage -> PersistentVolumeClaims
Now, in the PersistentVolumeClaims page:
- press the Create PersistentVolumeClaim button
-
choose the appropriate StorageClass from the dropdown
(skills-network-learner in my case) - in the PersistentVolumeClaim name type ccd-openshift-pipeline-pvc
- for Access mode choose Single user (RWO) radio button
- Size, set to 1 GiB
- for Volume mode choose Filesystem radio button
- press the Create button
3. Creating a new Pipeline
In the main menu:
- switch from Administrator back to Developer
- go to Pipelines
Now, in the Pipelines page:
-
press the Create button
(make sure that you have Pipeline and not Repository selected under the button) -
((You are presented with the pipeline builder. Ensure you have Pipeline Builder selected in Configure via))
In the Name field enter cd-openshift-pipeline - Scroll towards the bottom of the page to the Parameters section
-
Add the name parameter with its default value by entering the following:
parameter.name: app-name parameter.default: cicd-app.
-
Add the build-image parameter:
replacing CONTAINER_REGISTRY_NAMESPACE with the value appropriate for your setup
((to look up my value: echo $SN_ICR_NAMESPACE))parameter.name: build-image parameter.default: image-registry.openshift-image-registry.svc:5000/CONTAINER_REGISTRY_NAMESPACE/tekton-lab:latest
- scroll farther towards the bottom of the page to the Workspaces section
- add a new workspace with the name: output (it will be used to upload the code to and work on it).
- Press the Create button to save your work
4. Adding tasks to the Pipeline
Now when the groundwork on building the pipeline is done, let's add the tasks to it.
Open the pipeline in edit mode (select Pipeline from the main menu, select the pipeline name, and then go to Actions -> Edit Pipeline)
1 - Adding the cleanup task
- Click Add Task in the builder UI to open the Add task ... dialog.
- type cleanup to see the cleanup task by Red Hat.
- press the Add button to start using the task
2 - Adding the git-clone task
- Hover over the step to display the + buttons; use the + button on the right of the task to add a task
-
type RedHat git-clone and add the task by pressing the Add button
(the red exclaimation mark on the task means that the task should be configured) - to configure the task, press on the exclaimation mark
-
then fill the following fields in the open flyout:
url: https://github.com/ArthurPro123/wtecc-CICD_PracticeCode workspace.output: output
3 - Adding the Flask8 task (to lint the source code)
- use the + button on the right of the git-clone task to add this one
-
type Flask8 (from the community) and add the task by pressing the Add button
- press on the exclaimation mark
-
fill the following fields to configure the Flake8 task:
image: python:3.9-slim arg: --count arg: --max-complexity=10 arg: --max-line-length=127 workspace.source: output
4 - Adding the nose task (for unit testing the source code)
- use the + button on the BOTTOM of the Flask8 task
-
type nose and add the task by pressing the Add button
- press on the exclaimation mark
-
fill the following field to configure this custom task:
workspace.source: output
5 - Adding the buildah task
- use the + button on the left of the Flask8 task to add this one
-
type RedHat buildah (from the RedHat) and add the task by pressing the Add button
- press on the exclaimation mark
-
fill the following fields to configure the buildah task:
image: $(params.build-image) workspace.source: output
6 - Adding a task to deploy the image to the OpenShift cluster
- use the + button on the right of the git-clone task to add this one
-
type openshift-client (from the RedHat) and add the task by pressing the Add button
- press on the exclaimation mark
-
fill the following fields to configure the openshift-client task:
display name: deploy SCRIPT: oc create deployment $(params.app-name) --image=$(params.build-image) --dry-run=client -o yaml | oc apply -f -
Running the pipeline
To run the pipeline manually, you should be on the pipelines page (if you're not, press the Pipelines button in the main menu. There, press on cd-openshift-pipeline pipeline, then click the Actions dropdown, and the
This will display the Start Pipeline modal window.
Make sure that the following options are chosen:
output: PersistentVolumeClaim PVC: cd-openshift-pipeline-pvc
And finally, press the
Check the logs to see if there are issues with the pipeline.
((You should see the pipeline running on the next page. You can click on the task name to see the logs for a particular task. Alternatively, you can click on the Logs tab:))
Confirming the deployment of the application
Click on Topology in the main menu (available in in the Developer section). There should see the applications which are currently running.
Click on the one called cicd-app??? and click on View logs(Logs)??? in the flyout.If everything worked well, there should be a message SERVICERUNNING displayed, this message was generated by the Python code deployed (for demonstation purposes) and has nothing to to with OpenShift.
