Create a CI/CD pipeline to test and deploy an application.
Outcomes
Use custom and cluster tasks.
Create a CI/CD pipeline to test, build, and deploy an application.
On the workstation machine, use the lab command to prepare
your environment for this exercise, and to ensure that all required resources are available.
[student@workstation ~]$ lab start compreview-cicd
Specifications
You must create a pipeline that tests, builds, and deploys the words Node.js application.
Create a pipeline with the following tasks:
fetch-repository: Clones the repository.
npm-install: Installs the dependencies of the Node.js project.
npm-test: Runs the tests.
npm-lint: Runs the code linter.
build-push-image: Builds the container image and pushes the image to the OpenShift registry.
oc-deploy: Deploys the container image.
Create the pipeline workflow as the following screenshot displays.
Note that the npm-test and npm-lint tasks must run in parallel:

Use the compreview-cicd OpenShift project for this exercise.
The files required for the exercise are in the ~/DO288/labs/compreview-cicd directory.
The solutions are in the ~/DO288/solutions/compreview-cicd directory.
The source code of the words application is available at https://git.ocp4.example.com/developer/DO288-apps/ in the apps/compreview-cicd/words path.
Make no changes to this code.
The application includes a Containerfile that uses registry.ocp4.example.com:8443/ubi8/nodejs-16:latest as the base image.
To prevent permission problems in the build-push-image task, make this image public.
You can make the image public in the image settings page of the Quay classroom registry.
Use developer both as the user and password to log in to Quay.
The application contains a kubefiles/app.yaml that defines a deployment, a service, and a route.
Use this file in the oc-deploy task to deploy the application.
The npm-install, npm-test, and npm-lint tasks must use the npm custom task.
You can create this task from the /home/student/DO288/labs/compreview-cicd/npm-task.yaml file.
To use the task, pass the following parameters:
The ARGS parameter defines the arguments to pass to the npm command.
The CONTEXT parameter defines the context directory where the task runs the npm command.
Complete the /home/student/DO288/labs/compreview-cicd/pipeline.yaml file to define the pipeline.
Initially, the YAML file includes the definition of the pipeline parameters and the workspace name.
Use the following specifications to define the fetch-repository task:
Use the git-clone cluster task.
Pass the Git URL parameter from the pipeline inputs to the task.
Pass the Git revision parameter from the pipeline inputs to the tasks.
Configure the task to use the shared workspace.
Use the following specifications to define the npm-install task:
Use the npm custom task.
Configure the task workspace to have access to the application cloned in the fetch-repository task.
Configure the task to use APP_PATH pipeline parameter as the context.
Pass the following arguments to the task:
install --no-package-lock
Use the following specifications to define the npm-test task:
Use the npm custom task.
Configure the task workspace to have access to the application cloned in the fetch-repository task.
Configure the task to use APP_PATH pipeline parameter as the context.
Pass the following arguments to the task:
test
Use the following specifications to define the npm-lint task:
Use the npm custom task.
Configure the task workspace to have access to the application cloned in the fetch-repository task.
Configure the task to use APP_PATH pipeline parameter as the context.
Pass the following arguments to the task:
run lint
Use the following specifications to define the build-push-image task:
Use the buildah cluster task.
Configure the task workspace to have access to the application cloned in the fetch-repository task.
Configure the task to use $(params.IMAGE_REGISTRY)/$(context.pipelineRun.namespace)/$(params.IMAGE_NAME):$(context.pipelineRun.uid) as the image name.
Configure the task to use ./Containerfile as the Dockerfile path.
Configure the task to use APP_PATH pipeline parameter as the context.
Use the following specifications to define the oc-deploy task:
Use the openshift-client cluster task.
Configure the task workspace to have access to the application cloned in the fetch-repository task.
Configure the task to execute the following script.
The script uses the app.yaml template file, which defines a deployment, a service, and a route for the application.
oc process -f $(params.APP_PATH)/kubefiles/app.yaml \ -p IMAGE_NAME=$(params.IMAGE_REGISTRY)/$(context.pipelineRun.namespace)/$(params.IMAGE_NAME):$(context.pipelineRun.uid) \ | oc apply -f -
Use the following configuration when you run the pipeline:
APP_PATH parameter: apps/compreview-cicd/words.
Storage for the shared workspace: Use /home/student/DO288/labs/compreview-cicd/volume-template.yaml as the volume template file.
If you use the tkn CLI, then you can use the --use-param-defaults flag so that the CLI uses default values for the other parameters.
You can verify the deployed application by sending an HTTP GET request to http://words-compreview-cicd.apps.ocp4.example.com/word/hello.
The response must be similar to the following object:
{
"input": "hello",
"palidrome": false,
"length": 5
}Make the Node.js 16 image public.
Open the web browser and navigate to https://registry.ocp4.example.com:8443.
Log in to Quay with the developer user and the developer password.
Search for the ubi8/nodejs-16 repository.
In the left pane, click the icon.
Scroll down to the section and click . Click to confirm the operation.
Log in to OpenShift as the developer user and switch to the compreview-cicd project.
Log in as the developer user.
[student@workstation ~]$ oc login -u developer -p developer \
https://api.ocp4.example.com:6443
Login successful.
...output omitted...Ensure that you are on the compreview-cicd project.
[student@workstation ~]$ oc project compreview-cicd
Already on project "compreview-cicd" on server "https://api.ocp4.example.com:6443".Create the custom task for running NPM commands.
Change to the lab directory.
[student@workstation ~]$ cd ~/DO288/labs/compreview-cicdApply the npm-task.yaml file to create the npm task.
[student@workstation compreview-cicd]$ oc apply -f npm-task.yaml
task.tekton.dev/npm createdCreate the pipeline.
Open the /home/student/DO288/labs/compreview-cicd/pipeline.yaml with your editor of choice.
Configure the fetch-repository task as follows:
- name: fetch-repository
taskRef:
name: git-clone
kind: ClusterTask
params:
- name: url
value: $(params.GIT_REPO)
- name: revision
value: $(params.GIT_REVISION)
- name: deleteExisting
value: "true"
- name: sslVerify
value: "false"
workspaces:
- name: output
workspace: sharedConfigure the npm-install task as follows:
- name: npm-install
taskRef:
name: npm
kind: Task
workspaces:
- name: source
workspace: shared
params:
- name: CONTEXT
value: $(params.APP_PATH)
- name: ARGS
value: install --no-package-lock
runAfter:
- fetch-repositoryConfigure the npm-test task as follows:
- name: npm-test
taskRef:
name: npm
kind: Task
workspaces:
- name: source
workspace: shared
params:
- name: CONTEXT
value: $(params.APP_PATH)
- name: ARGS
value: test
runAfter:
- npm-installConfigure the npm-lint task as follows:
- name: npm-lint
taskRef:
name: npm
kind: Task
workspaces:
- name: source
workspace: shared
params:
- name: CONTEXT
value: $(params.APP_PATH)
- name: ARGS
value: run lint
runAfter:
- npm-installConfigure the build-push-image task as follows:
- name: build-push-image
taskRef:
name: buildah
kind: ClusterTask
params:
- name: IMAGE
value: $(params.IMAGE_REGISTRY)/$(context.pipelineRun.namespace)/$(params.IMAGE_NAME):$(context.pipelineRun.uid)
- name: DOCKERFILE
value: ./Containerfile
- name: CONTEXT
value: $(params.APP_PATH)
workspaces:
- name: source
workspace: shared
runAfter:
- npm-test
- npm-lintConfigure the oc-deploy task as follows:
- name: oc-deploy
runAfter:
- build-push-image
taskRef:
name: openshift-client
kind: ClusterTask
workspaces:
- name: manifest-dir
workspace: shared
params:
- name: SCRIPT
value: |
oc process -f $(params.APP_PATH)/kubefiles/app.yaml \
-p IMAGE_NAME=$(params.IMAGE_REGISTRY)/$(context.pipelineRun.namespace)/$(params.IMAGE_NAME):$(context.pipelineRun.uid) \
| oc apply -f -Create the pipeline.
[student@workstation compreview-cicd]$ oc apply -f pipeline.yaml
pipeline.tekton.dev/words-cicd-pipeline createdRun the pipeline.
[student@workstation compreview-cicd]$ tkn pipeline start \
--use-param-defaults words-cicd-pipeline \
-p APP_PATH=apps/compreview-cicd/words \
-w name=shared,volumeClaimTemplateFile=volume-template.yaml
PipelineRun started: words-cicd-pipeline-run-5p6s6
In order to track the PipelineRun progress run:
tkn pipelinerun logs words-cicd-pipeline-run-5p6s6 -f -n compreview-cicdVerify the pipeline logs.
Find the name of the pipeline run.
[student@workstation compreview-cicd]$tkn pr listNAME STARTED DURATION STATUSwords-cicd-pipeline-run-5p6s652 seconds ago --- Running
View the logs of the pipeline.
[student@workstation compreview-cicd]$ tkn pr logs -f PIPELINE_RUN_NAME
[fetch-repository : clone] + '[' false = true ']'
...output omitted...
[oc-deploy : oc] deployment.apps/words created
[oc-deploy : oc] service/words created
[oc-deploy : oc] route.route.openshift.io/words createdVerify that the application is online. Send an HTTP request.
[student@workstation compreview-cicd]$ curl -s \
http://words-compreview-cicd.apps.ocp4.example.com/word/hello | jq
{
"input": "hello",
"palidrome": false,
"length": 5
}