Jenkins Job Creation Using JenkinsFile

Samkit Shah
4 min readJul 2, 2020

--

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

This is my Dockerfile for Jenkins

Now we’re gonna create jobs and copy all the files. If the code is written in php it’lll copy in php dockerfile and if its in html it copies in html dockerfile.

Here is the groovy code which I’ve written

job(‘task6_j1’) {
triggers {
upstream(‘seed_task6_j1’, ‘SUCCESS’)
}
steps {
shell(‘’’
if sudo ls /GitPull | grep .html
then
sudo docker build -t samkit/htmlos /task6/htmlweb/.
sudo docker login
sudo docker push samkit/htmlos
if sudo kubectl get deploy | grep html
then
sudo kubectl delete deploy html
sudo kubectl delete pvc pvc1
sudo kubectl create -f /root/task3/task3_html.yml
sudo kubectl apply -f /root/task3/service.yml
else
sudo kubectl create -f /root/task3/task3_html.yml
sudo kubectl create -f /root/task3/service.yml
fi
else
sudo docker build -t samkit/php /task6/phpweb/.
sudo docker login
sudo docker push samkit/php
if sudo kubectl get deploy | grep php
then
sudo kubectl delete deploy php
sudo kubectl delete pvc pvc2
sudo kubectl create -f /root/task3/task3_php.yml
sudo kubectl apply -f /root/task3/service.yml
else
sudo kubectl create -f /root/task3/task3_php.yml
sudo kubectl create -f /root/task3/service.yml
fi
fi ‘’’)
}
}
job(‘task6_j3’) {
triggers {
upstream(‘task6_j1’, ‘SUCCESS’)
}
steps {
shell(‘’’
status=$(curl -o /dev/null -s -w “%{http_code}” 192.168.99.105:30554)
if [[ $status!=200 ]]
then
curl — user “admin:samkit” 192.168.29.53:8080/job/task3_job4/build?token=shah
else
exit 0
fi ‘’’)
}
}
job(‘task6_j4’) {
authenticationToken(‘shah’)
steps {
shell(‘’’
if sudo docker container ls -a | grep mail
then
sudo docker rm -f mail
sudo docker run -dit — name mail py_os:1
else
sudo docker run -dit — name mail py_os:1
fi
‘’’)
}
}
buildPipelineView(‘task-6’) {
displayedBuilds(5)
selectedJob(‘seed_task6_j1’)
alwaysAllowManualTrigger()
showPipelineParameters()
refreshFrequency(60)
}

This DSL will generate job1,3 and Job4

Job1:

If the code is of php so Jenkins will build the php installed docker image for kubernetes to launch the pods.

After Executing these resources we can see that all the pods will be working perfectly in our base system

Output of Job1

This is the Output of the website

Now we’re gonna delete the pods to see if the data is persisten or not

Now we’re gonna launch new deployment to see if the data remains persistent or not.

Job3 -Testing

This jobs trigger job 4 , and if it does not work , it will exit

Job-4 mail

This job will send us an email if there is an error

Finally we can see the builld pipeline

Thanks for reading !!

Lets connect on linkedln: link

--

--

Samkit Shah

Machine Learning | Deep Learning | DevOps | MLOps | Cloud Computing | BigData