Jenkins’s dsl(domain specific language) Groovy

Dhiraj Kumar
5 min readSep 6, 2020

Jenkins: jenkins is the devops tool which helps to automation of projects , by which projects are tracked and done automatically. jenkins can be used as integration tool also . By jenkins many devops tool are integrated and done project in loop automaticaly without using more human resources .

In devops when developer wants to configure job in jenkins without access login ID and password then dsl(domain specific language )is used for configuration job .

Jenkins support groovy language because groovy support java and jenkins already have java .

When a job is configured for downloading code from github then developer upload code for job configuration that is written in groovy language . After download code from github automatic a job is created .

Task description:

Perform task with the help of Jenkins coding file ( called as jenkinsfile approach ) .

  1. First of all created container image that’s has jenkins installed using dockerfile .

First of all created a directory “devops_task6” using cmd

mkdir devops_task6

After creating directory go to that directory and create a Dockerfile for image creating that has jenkins installed and have power to operate kubernetes resources

cd devops_task6

Dockerfile:

After creating Dockerfile run below cmd to create image

docker build -t jenkins_image:v2 .

After creating image tagged a name with image name and upload on github.

docker tag jenkins_image:v2 firsttalk26/jenkins_image:v2 
docker push firsttalk26/jenkins_image:v2

After uploading image on github , i have created deployment configuration file for deployment of jenkins on kubernetes .

jenkins_deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins-deployment
labels:
app: jenkins
spec:
replicas: 1
selector:
matchLabels:
app: jenkins
strategy:
type: Recreate
template:
metadata:
labels:
app: jenkins
spec:
containers:
- image: firsttalk26/jenkins_image:v2
name: jenkins
ports:
- containerPort: 8080
name: jenkins
volumeMounts:
- name: jenkins-pvc
mountPath:
/root/.jenkins
volumes:
- name: jenkins-pvc
persistentVolumeClaim:
claimName: jenkins-pvc

jenkins_pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins-pvc
labels:
name: jenkinspvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

jenkins_service.yml

apiVersion: v1
kind: Service
metadata:
name: jenkins-service
labels:
app: jenkinsService
spec:
ports:
- port: 8080
nodePort: 30006
selector:
type: jenkins
type: NodePort

For launch jenkins deployment , service and pvc run below cmd

kubectl create -f jenkins_pvc.yml
kubectl create -f jenkins_deployment.yml
kubectl create -f jenkins_service.yml

After launch pod of jenkins open jenkins dashboard using url and created job for complete project .And created three job

Job in jenkins:

Parentjob: Pull the repo from github automatically when some developers push repo on github , and job is created using written code in groovy language by the developer .

Job1: Pull the github repo automatically when some developers push repo on github , and copy in the folder task6_data

Job2: By looking on the code or programme file , jenkins should automatically start the respective language interpreter installed image container to deploy code on top of kubernetes .

Job3: Test app , it is working or not . If app is not working then sent email to developer with error messages

First of all i have created a code file using groovy language and i have uploaded on github .

Task6.groovy:

job("task6_job1"){
description("my first dsl job")
scm{
github('firsttalk26/devops1','master')
}
triggers{
scm("* * * * *")
}
steps{
shell('''if ls /root/.jenkins/workspace/ | grep task6_data
then
cp -rf * /root/.jenkins/workspace/task6_data/
echo "folder created"
else
sudo mkdir /root/.jenkins/workspace/task6_data
cp -rf * /root/.jenkins/workspace/task6_data/
fi''')

}
}
job("task6_job2"){
description("my 2nd dsl job")
triggers{
upstream("task6_job1",'SUCCESS')
}
steps{
shell('''if ls /root/.jenkins/workspace/task3_data/ | grep .html
then
if kubectl get deployment | grep webserver-deployment
then
echo "already webserver-deployment created"
else
kubectl create -f /root/.jenkins/workspace/task3/webserver_deployment.yml
fi
else
echo "it's not html code"
if ls /tas3/ | grep .phpcode
then
echo " it is php code"
if kubectl get deployment | grep webapp-deployment
then
echo " webapp-deployment is already created"
else
kubectl create -f /root/.jenkins/workspace/task3/php.yml
fi
else
echo "recheck code"
fi
fi''')
}
}
job("task6_job3"){
description("my 3rd dsl job")
triggers{
upstream("task6_job2",'FAILURE')
}
steps{
shell('''podname=$(kubectl get pods -l type=html --output=jsonpath={.items..metadata.name})
kubectl cp /root/.jenkins/workspace/task3_data/ $podname:/usr/local/apache2/htdocs/status=$(curl -o /dev/null -s -w "%{http_code}" http://192.168.99.109:30001)
if [ $status == 200 ]
then
exit 0
else
exit 1

fi
''')

}
publishers{
mailer("firsttalk26@gmail.com", false, false)
}
}

Parentjob:

In parent job Task6.groovy file is downloaded and job is configured using written code

Job1:

Job2:

Job3:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Dhiraj Kumar
Dhiraj Kumar

Written by Dhiraj Kumar

Expertise on Cloud Computing who has helped many startups to reduce cloud cost upto 40% based on business need. Focused to optimize development process.

No responses yet

Write a response