Automating web-server deployment using Groovy Script

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.

Let’s start by setting up the environment:

  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.

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.

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

ERROR: script not yet approved for use

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:

The Console output for the same is:

Generated Job1:

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.

In the console output, we can see

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

Generated Job2

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

Successful build triggers a new build of Testing.

Generated job3:

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.

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

And yay🎉our website is successfully deployed.

We can visualize this using the build pipeline:

Thank you all. Connect with me here.

--

--

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
Shirsha Datta

Shirsha Datta

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.