Understanding Jenkins Jobs: Essential Component of CI/CD Automation

Introduction:

In the realm of integration and continuous delivery (CI/CD) Jenkins is an automation server that helps streamline software development pipelines. At its core Jenkins relies on jobs which’re essential for automating tasks throughout the software development lifecycle. In this guide we will explore the intricacies of Jenkins jobs, including their definition, components and importance, in facilitating CI/CD workflows. Let’s uncover the elements that make up Jenkins jobs and how they contribute to optimizing development processes and expediting software delivery.

What Is a Job in Jenkins?

In Jenkins, a job is a fundamental unit of work that defines a specific task or set of tasks to be executed as part of a continuous integration or continuous delivery (CI/CD) pipeline. Jobs in Jenkins can encompass various activities such as compiling code, running tests, deploying applications, and performing other automation tasks.

By creating and managing jobs in Jenkins, teams can automate repetitive tasks, standardize development processes, and achieve faster and more reliable software delivery. Jobs serve as the building blocks of CI/CD pipelines, enabling organizations to streamline their software development workflows and accelerate time-to-market.

What Is a Build in Jenkins?

A build in Jenkins is a particular execution of a Jenkins job. You can run Jenkins jobs multiple times, and each execution gets a unique build number. All the details pertaining to a particular execution, like artifacts created, console logs, and so on, are stored with that build number.

What Is a Free-Style Job in Jenkins?

Jenkins offers various job types, including pipeline and free-style jobs, tailored to project requirements. Free-style jobs are versatile, handling tasks like running tests, building applications, or generating reports. They’re ideal for simple build tasks and serve as the focus of this blog’s exploration. Further insights on free-style jobs are elaborated in upcoming sections.

How to Create a Job in Jenkins

To create a job in Jenkins, follow these steps:

1. Log in to the Jenkins system and navigate to the dashboard.

2. Click on either the “Create a Job” or “New Item” link.

3. If the dashboard is empty, you’ll see the “Create a Job” link. Otherwise, click the “New Item” link on the left side.

4. You’ll be directed to a page where you can enter the name of the job.

5. Provide a suitable name for your job and select the “Freestyle Project” option.

6. Click the “OK” button, which will create a new job

                                                 The field to enter the name of a Jenkins job

7. After clicking on “OK” button, you will land on the job configuration page

                                                         The Job Configuration page

How to Configure a Job in Jenkins

To configure a job in Jenkins, follow these steps:

1. On the Job Configuration page, you’ll find various sections to adjust settings.

2. In the General section:

  • Describe the job’s purpose in the Description field.
  • Manage build history data with options like Build Age and Build Count to discard old builds fields and save disk space.

                             The settings related to Discard Old Builds

Clicking the Advanced button will reveal two more fields—Days to Keep Artifacts and Max # of Builds to Keep with Artifacts

                                         The advanced settings related to the Discard Old Build option

Configure artifact retention by specifying the number of days builds should be kept and the maximum number of builds to retain.

3. Enable parameterization if the job requires external inputs and define parameter types.

                                          The list of job parameters

                       The String Parameter with values in the Name, Default Value, and Description fields

4. Optionally disable the project if it’s not currently needed or if infrastructure is unavailable.

5. Choose between single or concurrent builds, which can execute multiple builds simultaneously for faster processing.

6. Explore advanced options like Quiet Period, SCM Checkout Retry Count, and downstream/upstream project dependencies for finer control over job execution.

                      The additional options shown after clicking the Advanced button

7. Consider enabling options to block job execution when upstream or downstream projects are building to manage dependencies efficiently.

Custom Workspace:

Before delving into this setting, it’s essential to grasp the concept of a workspace in Jenkins. A workspace serves as the directory where builds are executed. When a Jenkins job checks out a Source Code Repository, it typically does so in this workspace directory. By default, when a build commences, a workspace directory named after the job being executed is created in the ${JENKINS_HOME}\workspace folder. The ${JENKINS_HOME} directory is usually located at ${CurrentUser}\.Jenkins. However, you have the option to change the workspace directory location by specifying the path in this field.

This capability to alter the workspace location can prove beneficial in scenarios where another job relies on the artifacts of the current job, especially if the paths of these artifacts are hard-coded into that job. For instance, if the workspace location is set to D:\MyFirstJenkinsJob, when the job is executed, a directory named MyFirstJenkinsJob will be created on the D:\ drive and used as the workspace.

Display Name:

Setting a display name here allows you to customize how the project is identified throughout the Jenkins WebUI. For example, if the display name is set to “MyJob,” the job will be referred to as “MyJob” on the Jenkins dashboard.

                                         The job is called MyJob on the Jenkins dashboard

It’s worth noting that the display name is only visible within the WebUI. Despite setting a display name, the workspace folder will still be created with the project’s original name and not the display name. For instance, if the job’s name is “MyFirstJenkinsJob” and the display name is “MyJob,” the workspace folder will be created with the name “MyFirstJenkinsJob” rather than “MyJob.”

Keep the Build Logs of Dependencies

This setting is available in the Display Name option, from the Advanced settings page. If this setting is enabled, all builds referenced here are protected from log rotation. Log rotation is the process of automatic compression, deletion, and mailing of the Jenkins build’s logs

Source Code Management

The “Source Code Management” section in Jenkins is crucial for configuring how your Jenkins job interacts with your code repository, especially when using Git. Here’s a breakdown of the key points:

1. Git Plugin Requirement:

The Git option appears in the Source Code Management section only if the Git plugin is installed in Jenkins.

2. Repository URL:

You must provide the URL of the Git code repository in this field. Jenkins will use this URL to download the latest code from the repository.

3. Authentication for Private Repositories:

If your Git repository is private, you need to provide authentication information such as username/password, SSH private key, or API token. This ensures Jenkins can access the repository securely.

The authentication error when Jenkins tries to access a private repository without the required credentials

4. Credentials Management:

To provide authentication information, you need to create a Credentials entry of type Global in Jenkins. This entry will store the required authentication details. You then select this entry in the Credentials dropdown of the job configuration page.

5. Windows Credentials Manager:

Jenkins can also use credentials stored in the Windows Credentials Manager for authentication. This can be useful if your Git credentials are already stored there.

6. Branches to Build:

By default, Jenkins jobs look for changes in the master branch. You can specify a different branch if needed.

7. Build Triggers:

Configure when Jenkins should start running your jobs. Options include triggering builds remotely, triggering builds after other projects are built, and periodic builds.

8. Build Step:

Define the steps to execute in your job, such as running a batch command or build tool.

9. Post-Build Actions:

Configure actions to perform after the job completes, such as sending email notifications.

Understanding and configuring these sections properly is essential for setting up automated build processes effectively in Jenkins.

Running a Job in Jenkins and Checking Its Output:

Once you’ve configured and saved a Jenkins job, you can run it manually or wait for it to be triggered according to its configured schedule. Here’s how to run a job and check its output:

1. Manual Execution:

  • Locate the desired job on the Jenkins dashboard.
  • Initiate the job manually by clicking on its name or the clock icon.
  • Monitor the execution status in the “Build Executor Status” section, where a progress bar indicates ongoing execution.

                        The progress bar indicates that the job is being executed

2. Build History:

  • After the job completes, navigate to the “Build History” link on the left side of the dashboard.
  • This page displays a list of executed builds, typically labeled from Build #1 to the latest build number.

                                                The build history for an executed job

3. Console Output:

  • To inspect detailed logs of a specific build, click the “Console Output” link for the corresponding build entry in the build history.
  • The console output provides comprehensive information about each step of the job’s execution, aiding in troubleshooting and analysis.

                                                         The detail console output of Build

Additional Actions in Jenkins:

Editing a Job:

  • To modify an existing job, access the job’s dropdown menu on the dashboard and select “Configure.”
  • Make necessary modifications to the job configuration and save the changes.

                              The Job Configuration page for the CompileJavaApplication job

Viewing a Job’s Workspace:

  • Click on the job entry to access its details page.
  • Navigate to the “Workspace” link on the left side of the page to view the workspace directory where the job operates.

                                             The workspace for MyJob

Clearing a Job’s Workspace:

  • Within the job’s details page, click the “Workspace” link.
  • Then, select “Wipe Out Current Workspace” to clear the workspace directory.

The Wipe Out Current Workspace menu option under the Workspace menu

Deleting a Job:

  • From the dropdown menu of the desired job on the Jenkins dashboard, choose “Delete Project.”
  • Confirm the deletion in the prompted confirmation window to remove the job permanently.

                                 The Delete Project menu in the dropdown opened for a job

Following these steps ensures smooth execution and effective management of jobs within Jenkins, facilitating streamlined software development processes.

In conclusion, mastering the creation and execution of jobs in Jenkins is fundamental to establishing robust and efficient automated build and deployment pipelines. By understanding how to configure jobs, run them manually or automatically, and analyze their output, users can effectively manage software development workflows. Additionally, the ability to edit, view workspaces, clear workspaces, and delete jobs provides further flexibility and control over the continuous integration and delivery process. With these skills, teams can leverage Jenkins to streamline development, enhance collaboration, and achieve faster and more reliable software releases.

Do you like to read more educational content? Read our blogs at Cloudastra Technologies or contact us for business enquiry at Cloudastra Contact Us.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top