CI/CD Pipeline with Jenkins for WebServer Development (using Kubernetes) with Groovy Language

Logo

Hello Everyone,

In this blog we are going to setup a CI/CD pipeline for Web Server development using Jenkins with Groovy language. Here we age going to use Jenkinsfile to run other jobs in order to create the pipeline using Groovy language.

Problem Statement:

  1. When we launch this image, it should automatically starts Jenkins service in the container.
  2. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
  3. Job2 ( Seed Job ) : Pull the Github repo automatically when some developers push repo to Github.
  4. Further on jobs should be pipeline using written code using Groovy language by the developer
  5. Job1 :
    1). By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed )
    2). Expose your pod so that testing team could perform the testing on the pod
    3). Make the data to remain persistent using PVC ( If server collects some data like logs, other user information )
  6. Job3 : Test your app if it is working or not.
  7. Job4 : if app is not working , then send email to developer with error messages and redeploy the application after code is being edited by the developer.
Architecture

Lets start with Seed job Job 0 , that will run all other jobs or to initiate all other Jobs.

Subsequent jobs should be performed via JenkinsFile’s Groovy Code.

We will create the solution, Step-by-step :

Step 1: Create Dockerfile for Jenkins-Kubernetes Image.

For this create a separate directory i.e., Task06(in my case).

Creating separate directory

Create Dockerfile for Jenkins-Kubernetes Image.

Command to create Dockerfile

Specs : Base Image|Software Dependencies : wget, git, httpd, python3, net-tools, java open-jdk, Open-ssh| Jenkins : Installation, access, start jenkins service | Kubectl : installation, access, Creating directory and copying all credentials into it| Exposing Port 8080 : Default port for jenkins service

Dockerfile for jenkins-kubernetes image

Build the Image via this dockerfile :

Creating jenkins-kubernetes image

After some minutes, our Docker image will be ready.

Step 2: Dockerfile for HTML and PHP Software Images.

(To be uploaded on github by Developer later on)

  1. For HTML
Dockerfile for HTML

2. For PHP

Dockerfile for PHP

Step 3: Creating Persistent Volume Claim (PVC) and Deployment for HTML

  1. PVC
code for PVC

2. Deployment

code for Deployment

Step 4: Creating Persistent Volume Claim (PVC) and Deployment for PHP

  1. PVC
code for PHP pvc

2. Deployment

code for PHP deployment

Step 5: Create Service

This service will expose the deployment on some port.

Code for creating service

Step 6: Launch a Container from this image and it should automatically start the Jenkins service in the container.

Run the command below to launch the jenkins container image:

docker run -it -v  -p 808:8080 --name jenkinsos jenkinskubernetes:latest

Now try to access jenkins at 808 port of IP of the VM.

In my case, IP : 192.168.99.102

On accessing,it will ask for Authentication password , and that password we need to copy from the screen of the VM.

Jenkins Dashboard

Step 7: Creating the Seed job

  • Installing DSL plugin.
Installing DSL plugin
  • Creating a job , build script having jenkinsfile script written in Groovy language.
Creating job-0

With the code below :

code for jenkinsfile
Code for jenkinsfile(cont.)
Build using jenkinsfile

As soon as we run this job, it will deploy other four jobs mentioned in the Groovy script.

All deployed Jobs

Now, when we run Job-1 all the jobs will be triggered which are in the pipeline.

Hence, we have successfully created a CI/CD pipeline for Webserver Deployment using Groovy Language. Feel free to reach, if any query.

Thank You!!

I am a tech enthusiast, researcher and work for integrations. I love to explore and learn about the new technologies and their right concepts from its core.