DevOps AL task 6
This task is similar in every way to task 3 except in this task we are not creating jobs manually from Jenkins GUI. Instead Groovy file, which is DSL, is used to create the jobs, build pipeline and configure them.
WHAT IS GROOVY DSL?
Domain Specific Languages (DSLs) have become a valuable part of the Groovy idiom. To a developer, DSLs are consumable and understandable, which makes implementation more fluid compared to traditional programming. Using this developers can write the code for building the jobs and configure them which was previously done by operators.
WHAT IS JENKINSFILE?
Jenkins pipelines can be defined using a text file called JenkinsFile. It can implement pipeline as code using JenkinsFile, and this can be defined by using a domain specific language (DSL). With JenkinsFile, we can write the steps needed for running a Jenkins pipeline.
TASK DESCRIPTION:-
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. 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: Pull the code from github to deploy app
7. Job2 :
i. 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 )
ii. Expose your pod so that testing team could perform the testing on the pod
iii. Make the data to remain persistent using PVC ( If server collects some data like logs, other user information )
8. Job3 : Test your app if it is working or not.
9. 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
For reference here is task 3 link:-
Firstly I have created a groovy file to create jobs and build pipeline:-
job("Job1_t6"){
description("Pulling code")
scm {
github('tanisha-agrawal/devopsaltask3','master')
}
triggers {
scm("* * * * *")
}
steps {
shell('sudo cp * /jenkins/')
}
}
job("Job2_t6"){
description("Creating and exposing deployment")
triggers {
upstream('Job1_t6', 'SUCCESS')
}
steps {
shell('''
if sudo docker ps | grep kubec
then
sudo docker rm -f kubec
else
echo "No kubec"
fi
sudo docker run -dit -v /jenkins:/task3 --name kubec tanisha30/kubec:v2
sudo docker exec kubec kubectl delete all --all --server https://192.168.99.102:8443 --client-key /home/jenkins/client.key --client-certificate /home/jenkins/client.crt --certificate-authority /home/jenkins/ca.crt
sudo docker exec kubec kubectl delete pvc --all --server https://192.168.99.102:8443 --client-key /home/jenkins/client.key --client-certificate /home/jenkins/client.crt --certificate-authority /home/jenkins/ca.crt
if ls /jenkins | grep php
then
sudo docker exec kubec kubectl create -f /task3/web-php.yaml --server https://192.168.99.102:8443 --client-key /home/jenkins/client.key --client-certificate /home/jenkins/client.crt --certificate-authority /home/jenkins/ca.crt
elif ls /jenkins | grep html
then
sudo docker exec kubec kubectl create -f /task3/web-html.yaml --server https://192.168.99.102:8443 --client-key /home/jenkins/client.key --client-certificate /home/jenkins/client.crt --certificate-authority /home/jenkins/ca.crt
else
exit 1
fi
''')
}
}
job("Job3_t6")
{
description("Testing the app")
triggers {
upstream('Job2_t6', 'SUCCESS')
}
steps {
shell('''
export status=$(curl -s -i -w "%{http_code}" -o /dev/null/ http://192.168.99.102:32004)
if echo $status==200
then
exit 1
else
exit 0
fi
''')
}
}
job("Job4_t6")
{
description("Sending email")
triggers {
upstream('Job3_t6', 'SUCCESS')
}
publishers {
extendedEmail {
recipientList('tanishaagrawal016@gmail.com@gmail.com')
defaultSubject('Job status')
attachBuildLog(attachBuildLog = true)
defaultContent('Status Report')
contentType('text/html')
triggers {
always {
subject('build Status')
content('Body')
sendTo {
developers()
recipientList()
}
}
}
}
}
}
buildPipelineView('Task-6 view') {
filterBuildQueue()
filterExecutors()
title('Groovy Pipeline')
displayedBuilds(1)
selectedJob('Job1_t6')
refreshFrequency(3)
}
File is there in this github link:-
I have used Jenkins container which was used in task 3. After this we need to install Job DSL plugin. Then create a seed job as follows:-
Apply and save
Now if you run this you have to approve the script by going to Manage Jenkins-> In-Process script approval-> approve
After doing this again build seed job. Output will look like this:-
Now you can check jobs and build pipeline view are created and also configured:-
And the web-page deployed:-
Thanks!