Automation using Jenkins-Groovy and Kubernetes.

Anurag Mittal
7 min readJul 13, 2020

Use of Groovy

It can be used to orchestrate your pipeline in Jenkins and it can glue different languages together meaning that teams in your project can be contributing in different languages. Groovy can seamlessly interface with the Java language and the syntax of Java and Groovy is very similar.

Some of the operation responsibility is managed by developers than this kind of way we manage the things is one example of Devops.

Seed job

Job that will fetch the code from github and this code automatically create and run another jobs for which developer had pushed code to Github.

Jenkins used a language called GROOVY as Domain Specific Language.

Anything that you want to do in Jenkins as management prospective can be done with Groovy.

Groovy is one kind of scripting language.

TASK DESCRIPTION-

1. Create container image that’s has Jenkins installed using dockerfile.

2. When we launch this image, it should automatically start 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 the 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. Job2: Test your app if it is working or not.

8. Job3: If app is not working, then send email to the developer with error messages and redeploy the application after code is being edited by the developer.

Tools used in this project

  1. RHEL8 vm
  2. Jenkins
  3. Docker
  4. Kubernetes

LET’S START THE PROCESS

First creating a Directory . In this Directory I have Dockerfile for creating Image that has jenkins and kubectl installed . I have added one folder .kube in this directory that contains ca.crt, client.crt,client.key and config file that is used by kubectl for connecting to kubernetes cluster.

This is config file inside the .kube folder. This config file covers the details and path of ca.crt, client.crt,client.key .

Dockerfile for creating an image

For creating the docker image with the help of this Dockerfile. we have to run one command.

command → docker build -t anurag08/task-6:v1 .

Our image has been created successfully.

Now I am launching one container with this image that we have created. for launching the container we have to run command-

command → docker run -it — name myos -p 3698:8080 anurag08/task-6:v1

At the time of launching container with this image , it will give one password for login into jenkins server . use this password to login to jenkins server.

After login to jenkins we have to install some plugins that are required for this project.

  1. Github plugin — This plugin helps jenkins to download the code from github.

2. Job DSL — DSL plugin for Jenkins allows users to describe jobs using a Groovy-based language and manage the scripts and updates of Jenkins jobs.

3. Build pipeine plugin — This plugin helps jenkins to visualize the chaining of jobs in a descriptive and beautiful way.

Now creating Seed job — Job that will fetch the code from github and this code automatically create and run another jobs for which developer had pushed code to Github.

provide URL of the github where developer has pushed the code.

Jenkins will go to Github after every one minute and check if developer has push any code, if he/she pushed the code then download it. for this we have to set trigger. Iam using Poll SCM Trigger.

Jenkins will create one folder inside the container on which jenkins is running and copy the whole code from github inside this folder.

we have both the option like if we want to give code directly, then choose Use the provided DSL script and give the code here. But If we want to run our code as a script, then choose Look on Filesystem and give the name of script with .groovy extension.

Make sure you load your file on Jenkins workspace.

I have used Look on Filesystem and provide my script name that jenkins will download from github .

Now run this job . see what output will come up after run it.

Here you see that seed job has been failed.

The seed job has been failed because script is not approved by jenkins. so we have to approved the script.

For approval go to Manage jenkins →In process Script Approval and approve the script.

Click on Approve option for approving the script.

Now again build seed job and this time output will give success.

As you can see it have generated two jobs and one view/pipeline

we can also see it from dashboard. Initially we have only seed job but this time we have two more job and one view that is generated/created by seed job.

And this is the pipeline view

Push on RUN and our all jobs will run

As you can see all the jobs in this pipeline build successfully.

we can see the output of these jobs-

output of job 1

output of job 2

  • Job1(Deploy deployment for website):

It launches the deployment with PVC and services. Here I have taken the example of webpage so I have used the Apache webserver.

code.groovy -

And code.sh is…

index.html -

job1-

The YAML code is launching for the website is in http-pod.yaml file that is -

Job2(Testing that website is running or not) and Job3(Sent a mail to the developer):

After successfully build, Job1 will trigger Job2 and it checks the website that is working or not. If not working, it sent a notification to the developer.

Groovy code for this…

I write one script for do test here(test_code.sh)…

mail.py-

failed_mail.py -

job 2 -

For creating a pipeline view, we use the below code…

Pull all the code in one file and your groovy script is ready.

our site that is running fine and mail has been sent to developer.

The mail that has been sent to developer is —

Github link —

https://github.com/anurag08-git/groovy.git

Task completed.

--

--

Anurag Mittal

Learning the new technologies like mlops, devops, hybrid multi cloud to enhance my skills.