In this article series, we will set up a continuous integration (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:
- Part 1: Continuous integration with Jenkins on OpenShift
- Part 2: Continuous deployment using Ansible Automation Platform on OpenShift
- Part 3: How a manual intervention pipeline restricts deployment
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.
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.
Follow these six steps to install Jenkins on OpenShift:
- From OpenShift Web Console, switch to the Developer perspective and navigate to the Topology view. Click on +Add > From Developer Catalog > All services
- Search for Jenkins.
- 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).
- After installation, one Jenkins pod should appear in the project.
- To access the dashboard of Jenkins, click on the route icon.
- For Jenkins dashboard access, you can use the OpenShift console credential, as shown in Figure 3.
Set up Jenkins CI pipeline
The continuous integration stage consists of building and pushing the image into the container registry.
- Make sure to have one git repository in place, including the Dockerfile and application dependencies like the requirements.txt file.
- 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).
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.
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.
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: September 27, 2024