Jenkins Pipeline with Groovy

Aditya Raj
4 min readJul 12, 2020

--

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 and job4 using build pipeline plugin in Jenkins
  4. Job2 ( 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. 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. 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

Step 1:

In this step we need to create container image that’s has Jenkins installed using Dockerfile.

Configuration file for kubectl

Now we need to build the image from the docker file using build command

docker build -t jenkins-kubectl:v1 .

From the image build we need to launch a container and then configure the jenkins.

For launching the container

docker run -it — name jenkinsos -p 8081:8080 jenkins-kubectl:v1

Now we need to setup the jenkins.

Step 2:

In this step we will will install the required plugin(Github, job-DSL and build-pipeline plugins) that will help us in running the groovey code but before that we need to disabled in the security settings of the Jenkins so that the built script can be run.

Now we will configure the seed job which will run the groovey code pulled from the github and then will create the job chain.

Console output of seed job

This job will create the other jobs .

Job 1: Pull the Github repo automatically when some developers push repo to Github.

Job 2 : This job will launch the container on the top of kubernetes by looking at the program file of the code. And then it will expose your pod so that testing team could perform the testing on the pod and also make the data to remain persistent using PVC.

For launching the pod and creating pvc and also exposing the pods I have craeted a yml code.

Job 3 : This job will test the app if it is working or not and if app is not working file then it will send the mail.

Job4 : If app is not working, the redeploy the application after code is being edited by the developer

Build pipeline of Job1, Job2, Job3:

The output of webapp

Github Link for Groovey Code:

Thank You!!!

--

--

Aditya Raj

I'm passionate learner diving into the concepts of computing 💻