What is Jenkins?
Jenkins is one of the most powerful and commonly used Continuous Integration and Deployment (CI/CD) tool in the DevOps world. A Job in Jenkins
denotes an automated flow created to do a particular set of tasks.
There are different types of Jobs available in the Jenkins like
Freestyle, Pipeline, DSL, etc. with the most common being the Jenkins Pipeline job.
Continuous Integration
In the DevOps culture, it is trivial to engage in a daily practice of merging the code present in a development environment into the main repository. Whenever we make an attempt to merge the code, there are tests run to ensure that no components of the application break. At this point in time, Continuous Integration (CI) comes into the picture to help the development team to detect bugs at an early stage, reduce the development time and act as a shield for application deployments.
Continuous Delivery
Continuous Delivery (CD) is the continuation of Continuous Integration. It is possible through a deployment pipeline. At the time, when the tests have been verified at the development environment, they are to be moved to the production environment. Continuous Delivery comes into the picture to automate the deployment actions that were previously performed manually. There are many CD tools that allow for easy creation of pipelines, such as Jenkins, CircleCI, AWS Code Deploy, etc.
Today's Agenda
In this post, we will learn how to configure a DSL Job in Jenkins to create other jobs dynamically on runtime. This plugin is available on all the Jenkins versions and we will create a pipeline job to run our DSL code. Prerequisite
This post has been prepared for the audience who :
- Have access to a system with Jenkins installed on it. If not yet done, please go ahead with the Normal Installation or the Customised Installation of Jenkins.
- Have a very basic understanding of Jenkins and its UI.
- And finally, are eager to learn and try such powerful tools.
Let's get started
Step 1: Install Job DSL Plugin.
Go to Jenkins home page > Manage Jenkins > Manage Plugins.
 |
Install Jenkins Job DSL Plugin |
Select the Job DSL Plugin and click on
button.
Once the plugin is installed, it's time to create a seed Job.
Similarly, also install Build Pipeline Plugin and Rebuilder plugins and restart your Jenkins server by hitting this URL - https://<your-jenkins-url>/restart.
Step 2: Configure Jenkins DSL seed Job.
DSL seed job is triggered to create the required jobs. Here we will try to create a few seed jobs that include a freestyle job and a pipeline job.
Go to New Item, provide a unique name to this seed job and select the Freestyle project job option, then click on the OK button.
Go to the Build section and add a new build step with Process Job DSLs.
Add the below code to the DSL Script section like this.
This code snippet creates a separate View in Jenkins that will display all the DSL type jobs. This is an optional code and can be skipped. Name functions include the name of all the jobs that we will be creating from this DSL job.
// This will create a View in Jenkins. listView('DSL_Jobs')
{ description('All new jobs for DSL demo') filterBuildQueue() filterExecutors() jobs { name('Onboard_Pipeline_Job') name('Onboard_Freestyle_Job') }
columns { status() weather() name() lastSuccess() lastFailure() lastDuration() buildButton() } } |
This code snippet will create a pipeline type job in Jenkins. This job is designed to fetch the Jenkinsfile and the rest of the files from Git.
// This will create a pipeline job.
pipelineJob('Onboard_Pipeline_Job') { def sshRepo = 'https://gitlab.example.com/DSL_code/democode.git' description("Test Onboard DSL Job Pipeline") keepDependencies(false)
parameters { choiceParam('Merge_Branch', ['dev', 'prod'], description = 'Select Branch to Use') stringParam('Service_Name', defaultValue = 'abc', description = 'Service to be Deployed') } properties { rebuild { autoRebuild(false) } }
definition { cpsScm { scm { git { remote { url(sshRepo) credentials('GitLab') } branch('master') scriptPath('Jenkinsfile') extensions { } // required as otherwise it may try to tag the repo, which you may not want } } } } } |
This code snippet will create a freestyle job in Jenkins. It is designed to take the required files from Git and I have also provided a small shell script option.
// This will create a freestyle job.
job('Onboard_Freestyle_Job') { def sshRepo = 'https://gitlab.example.com/DSL_code/democode.git' scm { git { remote { url(sshRepo) credentials('GitLab') } branch('master') } } triggers { scm('*/15 * * * *') } steps { shell('ls -l') } } |
You can copy all the above 3 code snippets and paste them one after the another and they will trigger sequentially.
 |
Output of SeedJob |
 |
Output with View and Jobs Created |
This is a very useful Jenkins plugin and could be customised according to your use-cases. For more information and functions of the Jenkins DSL Plugin, you can refer to the official documentation.
Also, you can refer to these pages which could be useful for you.
Comments
Post a Comment