DEV Community

Wycliffe A. Onyango
Wycliffe A. Onyango

Posted on

100 Days of DevOps: Day 72

Mastering Jenkins' Parameterized Builds

1. Introduction

Understanding Jenkins' core functionalities is crucial. Today's task is to create and run a simple parameterized Jenkins job. This exercise not only familiarizes us with the Jenkins UI but also demonstrates how to build jobs that accept user input, making them highly flexible and reusable.

This article details the step-by-step process of configuring and executing a parameterized Jenkins job, highlighting the configuration points and showcasing the successful execution.

2. Task Overview

The objective was to create a Jenkins Freestyle project named parameterized-job with two parameters:

  • String Parameter: Stage (default value: Build)
  • Choice Parameter: env (choices: Development, Staging, Production)

The job needed to execute a shell command that echoes the values passed to these parameters. Finally, we had to build the job at least once, specifically setting the env parameter to Production, to ensure its successful operation.

3. Step-by-Step Implementation

3.1. Accessing Jenkins and Creating a New Item

After logging into the Jenkins UI, the first step was to initiate the creation of a new job.

  1. From the Jenkins dashboard, click on "New Item" in the left-hand navigation pane.

  2. In the "Enter an item name" field, type parameterized-job.

  3. Select "Freestyle project" as the item type.

  4. Click "OK".

3.2. Configuring Parameters

With the job shell created, the next crucial step was to make it parameterized. This involved adding a String parameter and a Choice parameter.

  1. On the job configuration page, check the box "This project is parameterized".

  2. Click "Add Parameter" and select "String Parameter".

  • Set Name: Stage
  • Set Default Value: Build

3.Click "Add Parameter" again and select "Choice Parameter".

  • Set Name: env
  • Enter the choices (one per line):

    Development
    Staging
    Production
    

3.3. Defining the Build Step (Shell Command)

To demonstrate that the parameters are being correctly received, a simple shell command was added to echo their values.

  1. Scroll down to the "Build" section.

  2. Click "Add build step" and select "Execute shell".

  3. In the "Command" text area, enter the following:

    echo "The 'Stage' parameter value is: $Stage"
    echo "The 'env' parameter value is: $env"
    
  4. Finally, click "Save" at the bottom of the page to apply all changes.

3.4. Building the Job with Specific Parameters

The final step was to execute the job, ensuring we passed Production as the value for the env parameter.

  1. On the parameterized-job dashboard, click "Build with Parameters" in the left-hand menu.

  2. On the build form, leave "Stage" as its default (Build).

  3. For the "env" dropdown, select "Production".

  4. Click the "Build" button.

4. Verification and Console Output

After the build was triggered, we could monitor its progress and review the output to confirm that the parameters were correctly passed and processed.

  1. In the "Build History" on the left, click on the latest build number (e.g., #1).
  2. Click "Console Output" to view the build logs.

The console output clearly shows the shell commands executing and echoing the parameter values as expected:

Output

5. Conclusion

This exercise successfully demonstrated the creation and execution of a parameterized Jenkins job. We now understand how to define various types of parameters, integrate them into build steps, and trigger builds with specific inputs. This foundational knowledge is essential for developing more complex and dynamic CI/CD pipelines.

The job ran successfully, confirming that Jenkins correctly interpreted and utilized the provided parameters, marking the completion of this task.

Top comments (1)

Collapse
 
chaitrali_kakde profile image
Chaitrali Kakde

Great consistency 👏