Wednesday, June 29, 2022

Jenkins Seed Job using DSL Script to Generate Job

 Hey Guys!! How are you? In this learning post I would like to share with you awesome Jenkins feature experiment -  Infrastructure as a Code (IaC). The entire Jenkins Job or Project can be configured manually step-by-step in the regular fashion, when you click on the "new item" on the Jenkins Dashboard to create a project. But here story is different - with DSL script you could do the same thing that is dynamically creating a Job with a single flexible scalable durable way with Groovy script. 

Jenkins DSL Groovy script generating dynamic Job

Groovy is more like Shell scripting but totally executable on Java Virtual Machine. Those who know Java it will be easy to understand the Groovy syntax and its blocks. If you don't then no worries we will be coming up with easy to use DSL script in the following sections.

Prerequisite for DSL Job creation

  • Jenkins master-slave installed up and running. SCM installed/github Up and running
  • Jenkins must have plugins installed such as Copy to Archive, Archive Artifacts etc
  • Jenkins "Global Tool Configuration" should have "Maven" , "JDK" specially needed for Java Build process

On the Docker Play with Docker (PWD) you can run the following commands
docker run --name jenkinsmaster -u root \
--rm -d -p 8081:8080 \
-v jenkins-data:/var/jenkins_home \
 jenkins/jenkins
You can check the docket hosting machine port open for Jenkins containers:
netstat -tupln|grep 8081
#To get the password from the logs 
docker logs jenkinsmaster

What Jenkins plugin need to be installed? Job DSL plugin

We need to install the 'Job DSL' plugin. This will enable you to write Groovy scripts. As regular plugin installation procedure - Navigate to Manage Jenkins -> Manage Plugins ->Go to the  availability tab search for "Job DSL'. Once found select it and click on the 'Install without Restart' Button. 

How to write a Jenkins DSL groovy script? 

This is very simple! Go to your Jenkins Dashboard and select "New Item" select "Free Style Project".  In the Build section - select  “Process Job DSL” as Build step this will be visible only when you install the Job DSL plugin.

Developing a Seed Job Logic : 

    Parameters in DSL

    The following is the example where you can use the parameterised project in DSL, we can use the following snippet in 'Build' step Groovy script can be used as following 'Job DSL'
      job('parameterized_dsl_job'){
    	description("This project is seeded with parameters")
    	
    	parameters{
    		stringParam('user_name',defaultValue = 'Vybhava", description = 'Please enter user_name')
    	}
    		
    	setps {
    		shell("echo Hello $user_name")
    	}
    }
      
  1. To use the Job DSL plugin, you first need to create a seed job. The seed job is a Jenkins job which runs newly created a DSL scripts, and then generates a new job name as "Maven-Project-DSL". 
  2. In that job we can have a "Source Code Management" section with a git repo URL as given in the DSL script scm block.
  3. This dynamically created job can be chosen to trigger every 2 mins this can be modified as per your Project needs. This block is like in the Build Trigger section having Poll SCM
  4. In Build Step/section we use to select "Invoke Top level Maven Targets" option similar to it we can use steps block and mention it "clean package" targets and also provide where is your pom.xml that will create a war file. Note: look into the pom.xml file packaging line according use the artifact option
  5. Like our Post Build action  here in the DSL script we can have a publisher block that calls archiveArtifacts with a parameter, here I've choose for **/*.war because same will be used in the deployment in the Tomcat server.
job('Maven-Project-DSL') {
    description("Maven job generated by the DSL on ${new Date()}, the project is a small Maven project hosted on github")
    // Source Code Management section equivalent block
scm { git("https://github.com/Bhavanishekhar/Jenkins_integrations.git", 'master') } // Build Trigger section equivalent triggers { scm('H/2 * * * *') // every 2 minutes SCM Polling is scheduled here } // Like Build having Invoke top-level Maven targets steps { maven('clean package', 'maven-samples/single-module/pom.xml') } publishers { //archive the jar/war/ear file generated archiveArtifacts '**/*.war' } }
Here the publishers is like 'Post Build Actions' on the Jenkins configuration page.

6. Save the Seed Job and click on the "Build Now" and then Job will be in Failed state. Check the Console Output. 

Jenkins Seed Job FAILURE - script not set approval

7. Go to the Manage Jenkins and select the "In-process Script Approval" this will allows us to review proposed DSL script inside the Jenkins process and generate a new Job. In the "ScriptApproval" page you could see choice as shown:


Jenkins Seed Job ScriptApproval action


8. You need to click on the "Approve", and then go back to the Seed Job and build again.
9. After seed job execution completes you will be getting a new Job with name "Maven-Project-DSL"



Thanks for your time if you like this post please share it with your friends and colleagues.


Reference: 

  1. Job DSL
  2. DSL Help on groovy syntax

No comments:

Categories

Kubernetes (24) Docker (20) git (13) Jenkins (12) AWS (7) Jenkins CI (5) Vagrant (5) K8s (4) VirtualBox (4) CentOS7 (3) docker registry (3) docker-ee (3) ucp (3) Jenkins Automation (2) Jenkins Master Slave (2) Jenkins Project (2) containers (2) create deployment (2) docker EE (2) docker private registry (2) dockers (2) dtr (2) kubeadm (2) kubectl (2) kubelet (2) openssl (2) Alert Manager CLI (1) AlertManager (1) Apache Maven (1) Best DevOps interview questions (1) CentOS (1) Container as a Service (1) DevOps Interview Questions (1) Docker 19 CE on Ubuntu 19.04 (1) Docker Tutorial (1) Docker UCP (1) Docker installation on Ubunutu (1) Docker interview questions (1) Docker on PowerShell (1) Docker on Windows (1) Docker version (1) Docker-ee installation on CentOS (1) DockerHub (1) Features of DTR (1) Fedora (1) Freestyle Project (1) Git Install on CentOS (1) Git Install on Oracle Linux (1) Git Install on RHEL (1) Git Source based installation (1) Git line ending setup (1) Git migration (1) Grafana on Windows (1) Install DTR (1) Install Docker on Windows Server (1) Install Maven on CentOS (1) Issues (1) Jenkins CI server on AWS instance (1) Jenkins First Job (1) Jenkins Installation on CentOS7 (1) Jenkins Master (1) Jenkins automatic build (1) Jenkins installation on Ubuntu 18.04 (1) Jenkins integration with GitHub server (1) Jenkins on AWS Ubuntu (1) Kubernetes Cluster provisioning (1) Kubernetes interview questions (1) Kuberntes Installation (1) Maven (1) Maven installation on Unix (1) Operations interview Questions (1) Oracle Linux (1) Personal access tokens on GitHub (1) Problem in Docker (1) Prometheus (1) Prometheus CLI (1) RHEL (1) SCM (1) SCM Poll (1) SRE interview questions (1) Troubleshooting (1) Uninstall Git (1) Uninstall Git on CentOS7 (1) Universal Control Plane (1) Vagrantfile (1) amtool (1) aws IAM Role (1) aws policy (1) caas (1) chef installation (1) create organization on UCP (1) create team on UCP (1) docker CE (1) docker UCP console (1) docker command line (1) docker commands (1) docker community edition (1) docker container (1) docker editions (1) docker enterprise edition (1) docker enterprise edition deep dive (1) docker for windows (1) docker hub (1) docker installation (1) docker node (1) docker releases (1) docker secure registry (1) docker service (1) docker swarm init (1) docker swarm join (1) docker trusted registry (1) elasticBeanStalk (1) global configurations (1) helm installation issue (1) mvn (1) namespaces (1) promtool (1) service creation (1) slack (1)