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.
From the Jenkins dashboard, click on "New Item" in the left-hand navigation pane.
In the "Enter an item name" field, type
parameterized-job
.Select "Freestyle project" as the item type.
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.
On the job configuration page, check the box "This project is parameterized".
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.
Scroll down to the "Build" section.
Click "Add build step" and select "Execute shell".
-
In the "Command" text area, enter the following:
echo "The 'Stage' parameter value is: $Stage" echo "The 'env' parameter value is: $env"
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.
On the
parameterized-job
dashboard, click "Build with Parameters" in the left-hand menu.On the build form, leave "Stage" as its default (
Build
).For the "env" dropdown, select "Production".
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.
- In the "Build History" on the left, click on the latest build number (e.g.,
#1
). - Click "Console Output" to view the build logs.
The console output clearly shows the shell commands executing and echoing the parameter values as expected:
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)
Great consistency 👏