CI/CD Pipeline Using Groovy Script

CI/CD Pipeline Using Groovy Script

Hello Everyone !

Let’s make the things more Agile …


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

A continuous delivery pipeline is an automated expression of your process for getting software from version control right through to your users and customers.

Jenkins Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines “as code”.


Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control.

Here I used Groovy language to write this file .


Apache Groovy is a Java-syntax-compatible object-oriented programming language for the Java platform. It is both a static and dynamic language with features similar to those of Python, Ruby, and Smalltalk. It can be used as both a programming language and a scripting language for the Java Platform, is compiled to Java virtual machine (JVM) bytecode, and interoperates seamlessly with other Java code and libraries.



Perform third task with the help of Jenkins coding file ( called as jenkinsfile approach ) and perform the with following phases:

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. Job1 : 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. Job2 :

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.

No alt text provided for this image

Dockerfile

No alt text provided for this image

Dockerfile for html code

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.

  • First Job

As the developer pushes the code in the github, this job will automatically copy the code and paste it to my Base OS in /Dev6/code folder.

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

Console Output of Job 1:-

No alt text provided for this image

Now, as soon as job 1 is built successfully, it will automatically trigger Job 2.

JOB2:

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: hemant123/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

JOB3:

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''')
      }    
}

JOB4:

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
No alt text provided for this image


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

Udit Agarwal的更多文章

社区洞察

其他会员也浏览了