Jenkins Job automation using groovy.

Jenkins Job automation using groovy.

This is the 6th task of DevOps AL training given by Mr Vimal Daga sir...

About the task..

About the task..

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

Lets start doing the task..

Very first I will create a container image that has Jenkins installed using docker file..


This is the 6th task of DevOps AL training given by Mr Vimal Daga sir...

About the task..

About the task..

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

Lets start doing the task..

Very first I will create a container image that has Jenkins installed using docker file.

No alt text provided for this image


to create image i have used a command "docker build -t [name] ."

Now i will launch a container by mounting one of the directory of the container to the base OS.

No alt text provided for this image


After doing the above steps. Now i have to configure the jenkins..

Configuration part of jenkins you wil get from the below link..

https://www.dhirubhai.net/posts/manmohan-singh-8a0b24186_devops-kubernetes-vimaldaga-activity-6745030476492357632-qKxh

I have also downloaded some plugins like GitHub , SSH , Email , Build Pipeline , Job dsl in order to process the task..

Now i will create a seed job.. This seed job will automatically create Jobs in jenkins..

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

All the job has been generated successfully..

Groovy code that i used to generate the jobs.

job("Job1"){
	description("this job will download the file from the github")
	        
	scm{
	github('Mansh-star/devops-task-6', 'master')
	}
	triggers {
	scm('* * * * *')
	}
	steps {
	shell('cp -rvf * /root/ajay6/')
	}
	}
	

	job('Job2'){
	description("this job will deploy the the code in the webserver")
	

	triggers {  
	upstream('Job1', 'SUCCESS')
	}
	steps{
	remoteShell('[email protected]:108:22') {
	command('''if sudo ls /root/task6 | grep .html
	then
	if sudo kubectl get deployment | grep web
	then
	echo " deployment is running "
	else
	sudo kubectl create -f /root/web.yml
	sleep 30
	if kubectl get pods | grep webaj
	then
	ajay=$(sudo kubectl get pods -o=name | grep webaj | sed "s/^.\\{4\\}//")
	sudo kubectl cp /root/task6/index.html $ajay:/usr/local/apache2/htdocs/
	else
	echo "code not copy"
	fi
	fi
	else
	echo "not a html code"
	fi
	''')
	}
	}
	}
	

	job("Job3"){
	description("this job will do the testing")
	

	triggers {
	upstream('Job2','SUCCESS')
	}
	steps{
	remoteShell('[email protected]:22') {
	command('''if sudo kubectl get pods | grep webaj
	then
	webaj_status_code=$(curl -o /dev/null -s -w "%{http_code}"
192.168.99.102:30000)
	if [[ $webaj_status_code == 200 ]]
	then
	echo "website is running"
	else
	echo "their is some problem is website"
	exit 1
	fi
	else
	echo " not running "
	fi''')
	}
	}
	

	publishers {
	extendedEmail {
	recipientList('[email protected]')
	defaultSubject('Something is wrong...')
	defaultContent('Testing failed...')
	contentType('text/html')
	triggers {
	beforeBuild()
	stillUnstable {
	subject('Subject')
	content('Body')
	sendTo {
	developers()
	requester()
	culprits()
	}
	}
	}
	}
	}

	}

Lets see the job configuration that is build by seed job..

Job 1..

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

Job 2.

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

Job 3.

No alt text provided for this image


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

This job also send mail to the developer if something went wrong..

No alt text provided for this image

Here is the yaml code that i used to create deployment , pvc , service in job 2..

apiVersion: v1
kind: Service
metadata:
  name: webaj
  labels:
    app: webaj
spec:
  ports:
    - port: 80
      nodePort: 30000
  selector:
    app: webaj
    tier: frontend
  type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: webaj-pv-claim
  labels:
    app: webaj
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: webaj
  labels:
    app: webaj
spec:
  selector:
    matchLabels:
      app: webaj
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: webaj
        tier: frontend
    spec:
      containers:
      - image: httpd
        name: webaj
        
        ports:
        - containerPort: 80
          name: webaj
        volumeMounts:
        - name: webaj-persistent-storage
          mountPath: /usr/local/apache2/htdocs/
      volumes:
      - name: webaj-persistent-storage
        persistentVolumeClaim:

          claimName: webaj-pv-claim


Console output of above jobs

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

Here is the Deployment , Service , Pod , PVC created by Job 2.

No alt text provided for this image

Now finally i will access the site from the browser.

No alt text provided for this image

Here is the Build Pipeline

No alt text provided for this image

All the code that is used in this task you will get from the below link..

https://github.com/manmohan2823/devops-task6-groovy.git

Thank you guys for going through this task..

Have a great day..




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

Manmohan .的更多文章

社区洞察

其他会员也浏览了