Bookmark this page

Lab: Implementing Continuous Integration and Deployment with OpenShift Pipelines

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
    }
  1. Make the Node.js 16 image public.

    1. Open the web browser and navigate to https://registry.ocp4.example.com:8443.

    2. Log in to Quay with the developer user and the developer password.

    3. Search for the ubi8/nodejs-16 repository.

    4. In the left pane, click the Settings icon.

    5. Scroll down to the Repository Visibility section and click Make Public. Click OK to confirm the operation.

  2. Log in to OpenShift as the developer user and switch to the compreview-cicd project.

    1. Log in as the developer user.

      [student@workstation ~]$ oc login -u developer -p developer \
        https://api.ocp4.example.com:6443
      Login successful.
      
      ...output omitted...
    2. 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".
  3. Create the custom task for running NPM commands.

    1. Change to the lab directory.

      [student@workstation ~]$ cd ~/DO288/labs/compreview-cicd
    2. Apply the npm-task.yaml file to create the npm task.

      [student@workstation compreview-cicd]$ oc apply -f npm-task.yaml
      task.tekton.dev/npm created
  4. Create the pipeline.

    1. Open the /home/student/DO288/labs/compreview-cicd/pipeline.yaml with your editor of choice.

    2. 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: shared
    3. Configure 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-repository
    4. Configure 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-install
    5. Configure 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-install
    6. Configure 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-lint
    7. Configure 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 -
    8. Create the pipeline.

      [student@workstation compreview-cicd]$ oc apply -f pipeline.yaml
      pipeline.tekton.dev/words-cicd-pipeline created
  5. Run 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-cicd
  6. Verify the pipeline logs.

    1. Find the name of the pipeline run.

      [student@workstation compreview-cicd]$ tkn pr list
      NAME                            STARTED          DURATION   STATUS
      words-cicd-pipeline-run-5p6s6   52 seconds ago   ---        Running
    2. 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 created
  7. Verify 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
    }

Evaluation

As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.

[student@workstation ~]$ lab grade compreview-cicd

Finish

As the student user on the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish compreview-cicd
Revision: do288-4.12-0d49506