Automating web-server deployment using Groovy Script

Shirsha Datta
5 min readSep 5, 2020
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.

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.

Seed Job Configuration 1.1
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.

Groovy Script

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

ERROR: script not yet approved for use
seed job output 2.0
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:

On Seed Job successful build

The Console output for the same is:

Console output of seed job

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.

Generated job1 config 1.1
Generated job1 config 1.2
Generated job1 config 1.3

In the console output, we can see

Generated job1 output 1.1
Generated job1 output 1.2

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

YAML files
Generated job2 config 1.1
Generated job2 config 1.2
Generated job2 console output 1.1

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.

Generated job3 config 1.1
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

Generated job3 console output1.1

And yay🎉our website is successfully deployed.

Our simple webpage

We can visualize this using the build pipeline:

Build pipeline

Thank you all. Connect with me here.

--

--

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.