OpenShift + Pipelines 2

In this article series, we will set up a CI pipeline to compile and package a JavaScript game application into a Docker image using Jenkins on Red Hat OpenShift. Once we build the image, it will be pushed to the external Red Hat Quay container registry, Quay.io. When the developer pushes the changes into the Git repository, all these actions trigger.

This is a series of complete CI/CD pipelines on OpenShift using the Jenkins and Red Hat Ansible Automation Platform. We will cover the following topics:

This article is based on the assumption that you have basic knowledge of Jenkins, OpenShift, and Ansible Automation Platform. You will also need administrator privileges for your OpenShift cluster.

The CI pipeline architecture

The developer commits and pushes the changes after initiating the action, as shown in the architecture diagram (Figure 1). Jenkins will detect the changes with the help of polling or webhooks. We build the image in the OpenShift cluster and push it to the Quay.io container registry using buildconfig.

A diagram of continuous integration architecture.
Figure 1: A continuous integration architecture diagram.

Install Jenkins on OpenShift

Now we need Jenkins Dashboard, which will run the CI pipeline. The easiest way is to deploy a pod of Jenkins on OpenShift from the developer's catalog, as shown in Figure 2.

A screenshot of the Developer Catalog from where you can install Jenkins.
Figure 2: Install Jenkins from the Developer Catalog.

Follow these six steps to install Jenkins on OpenShift:

  1. From OpenShift Web Console, switch to the Developer perspective and navigate to the Topology view. Click on +Add > From Developer Catalog > All services
  2. Search for Jenkins.
  3. Select the persistence Jenkins and install it (For this article, I am keeping the settings as default, but you can modify the settings as per your requirement).
  4. After installation, one Jenkins pod should appear in the project.
  5. To access the dashboard of Jenkins, click on the route icon.
  6. For Jenkins dashboard access, you can use the OpenShift console credential, as shown in Figure 3.
The Jenkins login screen with OpenShift credentials.
Figure 3: The Jenkins login screen with OpenShift credentials.

Set up Jenkins CI pipeline

The continuous integration stage consists of building and pushing the image into the container registry.

  1. Make sure to have one git repository in place, including the Dockerfile and application dependencies like the requirements.txt file.
  2. Create a Jenkinsfile with the following contents and add this jenkinsfile to the git repository.
pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
        stage("Checkout") {
            steps {
                checkout scm
            }
        }
        stage("Docker Build") {
            steps {
              sh '''
                  #oc start-build --from-build=<build_name>
                  oc start-build -F red-api --from-dir=./api/
              '''
            }
        }
    }
}

 

3. Next, create a BuildConfig file using the following content that will build the source code to executable and push.

apiVersion: build.OpenShift.io/v1
kind: BuildConfig
metadata:
  labels:
    app.kubernetes.io/name: red-api # your application name
  name: red-api # your application name
spec:
  output:
    to:
      kind: DockerImage
      name: ***************** # add yourimage
  source:
    # Expect a local directory to be streamed to OpenShift as a build source
    type: Binary
    binary: {}
  strategy:
    type: Docker
    dockerStrategy:
      # Find the image build instructions in./Dockerfile
      dockerfilePath: Dockerfile

Ex: name: quay.io/<username>/cd:latest

4. Next, create the secrets which will help our build config to push our recently built image in the container registry.

5. To create a secret, type the following command into your terminal and make sure to use the username and password of your environment. For this exercise, we are using a Quay.io container registry.

$ oc create secret docker-registry my-secret --docker-server=quay.io --docker-username=xxxx  --docker-password=xxx

$ oc secrets link builder my-secret --for=mount

6. Next, we will create a pipeline from the Jenkins dashboard (Figure 4).

A screenshot of the Jenkins dashboard.
Figure 4: The Jenkins dashboard​​​​​ after installation.

7. Select Pipeline from New Item and give a name to that pipeline.

8. Select the Build Triggers when the changes are pushed in the GitHub repository.

9. In the pipeline, select Pipeline Script from SCM.

10. Fill in the details according to the snapshot shown in Figure 5 for the config we're using for this article.

Ansible Automation Platform integrate with jenkins pipeline​​​​​​.
Figure 5: Integrate Ansible Automation Platform with Jenkins pipeline​​​​​​.

11. Once the pipeline is ready, execute it by clicking the Build Now button. You can see a glimpse of the pipeline in Figure 6.

A screenshot showing the completed CI stage and the CI pipeline running in the Jenkins dashboard.
Figure 6: The CI pipeline running in the Jenkins dashboard.

What’s Next?

The next article is based on the continuous deployment using the Ansible Automation platform on the OpenShift cluster. You will learn how to install the Ansible Automation platform using the operator’s hub on OpenShift and also the integration of Jenkins and Ansible Automation platform. Check out a demo of this project in DevNation2022.

Last updated: January 31, 2024