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
docker run --name jenkinsmaster -u root \ --rm -d -p 8081:8080 \ -v jenkins-data:/var/jenkins_home \ jenkins/jenkinsYou 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?
- 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".
- In that job we can have a "Source Code Management" section with a git repo URL as given in the DSL script scm block.
- 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
- 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
- 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.
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") } }
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' } }
Jenkins Seed Job FAILURE - script not set approval |
Jenkins Seed Job ScriptApproval action |
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.
No comments:
Post a Comment