Automation Deployment With Groovy Script

Automation Deployment With Groovy Script


??Task-6??


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

Prerequisite:

  • Docker installed
  • K8s setup in your system
  • kubectl configured.

Step-1)Make a jenkins Container

  • Here i am using Dockerfile for jenkins conatiner.
  • Code
FROM centos
	RUN yum install wget -y
	RUN wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
	RUN rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
	RUN yum install java-11-openjdk.x86_64 -y
	RUN yum install jenkins -y
	RUN yum install -y openssh-server
	RUN yum install net-tools -y
	RUN yum install git -y
	RUN yum install httpd -y
	RUN yum install which -y
	RUN yum install python36 -y
	RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
	RUN chmod +x ./kubectl
	RUN mv ./kubectl /usr/local/bin/kubectl
	RUN mkdir .kube
	COPY ca.crt /root/
	COPY client.crt /root/
	COPY client.key /root/
	COPY config /root/.kube/
	RUN echo "jenkins ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
	RUN echo "java -jar /usr/lib/jenkins/jenkins.war &" >> /root/.bashrc
	RUN bash
  • Now build this Dockerfile:
docker build -t bobby8249/kube_clu .
No alt text provided for this image


  • Now start your jenkins conatiner.
docker run -it -p 8085:8080 --name jenkube bobby8249/kube_clu


Step-2)Setup the Jenkins.

  • After this , type your IP:8085 in your browser:
No alt text provided for this image
  • Copy the password from the terminal and paste, for unlock jenkins.
  • Otherwise run the below command inside the containner


cat /root/.jenkins/secrets/initialAdminPassword


No alt text provided for this image
  • Install suggested plugins it will install all basic use plugins for you . If you not need to install all plugins then close it .
No alt text provided for this image


No alt text provided for this image
  • Click on Start using Jenkins.
  • jenkins Home Page.
No alt text provided for this image
  • Now we need to install plugins in jenkins.
  • Goto Manage Jenkins > Plugins Manage > Available, install the plugins, => Github , Job DSL, Build Pipeline.
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
No alt text provided for this image
  • Click on Download now nd install after restart.

Step-3)Make Seed job. (task6_job2)

  • Goto jenkins homepage > Click on New Items > Name it(task6_job2)>Select free style job.
  • Configure with following image:
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
  • Save it , Build it.

Console Output Of job2:

No alt text provided for this image


  • Code for job1:
job("task6_job1"){
        description("this job will copy the file in you os version and push image to docker hub 
         2.this will create deployment for website and expose deployment")
        scm {
                 github('Bobby8249/devops_task6' , 'master')
             }
        triggers {
                scm("* * * * *")
                
        }


        steps {
        shell('''sudo cp * /
sudo docker build -t bobby8249/http:latest .
sudo docker push bobby8249/http
if sudo kubectl get deployment | grep myweb
then
echo " updating"
else
sudo kubectl create deployment myweb --image=bobby8249/http
sudo kubectl autoscale deployment myweb --min=10 --max=15 --cpu-percent=80
fi
if sudo kubectl get deployment -o wide | grep latest
then 
sudo kubectl set image deployment myweb http=bobby8249/http
else
sudo kubectl set image deployment myweb http=bobby8249/http:latest
fi
if sudo kubectl get service | grep myweb
then 
echo "service exist"
else
sudo kubectl expose deployment myweb --port=80 --type=NodePort
fi ''')
      }
}

Now, when the job1 is build it will go to the github where the Developer have uploaded the HTML file and Dockerfile copy both the files the linux system. And build a docker image using the Dockerfile and push(upload) the docker inage in the developer's docker repository in www.hub.docker.com .

now in job2 kubernetes will fetch this image from the dockerhub and use it to create pods and launch the website created by the developer in it and expose it so the it is accessible from anywhere.

code of job3:

job("task6_job3") {
  description ("It will test if pod is running else send a mail")
  
  triggers {
    upstream('task6_job1', 'SUCCESS')
  }
  steps {
    shell('''if sudo kubectl get deployment | grep myweb
then
echo "send to production"
else
echo "sending back to developer"
exit 1
fi''')
  }
  publishers {
    extendedEmail {
      contentType('text/html')
      triggers {
        success{
          attachBuildLog(true)
          subject('Build successfull')
          content('The build was successful and deployment was done.')
          recipientList('[email protected]')
        }
        failure{
          attachBuildLog(true)
          subject('Failed build')
          content('The build was failed')
          recipientList('[email protected]')
        }
      }
    }
  }
}

Code for build pipeline

buildPipelineView('jobs-view') {
  filterBuildQueue(true)
  filterExecutors(false)
  title('j-jobs-view')
  displayedBuilds(1)
  selectedJob('task6_job1')
  alwaysAllowManualTrigger(false)
  showPipelineParameters(true)
  refreshFrequency(1)
}


FINAL CODE:

job("task6_job1"){
        description("this job will copy the file in you os version and push image to docker hub 2.this will create deployment for website and expose deployment")
        scm {
                 github('Bobby8249/devops_task6' , 'master')
             }
        triggers {
                scm("* * * * *")
                
        }


        steps {
        shell('''sudo cp * /
sudo docker build -t bobby8249/http:latest .
sudo docker push bobby8249/http
if sudo kubectl get deployment | grep myweb
then
echo " updating"
else
sudo kubectl create deployment myweb --image=bobby8249/http
sudo kubectl autoscale deployment myweb --min=10 --max=15 --cpu-percent=80
fi
if sudo kubectl get deployment -o wide | grep latest
then 
sudo kubectl set image deployment myweb http=bobby8249/http
else
sudo kubectl set image deployment myweb http=bobby8249/http:latest
fi
if sudo kubectl get service | grep myweb
then 
echo "service exist"
else
sudo kubectl expose deployment myweb --port=80 --type=NodePort
fi ''')
      }
}








job("task6_job3") {
  description ("It will test if pod is running else send a mail")
  
  triggers {
    upstream('task6_job1', 'SUCCESS')
  }
  steps {
    shell('''if sudo kubectl get deployment | grep myweb
then
echo "send to production"
else
echo "sending back to developer"
exit 1
fi''')
  }
  publishers {
    extendedEmail {
      contentType('text/html')
      triggers {
        success{
          attachBuildLog(true)
          subject('Build successfull')
          content('The build was successful and deployment was done.')
          recipientList('[email protected]')
        }
        failure{
          attachBuildLog(true)
          subject('Failed build')
          content('The build was failed')
          recipientList('[email protected]')
        }
      }
    }
  }
}




buildPipelineView('jobs-view') {
  filterBuildQueue(true)
  filterExecutors(false)
  title('j-jobs-view')
  displayedBuilds(1)
  selectedJob('task6_job1')
  alwaysAllowManualTrigger(false)
  showPipelineParameters(true)
  refreshFrequency(1)
}


Output of Job1:

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


Console Output of job1:


No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
  • Here our job configured, it build without any issue.

Output of Job3:

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

Console Output of job3:


No alt text provided for this image

Here our job configured, it build without any issue and also with the message is “send to production” . Also send email to developer

Output of Build Pipeline View.

This build pipeline also created by seed job with DSl groovy code

No alt text provided for this image

Now if you check the pods created by the kubernetes

the pods are launched successfully in the kubernetes and the service is exposed with a random port

kubectl get  all
No alt text provided for this image

Output of Website.

  • Here our basic website comes which is written in HTML.
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image


Thank you !

Author :Bobby Singh

GitHub Link:

Profile Link:


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

Bobby Singh的更多文章

社区洞察

其他会员也浏览了