JENKINS AND GROOVY

JENKINS AND GROOVY

No alt text provided for this image

Jenkins is an incredibly powerful tool for DevOps that allows us to execute clean and consistent builds and deployments that mitigate the risks of a manual execution plan. But creating the jobs within Jenkins to do this work can be labor intensive, manually updating those jobs is often error-prone, and tracking changes to those jobs is tedious at best.

Thankfully there are a couple of tools available to help us with these issues: Jenkins Job Builder and Jenkin’s Job DSL Plugin

Jenkins Job Builder

Jenkins Job Builder is a command-line utility that will create Jenkins jobs based upon YAML configurations.

Jenkin’s Job DSL Plugin

Jenkin’s Job DSL plugin provides a Groovy-based DSL for creating Jenkins jobs from within other Jenkins jobs. Its primary advantage over Jenkins Job Builder is that it provides direct access to a job’s configuration XML so that it can be manipulated directly to supplement any functionality that might be lacking in the DSL.

To create a “Hello World” job using the Job DSL plugin:

  • Go to Jenkin’s Plugin Manager and install the Job DSL plugin in the Jenkins instance. Jenkins may need to be restarted.
  • Create a new free-style Jenkins job

To use the Job DSL plugin, you first need to create a seed job. The seed job is a Jenkins job which runs a DSL scripts, and then generates a new job. The seed job is a normal free-style Jenkins job that you add the “Process Job DSL” build step. This step takes the DSL and generates the configured jobs.

TASK-6 INTRODUCTION

Perform third task with the help of Jenkins coding file ( called as jenkinsfile approach ) and perform the with following phases:

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

First we need a container image with jenkins installed.

No alt text provided for this image

STEP-2

Now when we launch the image it will start jenkins automatically set the password and install the required plugins.

STEP-3

CREATE SEED JOB

Now we will create the job which will download the code from the github into jenkins workspace automatically as the developer pushes to github.

No alt text provided for this image

STEP-4

Create the following yml files to deploy the webserver , persistent storage(PVC), exposing the service.

apiVersion: v1
kind: Service
metadata: 
   name: web-service
   labels:
       app: httpd
spec:
   ports:
   - nodePort: 9091
     port: 80
     targetPort: 80
   selector:
     app: httpd
     tier: web
   type: NodePort 
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
    name: volumeclaim
    lables:
      app: httpd
spec:
    accessModes:
    - ReadWriteOnce
    resources:
      requests: 
        storage: 2Gi

---
apiVersion: apps/v1
kind: Deployment
metadata: 
    name: httpd
    labels: 
       app: httpd
spec:
    selector:
       matchLables:
          app: httpd
          tier: web
    strategy:
          type: Recreate
    template:
       metadata:
          labels:
            app: httpd
            tier: web
       spec:
          containers:
          - image: arshmishra169/web:v1
            name: httpd
            ports:
            - containerPort: 80
              name: httpd
            volumeMounts: 
            - name: ps
              mountPath: /var/www/html
            volumes:
            - name: ps
              persistenVolumeClaim:
                 claimName: volumeclaim

STEP-5

Now we will create the groovy script for creating the jobs and pipeline of the jobs for the jenkins.

Basic Example of writing groovy script

def gitUrl = 'git://github.com/test/test'

job('test-job') {
    scm {
        git(gitUrl)
    }
    triggers {
        scm('*/15 * * * *')
    }
    steps {
        maven('-e clean test')
    }
}

If you pass the above script to the DSL seed job, it will dynamically generate a Jenkins job named “test-job”. If you submit an updated DSL, it will run the seed job and update “test-job” but not delete any history/builds.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

STEP-6

Now to run the groovy script add this in the seed job in build option.

No alt text provided for this image

After this the following jenkins pipeline will be created.

No alt text provided for this image

You now have the means to source control your Jenkins job configurations, and generate your jobs using the power and flexibility of Groovy in the Job DSL plugin. Even if the Job DSL doesn’t support a Jenkins plugin you’re using, you can still configure it without having to modify the Job DSL itself.

I worked in collaboration with Arsh Mishra and we together able to complete the task.

This workflow is great for us as it allows our developers full control over their Jenkins job and gives them quick feedback if there are any issues.

THANK YOU!!!


Sourabh Choudhary

Software Engineer | Java | Spring Boot | RestFul WebServices | Microservices | DSA

4 年

Great explanation Ayush Gupta ??

要查看或添加评论,请登录

Ayush Gupta的更多文章

社区洞察

其他会员也浏览了