DevOps automation using Jenkins DSL (groovy script)

Kritika Shah
6 min readAug 16, 2020

In this article we are going to deploy a webserver on the top of Kubernetes, using the Jenkinsfile approach.

So before we start, let’s deep dive onto what is Jenkins coding file, or Jenkinsfile. A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control. Jenkinsfiles, using a Domain Specific Language (DSL) based on the Groovy programming language, are persistent files that model delivery pipelines ‘as code’.

Task Description :-

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 & job4 using build pipeline plugin in Jenkins .

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

>> Expose your pod so that testing team could perform the testing on the pod .

>> 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 .

Pre-Requisites : i) Jenkins on Rhel-8 server ii) Minikube installed with Kubernetes configured iii) Jenkins plugins — GitHub, Job DSL

Lets Start . .

Step 1: Create container image .

Create container image having Jenkins installed using dockerfile as -

Build this image using command :

docker build -t piyush0927/jenkins-kubernetes:latest  /root/jenkins-image/

Step 2: Launch image having Jenkins installed & start Jenkins WebUI .

Launch the container image to start Jenkins server as -

docker run -di --name jenkins-groovy -p 9090:8080 -v /root/jenkins-docker/:/root/.jenkins/ -v /var/run/docker/sock:/var/run/docker.sock  -v /root/task6:/root/task6 piyush0927/jenkins_kubernetes:latest

Start Jenkins WebUI on port no. 9090 with RedHat VM IP adress & setup credentials for account . After this you will see the following Home screen of Jenkins :

Step 3: Create seed job in Jenkins

This job will pull the groovy script & all the required codes from Github repository to create jobs & deploy web-server. To create seed job, “Job-DSL” plugin is required, download this plugin & then create this job (seed_job)

Write all the groovy scripts files name in “DSL Scripts” option to create jobs & build pipeline. Here, groovy scripts names are- job2_deploy.groovy, job4_notify_email.groovy, job3_testing.groovy & build_pipeline.groovy .

This job will fail in its first run since we would require to approve our DSL code. This can be done from Manage Jenkins >> In Process Script-Approval. Once the script is approved, the seed job will run successfully and configure further Jenkins jobs.

Configuration of Job-1 :-

Output of Job-1 :-

Configuration of Job-2 :-

Output of Job-2 :-

Configuration of Job-3 :-

Output of Job-3 :-

Since it is relevant from Job-3 that our app is functional, we can view our webpage on

http://<minikube ip>:<port no.>In this case, the webpage url is - 
http://192.168.99.100:30010

Build pipeline view of the workflow :-

--

--

Kritika Shah
Kritika Shah

No responses yet