CREATING JENKINS PIPELINE USING GROOVY ( INTEGRATION WITH GIT ,  GITHUB AND KUBERNETES)

CREATING JENKINS PIPELINE USING GROOVY ( INTEGRATION WITH GIT , GITHUB AND KUBERNETES)

What is Git:

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git is easy to learn and has a tiny footprint with lightning-fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and Clear Case with features like cheap local branching, convenient staging areas, and multiple workflows.

No alt text provided for this image

What is GitHub:

GitHub is a code hosting platform for collaboration and version control.

GitHub lets you (and others) work together on projects.

At a high level, GitHub is a website and cloud-based service that helps developers store and manage their code, as well as track and control changes to their code. To understand exactly what GitHub is, you need to know two connected principles:

  • Version control
  • Git
No alt text provided for this image

What is Jenkins:

Jenkins is a self-contained, open-source automation server that can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software.

Jenkins is a CI/CD tool.

Jenkins can be installed through native system packages, Docker, or even run standalone by any machine with a Java Runtime Environment (JRE) installed.

No alt text provided for this image

Groovy Script:

Apache Groovy is an object-oriented and Java syntax compatible programming language built for the Java platform.

Groovy can be used as a scripting language for the Java platform. It is almost like a super version of Java which offers Java’s enterprise capabilities.

No alt text provided for this image

What is Kubernetes:

Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.

It groups containers that make up an application into logical units for easy management and discovery. Kubernetes builds upon 15 years of experience of running production workloads at Google, combined with best-of-breed ideas and practices from the community.

No alt text provided for this image

What is Jenkins Pipeline ?

Jenkins Pipeline (or simply “Pipeline” with a capital “P”) is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.

continuous delivery (CD) pipeline is an automated expression of your process for getting software from version control right through to your users and customers. Every change to your software (committed in source control) goes through a complex process on its way to being released. This process involves building the software in a reliable and repeatable manner, as well as progressing the built software (called a “build”) through multiple stages of testing and deployment.

Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines “as code” via the pipeline domain specific language (dsl) syntax.

The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a project’s source control repository. This is the foundation of “Pipeline-as-code”; treating the CD pipeline a part of the application to be versioned and reviewed like any other code.

Creating a Jenkinsfile and committing it to source control provides a number of immediate benefits:

  • Automatically creates a Pipeline build process for all branches and pull requests.
  • Code review/iteration on the Pipeline (along with the remaining source code).
  • Audit trail for the Pipeline.
  • Single source of truth for the Pipeline, which can be viewed and edited by multiple members of the project.

While the syntax for defining a Pipeline, either in the web UI or with a Jenkinsfile is the same, it is generally considered best practice to define the Pipeline in a Jenkinsfile and check that in to source control.

No alt text provided for this image


Problem Statement :

1. Create container image that’s has Jenkins installed using dockerfile Or You can use the Jenkins Server on RHEL 8/7.

2. When we launch this image, it should automatically starts Jenkins service in the container.

3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins.

4. Job2 ( Seed Job ) : Pull the Github repo automatically when some developers push repo to Github.

5. Further on jobs should be pipeline using written code using Groovy language by the developer

6. 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 ).

7. Job3 : Test your app if it is working or not.

8. 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.

First of all we have to create the docker image which jenkins as well as kubectl configured.

Dockerfile:

No alt text provided for this image

Dockerfile for html code:

No alt text provided for this image

Here we have the genrate the script using Groovy language. In this particular task we don’t need to configure every job of jenkins we have to only create the job and seed job automatically configure all the jobs according to the script of Groovy language.Before did this we have to install some plugin of jenkins which are helpful in running the script. This is the concept Self-Service means in this system developer write the program code as well as write the code for jenkins jobs . This is the real meaning of DevOps in which some work of operational team done by the developer team.

We have to download the following Jenkins plugins:

  1. PostBuildScript
  2. Job DSL

Here we have the seed job generate_job.

No alt text provided for this image
No alt text provided for this image

GROOVY LANGUAGE SCRIPT FOR CONFIGURE ALL THE JOB :

JOB 1 :

This job is triggered when developer push some the repo/code from Git to Github then jenkins automatically download the code. In the job1 we use poll scm to see the code of Github if somethink is changed then it trigger the job1.

job("task6_job1") {
  description("Download the data from github")
               scm {
                       github('nikhil/devopstask6')
                    }
               triggers {
                       scm(" * * * * * ")
                    }
               steps {
                       shell("sudo  cp  -rvf   *  /root/devops_task6")
                    }

}

JOB 2 :

In this job we check the code file extension and according to the code type Jenkins should automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes. Expose your pod so that testing team could perform the testing on the pod. Also make the data to remain persistent ( If server collects some data like apache logs).

job("task6_job2") {
     description("checking the code and launching the pods")
     triggers {
                 upstream('task6_job1' , 'SUCCESS')
        }
      steps {
             shell('''sudo python3  /root/devops_task6/extension.py
             sleep 60''')
         }

}

Deployment for code by using yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
    name: myweb-deploy
    labels:
         app: webapp
spec:
    selector:
         matchLabels:
             app: webapp
             env: production
    template:
         metadata:
              name: myweb-pod
              labels:   
                  env: production
                  app: webapp
         spec:
              containers:
              -   name: myweb-con
                  image: nikhil/myweb:v1
                  ports:
                  -    containerPort: 80
                  volumeMounts:
                  -    mountPath: "/usr/local/apache2/htdocs"
                       name: jenkins-pvc
              volumes:
              -   name: jenkins-pvc
                  persistentVolumeClaim:
                       claimName: pv-claim-jenkins


Exposing the service by using yaml file:

apiVersion: v1
kind: Service
metadata:
     name: myweblb
spec:
     selector:
       app: webapp
     type: NodePort
     ports:
     -    port: 80
          targetPort: 80

         
          nodePort: 31000

Create PVC for storing the logs of apache web server by yaml file:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: pv-claim-jenkins
    labels:
         app: webapp
spec:
    accessModes:
     -  ReadWriteOnce
    resources:
        requests:

        
          storage: 1Gi

JOB 3 :

In this job we check the status code of the code.Mainly it is the testing job.

job("task6_job3") {
      description("Testing the code by seeing its status code")
      triggers {
                upstream('task6_job2' , 'SUCCESS')
         }
      steps {
             shell('''status1=$(curl  -s -o  /dev/null  -sw  "%{http_code}"             
                           https://192.168.99.108:31000/index.html)
             if [ $status1  == '200' ]
             then
                  echo "webpage code  is  good"
                  exit 0
             else
                  echo "webpage code is not good"
                  exit 0
             fi''')
      }    

}

JOB 4 :

If the code is not running properly then this job send the mail to the developer and remove all the pods , services as well as persistent volume.

job("task6_job4") {
      description("Sending Email if error occur in the code otherwise not")
      triggers {
                  upstream('task6_job3' , 'SUCCESS')
            }
      steps {
                  shell('''if [ $(curl -s -o /dev/null -w "%{http_code}"   
                         192.168.99.108:31000/test.html) == '200' ]
                  then
                      echo "code is good"
                      exit 0
                  else
                      echo "code is not right"
                      sudo python3 /root/devops_task6/sendemail.py
                      sudo cd /root/devops_task6/
                      sudo rm -f *
                      sudo kubectl delete all --all
                      sudo kubectl delete pvc --all
                      exit 0
                 fi''')
            }
   
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Thank you for watching this article.

No alt text provided for this image


要查看或添加评论,请登录

社区洞察

其他会员也浏览了