Automating Jenkins Jobs Using Job DSL Plugin
In the world of continuous integration and continuous delivery (CI/CD), Jenkins has emerged as one of the most popular and powerful automation tools. Its extensibility and flexibility make it a preferred choice for automating various tasks and processes within CICD pipelines.

One such powerful tool within Jenkins is the Job DSL (Domain Specific Language) plugin, which enables the automation of Jenkins job creation and configuration.
In this article, I will explore the benefits of using the Job DSL plugin and provide a step-by-step guide on how to automate Jenkins jobs using this plugin.
Understanding the Seed Job Concept
The seed job concept is a powerful approach to automating Jenkins job creation and management. In essence, a seed job is a Jenkins job that generates and maintains other jobs based on predefined Job DSL scripts. Instead of manually creating and configuring individual jobs, you define a seed job that, when executed, reads Job DSL scripts and creates or updates Jenkins jobs accordingly.
Benefits of Using Job DSL Plugin
The Job DSL plugin offers several advantages that make it a valuable asset for managing Jenkins jobs:
Code as Infrastructure: With the Job DSL plugin, you can define your Jenkins job configurations as code. This treats infrastructure (Jenkins jobs in this case) in the same way you treat application code. It allows version control, code reviews, and the ability to reproduce job configurations easily.
Consistency and Reusability: Writing Jenkins job configurations as code ensures consistency across your projects. You can define job templates, configurations, and steps that can be reused across multiple projects, reducing duplication and manual errors.
Simplified Maintenance: Job configurations defined as code are easier to maintain compared to the traditional point-and-click approach. Changes can be tracked, reviewed, and deployed more efficiently, promoting collaboration and reducing the risk of configuration drift.
Versioning and Rollback: Just like application code, Jenkins job configurations can be versioned. This enables you to roll back to previous configurations if needed, ensuring stability and reliability in your CI/CD pipelines.
Scalability: As your project grows and more Jenkins jobs are required, managing them manually becomes more complex. The Job DSL plugin allows you to scale your automation efforts by generating and managing jobs programmatically.
Now let’s dive into the process of automating Jenkins jobs using the Job DSL plugin.
Step 1: Install the Job DSL Plugin
- Log in to your Jenkins instance.
- Navigate to “Manage Jenkins” > “Manage Plugins.”
- In the “Available” tab, search for “Job DSL” and install the plugin.
- Restart Jenkins if needed.

Step 2: Create a Job DSL Script
- Once the plugin is installed, create a new job in Jenkins (a “Freestyle project” is suitable for this purpose).

2. In the job configuration, under the “Build” section, add a build step of type “Process Job DSLs.”

3. Within this build step, you can either inline your DSL script or provide a path to an external DSL script file.
Step 3: Write Your DSL Script
Now it’s time to write your Job DSL script. The script is written in Groovy, a powerful scripting language that integrates seamlessly with Jenkins.
Here’s a simple example of a Job DSL script that creates a basic Jenkins job that has a basic shell command:

Step 4: Build the Job
Save your DSL script and run the Jenkins job you created in Step 2. This will execute the DSL script and generate the corresponding Jenkins job(s) based on the defined configuration.
if you get an error message like this you have to approve the script by Navigating to “Manage Jenkins” > “In-process Script Approval”

After the build, you can see that the script has generated a Jenkins job called “example”.Go to configure in the example job and you ll see it has been configured to run the shell command



Let's Try Another One.
Use the above dsl script at the “Process Job DSLs” build step. This script will create a Jenkins job called “MyAutomatedJob” that has SCM configuration as well
Now let us dive into more advanced automation scripts
Most of the time we use Pipeline jobs in Jenkins. So can we automate Jenkins Pipeline Jobs as well? Of Course, we can.
This script will generate a Pipeline job called “test-pipeline” and it configures the job to use a Jenkinsfile stored in a git repository with credentials.
You can create multiple Jenkins jobs using one single Groovy script like this. Just execute the Jenkins seed job with DSL script and generate the corresponding Jenkins job(s) based on the defined configuration.
If you are maintaining a Folder architecture in Jenkins you can automate the folder creation stuff using Job DSL.
This will create a Folder called “ABCDE” inside your Jenkins instance.
Automating Jenkins jobs using the Job DSL plugin offers a streamlined and efficient way to manage your CI/CD pipelines. By treating Jenkins job configurations as code, you gain consistency, reusability, and scalability while simplifying maintenance and enabling version control.
For more insightful content on topics like DevOps and automation, feel free to follow me on Medium and LinkedIn.
Stay updated and keep enhancing your knowledge in the world of DevOps practices.