Focused Job Configuration with the Jenkins DSL Plugin

Published:10 May 2022 - 6 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

Organizations are increasingly adopting Jenkins to automate their build and test processes, making manually managing Jenkins jobs difficult. The good news is Jenkins DSL plugin allows you to define Jenkins jobs in a Domain Specific Language (DSL) syntax.

Jenkins DSL makes it possible to store jobs in version control systems, share them with other users, and generate new jobs from templates. And in this tutorial, you’ll learn how to take advantage of the Jenkins DSL plugin to help with automating application development.

Read on, so you never have to click through the Jenkins UI to create a job tediously!

Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following.

  • A Linux machine – This guide uses Ubuntu 20.04.
  • Java 11+ and Jenkins installed on your Linux machine.
  • Git to integrate Jenkins with Git repositories.
  • Pipeline: Job to add the Pipeline suite of plugins used to implement and integrate continuous delivery pipelines in Jenkins.

Installing the Jenkins DSL Plugin

Maintaining consistency between jobs can be difficult, but the Jenkins DSL plugin solves this problem. How? By allowing you to define jobs in a programmatic form, in a human-readable file.

But before creating jobs, you’ll have to install the Jenkins DLS plugin first:

1. SSH to your Jenkins server and run the following command to verify that you have the required -version of java installed and the jenkins service is running.

java -version && systemctl status jenkins

You’re all set to install the Jenkins DSL plugin if you see an output similar to the below.

Checking Java version and Jenkins status
Checking Java version and Jenkins status

2. Next, open your favorite web browser and navigate to http://your_server_ip:8080/pluginManager/available. Replace your_server_ip:8080 with your Jenkins server’s actual IP address and port number.

3. Lastly, install Jenkins DSL Plugin with the following:

  • As shown below, type in Job DSL in the search box on the top right.

If you already have the Job DSL plugin installed and upgraded from an older version, the Job DSL scripts will not appear in the list when searching for Job DSL. You can navigate to http://your_server_ip:8080/pluginManager/installed to check if the plugin Job DSL is there.

  • Tick the checkbox beside the Job DSL option.
  • Click Install without restart at the bottom to install the Jenkins Job DSL plugin.
Installing the Jenkins DSL plugin
Installing the Jenkins DSL plugin

The Jenkins UI will show you a progress bar while installing the plugin. Once installation is complete, you’ll see the Success messages similar below.

Viewing Jenkins DLS Plugin Installation Progress
Viewing Jenkins DLS Plugin Installation Progress

Creating a Jenkins Seed Job Using the Jenkins DSL Plugin

Now that you have the Jenkins DSL plugin installed feel free to create a Jenkins seed job. Write a basic Job DSL script and incorporate the script into a Jenkins seed job. You’ll create a freestyle job that prints out a ‘Hello World!’ message to the Jenkins console.

To incorporate a Job DSL script into a Jenkins seed job:

1. Navigate to your Jenkins Dashboard, and click New Item in the left sidebar. Your browser redirects your browser to a page where you can create a new item (step two).

Adding a New Item
Adding a New Item

2. Next, create a new item with the following:

  • Enter an item name, as shown below. But the item name is set to seed in this tutorial.
  • Select Freestyle project (a repeatable build job, script, or pipeline) from the options below.
  • Click OK at the bottom to create the new item, and your browser redirects to the new item’s configuration page.
Creating seed job
Creating seed job

3. Keep the default settings for all other sections on the new item’s configuration page.

Scroll down to the Build section, click the Add build step dropdown button, and select Process Job DSLs. Doing so opens an option where you’ll provide a DSL script (step four).

Selecting process job DSLs
Selecting process job DSLs

4. Now, select the Use the provided DSL script option, as shown below, and populate the following code block to the field.

This code block is a freestyle job definition named jenkins-job-demo-1 that uses the shell step to print a “Hello World” message. The job’s name is what will appear in the Jenkins UI.

// Sets the API method you want to use (job)
job('jenkins-job-demo-1') { // Sets the name of the job to jenkins-job-demo-1
	// Define what the job does
  steps {
		// Prints a message
    shell('echo Hello World! This job is working!!')
  }
}

There are many available methods to create different types of Jenkins jobs, which you can find in the official documentation.

Providing the DSL Script
Providing the DSL Script

5. Click Save at the bottom of the page to save the configuration for the new item (seed).

Saving Configuration
Saving Configuration

6. Lastly, navigate to your Jenkins Dashboard to verify the seed job you created exists, as shown below.

Verifying the seed Job
Verifying the seed Job

Running a Seed Job

You’ve successfully created and verified the seed job. But how do you know the job works? You’ll run the seed job against the DSL script you provided in step four of the “Creating a Jenkins Seed Job Using the Jenkins DSL Plugin” section.

1. On your Jenkins Dashboard, click on the seed job to navigate its configuration page.

Navigating into the seed Job Page
Navigating into the seed Job Page

2. Next, click Build Now in the left sidebar to run the job.

Running the Seed Job
Running the Seed Job

3. Press F5 to refresh the page. You’ll see a new build appear under the Build History section with a success status (green check) at the bottom left, as shown below.

This output indicates the seed job ran successfully, which created a new Jenkins job (jenkins-job-demo-1) under the Generated Items section.

Viewing the See Job Works and Generated a New Job
Viewing the See Job Works and Generated a New Job

4. Now, navigate to your Jenkins Dashboard. As shown below, you’ll see the new Jenkins job (jenkins-job-demo-1) created by the seed job.

Click on the newly generated job to access its configuration page.

Verifying See Job’s Generated Job
Verifying See Job’s Generated Job

5. Click on Build Now to run the generated job (jenkins-job-demo-1).

Running the Generated Job (jenkins-job-demo-1)
Running the Generated Job (jenkins-job-demo-1)

6. Finally, click the arrow down button next to the successful build under the Build History section, as you see below. Choose Console Output from the context menu to see the job’s output.

Accessing Console Output of the Generated Job (jenkins-job-demo-1)
Accessing Console Output of the Generated Job (jenkins-job-demo-1)

If the generated job works, you’ll see the message saying Hello World! This job is working!!, as shown below, which is the message you defined in the Job DSL script.

At this point, you’ve successfully created and run a Jenkins job using the Job DSL plugin!

Viewing Console Output
Viewing Console Output

Creating a Job for a CI/CD Pipeline Using the Jenkins DSL Plugin

So far, you’ve learned how to create and run a Jenkins freestyle job using the Job DSL plugin. You’ll automate the non-human part of the software development process by creating a Jenkins job for a CI/CD pipeline.

Specifically, you’ll create a job that pulls code from a GitHub repository, builds the code, and runs the unit tests.

To create a Jenkins job for the CI/CD pipeline:

1. Navigate to http://your_server_ip:8080/job/seed/configure in your web browser to configure your seed job. Replace your_server_ip with your Jenkins server IP address, and replace seed with your job’s name.

2. Next, scroll down to the Build section, find the DSL Scripts field, and replace the existing content with the following code.

job('jenkins-job-demo-2') {
    steps {
        shell('echo Hello World!')
    }
}

pipelineJob('github-demo') {
    definition {
        cpsScm {
            scm {
                git {
                    remote {
                        github('jenkinsci/pipeline-examples')
                    }
                }
            }
            scriptPath('declarative-examples/simple-examples/environmentInStage.groovy')
        }
    }
}

Click on Save once you’ve added the code to save the changes.

Providing DSL Script for a Pipeline
Providing DSL Script for a Pipeline

3. Click Build Now in the left sidebar on the next screen to rerun the seed job, as shown below. Under the Build History section (bottom-left), you’ll see that the seed job ran successfully.

Click on the new build (#2) to access the build’s configuration page.

Rerunning the seed Job
Rerunning the seed Job

4. Now, navigate to the Console Output of the new build. You’ll see an Added items message and the name of the new job, as shown below.

This output indicates that the new pipeline job was added successfully.

Verifying the New Pipeline Job was Added Successfully
Verifying the New Pipeline Job was Added Successfully

5. Return to the Jenkin dashboard, and you’ll see the new pipeline job added (jenkins-job-demo-2) to the list of jobs. Click on that new pipeline job to access the job’s configuration page.

Verifying the New Jenkins Job Exists
Verifying the New Jenkins Job Exists

6. Finally, click on Build Now to run the new pipeline job. You will see that the job ran successfully, as indicated under the Build History section.

Click on the dropdown arrow next to the job and select Console Output.

Running the New Jenkins Job
Running the New Jenkins Job

In the Console Output below, you can see the job’s output, that Jenkins successfully cloned the repository, built the code, and ran the unit tests.

Viewing Console Output of the Pipeline Job
Viewing Console Output of the Pipeline Job

Conclusion

In this tutorial, you’ve learned how to create Jenkins jobs with the Job DSL plugin. You’ve touched on enhancing application development by creating a CI/CD pipeline with the Jenkins Pipeline plugin.

The Job DSL plugin and the Jenkins Pipeline plugin work side-by-side to offer a powerful way you can manage Jenkins jobs and build pipelines. Why not configure these plugins to create sophisticated build pipelines that automatically trigger Jenkins jobs as part of the build process?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!