CI/CD Pipeline Using Groovy Script

Rupali Gurjar
7 min readAug 13, 2020

Hello Everyone !

Let’s make the things more Agile …

Jenkins Pipeline

Jenkins Pipeline (or simply “Pipeline”) is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.

A continuous delivery pipeline is an automated expression of your process for getting software from version control right through to your users and customers.

Jenkins Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines “as code”.

What is Jenkins File

Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control.

Here I used Groovy language to write this file .

Groovy

Apache Groovy is a Java-syntax-compatible object-oriented programming language for the Java platform. It is both a static and dynamic language with features similar to those of Python, Ruby, and Smalltalk. It can be used as both a programming language and a scripting language for the Java Platform, is compiled to Java virtual machine (JVM) bytecode, and interoperates seamlessly with other Java code and libraries.

Task Overview

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

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 .

Task Description

Let’s begin the task !

It is always better to create a separate Workspace so that we can easily manage the things .

I copied all the required files to this workspace .

Here is my Dockerfile .

For more details You can see My Task 3 .

Let’s build this image using this command

docker build -t kubeweb:latest .

Now launch the Container

docker run --privileged -it --name webserver -p 8090:8080 kubeweb:latest 

It is the password to login the jenkins .

Here we can install suggested plugins or can install them manually .

Here we can set user and password

Let’s make a job for writing a Dsl script . But for this we need a plugin “Job_dsl” .

To make build pipeline , we need this plugin “Build Pipeline” .

Now , make the job

We will download Groovy script from Github .

Task6.groovy Script …

freeStyleJob('job1') {
description("Pull Code from Github")
scm {
github('rups04/Dev-task6', 'master')
}
triggers {
scm("* * * * *")
}
steps {
shell('''if sudo ls / | grep devops-task6
then
sudo rm -rvf /devops-task6
fi
sudo mkdir /devops-task6
sudo cp -rvf * /devops-task6''')
}
}
freeStyleJob('job2') {
triggers {
upstream('job1')
}
steps {
shell('''if ls /devops-task6 | grep .html
then
if sudo kubectl get all | grep myweb
then
kubectl delete all --all
kubectl delete pvc --all
fi
sudo kubectl create -f /root/deployment/html_deployment.yml
Pod_Name=$(kubectl get pod -l app=myweb -o jsonpath="{.items[0].metadata.name}")
sleep 50
sudo kubectl cp /devops-task6/*.html $Pod_Name:/usr/local/apache2/htdocs/

elif ls /devops-task6 | grep .php
then
if sudo kubectl get all | grep myweb-php
then
sudo kubectl delete all --all
sudo kubectl delete pvc --all
fi
sudo kubectl create -f /root/deployment/php_deployment.yml
Pod_Name=$(kubectl get pod -l app=myweb -o jsonpath="{.items[0].metadata.name}")
sleep 50
sudo kubectl cp /devops-task6/*.php $Pod_Name:/var/www/html/
else
echo "There is something wrong"
fi
''')
}
}
freeStyleJob('job3') {
triggers {
upstream('job2')
}
steps {
shell('''status=$(sudo curl -o /dev/null -s -w "%{http_code}" 192.168.99.100:30100)
if [[ $status == 200 ]]
then
echo "ok"
else
echo "There is some error"
sudo curl --user "<user_name>:<password>" http://172.17.0.2:8090/job/job4/build?token=Task6
fi
''')
}
}
freeStyleJob('job4') {
authenticationToken('Task6')
triggers {
upstream('job3')
}
steps {
shell('''if sudo kubectl get all | grep myweb
then
echo "everything is working fine"
else
python3 /Alert_mail.py
fi''')
}
}
buildPipelineView('Devops-task6') {
filterBuildQueue()
filterExecutors()
title('CI Pipeline')
displayedBuilds(5)
selectedJob('job1')
alwaysAllowManualTrigger()
showPipelineParameters()
refreshFrequency(10)
}

Build this job !

But it fails because this script is not approved for use .

For this manage jenkins>>security >> In-Process Sript Approval >>Approve

Now again build the job

It is working fine . Here we can see this job has created 4 another jobs .

How powerful and agile it is ^_^

Job1 : Pull the Github repo automatically when some developers push repo to Github.

Console Output :-

Job2 :

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 )

Console Output :-

Job3 : Test your app if it is working or not.

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 .

Alert_mail.py

Console Output :-

It doesn’t send mail to developer because Everything is working Fine .. Here we can see

Build Pipeline ..

Hope you liked this Article ..

Here is My Github Repository .

--

--