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

Comments

Popular posts from this blog

Ansible Jinja2 Templates: A Complete Guide with Examples

Ansible 11 The uri module with examples

Jenkins Active choices parameter - Dynamic input