Automating web-server deployment using Groovy Script

Image for post
Image for post
Groovy | Jenkins | K8S | Docker GIF credits: Daksh Jain

Everybody is well aware of the SILOS between the Developer and Operations team. But I am here to present the solution to bridge the gap between the Developer and the Operations team. Let me break this complex line into simple terms for better understanding.

When the developer pushes code, they have to wait for the operations team for testing,monitoring and deployment. The the best way so as to not to depend upon the Operations team is to writes these jobs in his own way i.e. CODE, put this in GitHub and somehow this CODE will automatically create the jobs in Jenkins.

And this is possible by using a Domain Specific Language for Jenkins called GROOVY. This gives us the essence of DevOps Culture and CI/CD in the complete sense.

So let us get going as to how to achieve the same:

The system requirements are as follows:

  • In RHEL8 docker & Kubectl must be installed and configured.
  • Jenkin-Plugins — Build Pipeline, Job DSL, GitHub plugins must be installed.
  1. The developer will write a code in Groovy Language and push it on GitHub.
  2. Using GitHub WebHooks Jenkins will automatically pull this code and jobs will be created which will automate website deployment.
  3. The Operations team managing Jenkins will have to run the first Job also called the seed job and the rest all will work through chaining and can be seen using a build pipeline view.

So the developer has pushed to Github, which will be monitored by using the GitHub webhooks. Now the only task of the Admin is to create the Job first time, which will pull the code from GitHub. After that, any changes to the code will be monitored using hooks.

Image for post
Image for post
Seed Job Configuration 1.1
Image for post
Image for post
Seed Job Configuration 1.2

In DSL scripts, test.groovy is the DSL script for creating the subsequent jobs as well as for creating the build pipeline view. This job is also called the seed job as it initiates all other jobs.

Image for post
Image for post
Groovy Script

After it is run for the first time, it shows the error:

ERROR: script not yet approved for use
Image for post
Image for post
seed job output 2.0
Image for post
Image for post
Image for post
Image for post
Accepting Script Approval

We need to accept the In-process Script Approval, by going to

Jenkins->Manage Jenkins ->In-process Script Approval ->Approve

After the error is rectified, and we run the job it shows this:

Image for post
Image for post
On Seed Job successful build

The Console output for the same is:

Image for post
Image for post
Console output of seed job

After the seed job is built it creates the job GitHub-Code automatically. Then we can go and check the job configuration for verification. It is the same as our requirement i.e to copy the files from GitHub, build the docker image from the Dockerfile and copy the code pushed by the developer, and then push the created image to DockerHub. We see that the image is created on the fly.

Image for post
Image for post
Image for post
Image for post
Generated job1 config 1.1
Image for post
Image for post
Generated job1 config 1.2
Image for post
Image for post
Generated job1 config 1.3

In the console output, we can see

Image for post
Image for post
Generated job1 output 1.1
Image for post
Image for post
Generated job1 output 1.2

On the successful build of GitHub-Code, it triggers a new build of Deployment.

The task of this job is to check the code and launch the containers accordingly. That is if it is an HTML code, run the container which has the appropriate configurations needed for it. We are using Kubernetes to create Deployment which uses Replica Sets behind the scenes to launch pods containing containers. And finally, expose the pods so as to access it from the public world. If the deployment already exists, then echo “Already running”.

Image for post
Image for post
YAML files
Image for post
Image for post
Generated job2 config 1.1
Image for post
Image for post
Generated job2 config 1.2
Image for post
Image for post
Generated job2 console output 1.1

Successful build triggers a new build of Testing.

It checks if the code by the developer for bugs if it finds bugs then, it notifies using status code. If the code is without errors it gives status code as 200.

Image for post
Image for post
Image for post
Image for post
Generated job3 config 1.1
Image for post
Image for post
Generated job3 config 1.2

And we can access the page using the IP. In this case, it is : 192.168.99.100:32123/index.html

Image for post
Image for post
Generated job3 console output1.1

And yay🎉our website is successfully deployed.

Image for post
Image for post
Our simple webpage

We can visualize this using the build pipeline:

Image for post
Image for post
Build pipeline

Thank you all. Connect with me here.

Written by

I am a DevOps Enthusiast and recently taken to Cloud Computing. Learning Flutter App Development currently. In my free time I engage in competitive coding.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store