Modern CI/CD Using Python+Gunicorn+NGINX+Jenkins Pipeline to Openshift (Part Three)

This blog is the third one of three-parts series regarding CI/CD of Python Application using Gunicorn, NGINX, Jenkins Pipeline to Openshift. In series, we will be explaining:

  1. Part One: We will explain about deployment structure and preparations.
  2. Part Two: We will write the Jenkins Pipeline.
  3. Part Three: Deploy using Jenkins Pipeline to Openshift and Webhook implementation.

In previous post, we have defined our pipeline. Now, we are going to discuss about how to use this pipeline.

Creating Python Jenkins Slave

Most important step before we start using pipeline is to create a Jenkins Slave. For our python execution, we need a python slave to execute step 1-4 described in previous blog. There is two ways to do that:

  • Way One:

    1. Go to Manage Jenkins(URL: Jenkins_Url/manage)
    2. Click on Configure System
      Screen-Shot-2018-08-12-at-2.18.12-AM
    3. Then go to bottom of settings, under Kubernetes Pod Template. Then click on Add Pod Template.
      Screen-Shot-2018-08-12-at-2.19.38-AM
    4. Now add configuration given as the following image:
      Screen-Shot-2018-08-12-at-2.18.49-AM
      If the image is not clear enough then please use the following dictionary:
    {
        Name: python
        Labels: python
        // click on add container
        Container Template: {
            Name: jnlp
            Docker Image: idealo/jenkins-slave-python-centos7:latest
            Always pull image: True
            Working directory: /tmp
            Arguments to pass to the command: ${computer.jnlpmac}${computer.name}
         }
    }
    
    1. Now save this configuration by clicking on Save button.
  • Way Two:
    Use this command from idealo's repository:

    oc create -f https://raw.githubusercontent.com/idealo/jenkins-ci/master/config-map.yaml
    

    Or check here and follow their instructions.

By the way, there are other jenkins python slaves available in github or other opensource repository. Feel free to use any of your choice. I am using this repository, for this demo purpose.

Create Build Config

We need to import our pipeline to openshift. For that we need to create a Build Config for that. Let's do that:

apiVersion: v1
kind: BuildConfig
metadata:
    labels:
        app: python-nginx-pipeline
    name: python-nginx-pipeline
spec:
    source:
        git:
            ref: master
            uri: <your git repository link>
    strategy:
        jenkinsPipelineStrategy:
            jenkinsfilePath: path/to/Jenkinsfile
        type: JenkinsPipeline
    triggers:
        - github:
            secret: secret
          type: GitHub
        - generic:
            secret: secret
          type: Generic
        - bitbucket:
            secret: secret
          type: Bitbucket

We can save this as jenkins-pipeline.yaml. You can import this using:

oc project cicd
oc create -f jenkins-pipeline.yaml

Now, if you go to web interface of Openshift and access in url path OPENSHIFT_URL > console > CI/CD project > browse > pipelines, you should see a new pipeline has been created. You can import the build config directly in openshift web interface as well.
Screenshot2

Running Pipeline

You can run the pipeline using the Start Pipeline on top right corner of pipeline page. Or you can do that using command line:

oc start-build python-nginx-pipeline

Outcome

Here comes the sweet results of your hard work. You should see the pipeline executions like this:

Before STAGE Deployment Prompt

Screen-Shot-2018-08-12-at-3.26.14-AM

Full Pipeline Execution

Screen-Shot-2018-08-12-at-3.27.45-AM

Blue Ocean Pipeline

If you go to Blue Ocean(comes build-in) of Jenkins, then go to your latest build, you should see a beautiful pipeline looks like this:
Screen-Shot-2018-08-12-at-3.29.10-AM

Also if you click on tests, then you should see the recorded result of tests:
Screen-Shot-2018-08-12-at-3.29.31-AM

In Artifacts section, you will see pipeline logs and the artifact file which you have stored in Step 4 of previous post:
Screen-Shot-2018-08-12-at-3.29.38-AM

Python+NGINX Running in DEV and STAGE Project

You should see your server is up and running in DEV and STAGE Project.
Screen-Shot-2018-08-12-at-12.00.08-PM

Automated Deployment

Last part of our implementation is automated deployment. For that we are going to use Webhook. It will allow us to start a build based on an event in git repository. For starting a build, we need to define triggers in Pipeline Build Config. We have defined triggers in our build config. Now we need to copy that trigger link from here:
Screenshot3

You can paste this webhook in your repository's webhook settings, for example like this:

BitBucket:

Screen-Shot-2018-08-13-at-9.33.01-PM

Github:

Screen-Shot-2018-08-13-at-9.37.32-PM

Then for any event(as per your configuration, like push, pull request merge etc) it will trigger a new build. FYI: this functionality will not work for minishift.

Working Example

You will find a working example available at here

Useful Resources

  1. If you want to implement Django Using Pipeline, checkout here.
  2. Read details about Jenkins Python Slave in here.
  3. Read about running NGINX on Openshift here.
  4. Read about CI/CD demo by RedHat in here.
  5. For Python2.7 based deployment use this Jenkins Slave.

Thank you for reading. Please let me know your feedback by commenting below.

Cheers!!


Arnab Kumar Shil

Love coding, traveling, anime, cycling, ping pong, motorbiking, and humor.

Dhaka, Bangladesh
https://ruddra.com

Comments

comments powered by Disqus