Setting up CI/CD Pipeline in Openshift

For deploying an application using CI/CD using Jenkins and Openshift, first of all we need an application that we want to deploy to various environment.

NodeJS Application

Writing Docker File

Next step woudl be setting up Dockerfile.

Preparing Openshift

Create three projects dev, state for the demo purpose.

Create a project named ci, where we will run jenkins

oc new-project dev
oc new-project stage
oc new-project ci

Preparing Jenkins

oc project ci
oc new-app jenkins-persistent
oc status
oc expose svc/jenkins-persistant

Now we need to give jenkins permission to make changes in dev and stage projects

oc policy add-role-to-user edit system:serviceaccount:ci:jenkins -n dev
oc policy add-role-to-user edit system:serviceaccount:ci:jenkins -n stage

Writing Jenkins Pipeline

Next we need to define our pipeline. In our case it will be Jenkins Pipeline

Creating Build Config

// jenkins-pipeline.yaml

apiVersion: v1
kind: BuildConfig
metadata:
  labels:
    app: hello-world-pipeline
  name: hello-world-pipeline
spec:
  source:
    git:
      ref: master
      uri: https://github.com/arttuladhar/hello-node.git
  strategy:
    jenkinsPipelineStrategy:
      jenkinsfilePath: Jenkinsfile
    type: JenkinsPipeline
  triggers:
    - github:
        secret: secret
      type: GitHub
    - generic:
        secret: secret
      type: Generic

Creating Pipeline

# Creating Pipeline
oc create -f openshift/jenkins-pipeline.yaml

# Starting Pipeline Build
oc start-build hello-world-pipeline

At this point you should see a running build.

Reference

  • https://ruddra.com/posts/openshift-python-gunicorn-nginx-jenkins-pipelines-part-one/