Jenkins Job Creation using Multibranch Job DSL

Introduction

In this blog, we will explore the world of Jenkins job DSL and learn how to leverage its capabilities to streamline and automate job configuration management. We will walk through the process of setting up the Job DSL environment, writing Job DSL scripts to define different types of jobs, managing job configurations as code, and integrating Job DSL with your CI/CD pipelines.

Prerequisites

Before getting started, ensure that you have the following prerequisites in place:

  • Jenkins is installed and running.
  • Job DSL plugin is installed and configured in Jenkins.

What is Job DSL?

Job DSL is a powerful plugin that allows you to define Jenkins jobs as code, bringing the benefits of version control, automation, and scalability to job configuration management. With Job DSL, you can write Groovy scripts to define Jenkins job configurations programmatically. 

DSL stands for Domain Specific Language.

Some of the important methods in Job DSL are

a. folder: You can create a folder using the folder DSL method. Folders provide a way to organize your jobs and create a hierarchical structure in Jenkins. Here’s an example of how to create a folder using the Job DSL:

folder('Microservice') {
    displayName('Microservice')
    description('Folder for Microservice')
}

b. pipelineJob: To create a Pipeline job, you can use the pipelineJob DSL method. The pipelineJob the method allows you to define a Pipeline job with its stages and configuration. Here’s an example of how to create a Pipeline job using the Job DSL:

pipelineJob('sample-app') {
  definition {
    cpsScm {
      scm {
        git {
          remote {
            url("https://gitlab.com/tarandeepsingh009/infrastructure.git")
            credentials('gitlab-token')
          }
          branch('main')
        }
      }
      scriptPath("Jenkinsfile")
    }
  }
}

c. multibranchPipelineJob: To create a Multi-Branch Pipeline job, you can use the multibranchPipelineJob DSL method. The multibranchPipelineJob method allows you to define a Multi-Branch Pipeline job that automatically discovers and builds branches in your source code repository. Here’s an example of how to create a Multi-Branch Pipeline job using the Job DSL:

multibranchPipelineJob('sample-app') {
    branchSources {
      branchSource {
        source {
        git {
            id('sample-app')
            remote('https://gitlab.com/tarandeepsingh009/infrastructure.git')
                credentialsId('gitlab-token')
        }
    }
        strategy {
                    defaultBranchPropertyStrategy {
                        props {
                            noTriggerBranchProperty()
                    }
                }
            }
        }
    }
    configure {
        def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
        traits << 'jenkins.plugins.git.traits.BranchDiscoveryTrait' {}
    }
        factory {
            workflowBranchProjectFactory {
                scriptPath('Jenkinsfile')
            }
        }
    orphanedItemStrategy {
        discardOldItems {
            numToKeep(20)
        }
    }
}

Let’s move ahead with implementation.

Step 1: Login to Jenkins server. 

Step 2: Click on Manage Jenkins and go to Manage Plugins section.

Step 3: In the Available plugins search for Job DSL and then install it.

Step 4: Once you have installed. Click on New Item, enter your item name and select Freestyle Project.

Step 5: Enter your Git repo URL and in the build steps dropdown, search for Process Job DSLs.

Step 6: Select Look on Filesystem as we have stored our Job DSL code in VCS and enter your seed job name.

Step 7: Save it and then click on Build Now.

Here is the output:

In the example below, we are creating a folder and inside that folder creating a pipeline job structure.

folder('Microservice') {
    displayName('Microservice')
    description('Folder for Microservice')
}

folder('Microservice/application') {
  pipelineJob('Microservice/application/sample-app') {
  definition {
    cpsScm {
      scm {
        git {
          remote {
            url("https://gitlab.com/tarandeepsingh009/infrastructure.git")
            credentials('gitlab-token')
          }
          branch('main')
         }
      }
      scriptPath("Jenkinsfile")
      }
    }
  }
}

Output of Pipeline Job:

The above example was for a single branched setup. What if we create a new branch for adding/testing a feature and want to test such code? For this, we need to edit our seed job, change branch name and re-run it.

You can see that every time we need to make such changes for running the specific branch. 

Here comes the concept of Multibranch pipeline. In the example below, we are creating a folder and inside that folder creating a multibranch pipeline job.

folder('Microservice') {
    displayName('Microservice')
    description('Folder for Microservice')
}

folder('Microservice/application') {
  multibranchPipelineJob('Microservice/application/sample-app') {
    branchSources {
      branchSource {
        source {
         git {
          id('sample-app')
          remote('https://gitlab.com/tarandeepsingh009/infrastructure.git')
          credentialsId('gitlab-token')
          }
        }
        strategy {
                    defaultBranchPropertyStrategy {
                        props {
                            noTriggerBranchProperty()
                       }
                  }
             }
         }
    }
    configure {
        def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
        traits << 'jenkins.plugins.git.traits.BranchDiscoveryTrait' {}
    }
        factory {
            workflowBranchProjectFactory {
                scriptPath('Jenkinsfile')
            }
        }
    orphanedItemStrategy {
        discardOldItems {
            numToKeep(20)
        }
     }
   }
}

Using multibranch pipeline we don’t need to worry about new branch creation in our code. Simply, we just need to click on Scan Multibranch Pipeline Now and it will automatically add the new branches if we have created.

Output of Multibranch Pipeline Job:

Conclusion

In conclusion, by leveraging the Job DSL plugin in Jenkins, we have successfully created folders to organize our jobs, defined pipeline jobs to automate our CI/CD workflows, and utilized multibranch pipeline jobs to dynamically manage branches. With these capabilities, we can streamline our software delivery process and enhance our overall CI/CD capabilities.

Blog Pundits: Deepak Gupta and Sandeep Rawat

OpsTree is an End-to-End DevOps Solution Provider.

Connect with Us

Author: tarandeepsingh009

Automation, DevOps, AWS, CI/CD, open-source, technical blogs

Leave a Reply