CI/CD Pipeline with Jenkins for WebServer Development (using Kubernetes) with Groovy Language
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:
- Create container image that’s has Jenkins installed using dockerfile Or You can use the Jenkins Server on RHEL 8/7
- When we launch this image, it should automatically starts Jenkins service in the container.
- Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
- Job2 ( Seed Job ) : Pull the Github repo automatically when some developers push repo to Github.
- Further on jobs should be pipeline using written code using Groovy language by the developer
- 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 ) - Job3 : Test your app if it is working or not.
- 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.
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).
Create Dockerfile for Jenkins-Kubernetes Image.
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
Build the Image via this dockerfile :
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)
- For HTML
2. For PHP
Step 3: Creating Persistent Volume Claim (PVC) and Deployment for HTML
- PVC
2. Deployment
Step 4: Creating Persistent Volume Claim (PVC) and Deployment for PHP
- PVC
2. Deployment
Step 5: Create Service
This service will expose the deployment on some port.
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.
Step 7: Creating the Seed job
- Installing DSL plugin.
- Creating a job , build script having jenkinsfile script written in Groovy language.
With the code below :
As soon as we run this job, it will deploy other four jobs mentioned in the Groovy script.
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!!