DEV Community

joy
joy

Posted on

Creating Parametrized builds using Jenkins

Introduction

Jenkins is a tool that makes life easier by automating the tasks we repeat over and over, whether it’s building code, running tests, or deploying an application. It is platform and environment agnostic, giving teams the freedom to integrate with almost any tool or technology. Acting as a centralized automation hub, Jenkins can be customized and extended through its extensive plugin ecosystem to fit nearly any workflow.

One feature that really brings out its flexibility is parameterized builds. Instead of creating a new job every time you want to deploy to a different environment or use a different version, you can simply add the options you want when starting the build. This enables a user to save on time, and allows a single job to run in multiple ways depending on your needs.

This article will show how Jenkins is able to use parameterized builds, that allow you to customize a job based on the inputs you provide before it runs. The steps below walk through how to set them up.

Prerequisites

1.Jenkins installed on your device.
2.Basic understanding of your project; Know what inputs you want to provide for example if it's environment, version or module.
3.A web browser to interact with Jenkins.

Step 1: Once Jenkins is installed, open it in a web browser using http://localhost:8080 and log in with the admin password.

Step 2: In the Jenkins dashboard, click Create Job, enter a name for your job, and select the Freestyle project option.

Creating a new project

Step 3: After naming the job, go to the Configure tab to set up the parameterized build. Jenkins offers eight types of parameters, but for this tutorial, we’ll focus on String and Choice parameters. Start by creating a String parameter named Stage with a default value of Build.

Creating a String Parameter

Step 4 : Next, add a Choice parameter named Env with three values: Development, Staging, and Production.

Adding the Choice Parameter with Development, Staging and Production options

Step 5: After adding the parameters, navigate to the Build section and click Add build step.

Build Steps added with the env commands

Step 6: Select Execute shell and enter commands that reference the Stage and Env parameters. For example we are using this in the build as shown in the previous:

echo "Current Stage: $Stage"
echo "Deploying to Environment: $Env"

Enter fullscreen mode Exit fullscreen mode

Running these commands shows the parameter values you entered for the job, confirming they’re applied correctly and highlighting how they guide the job’s execution.

Step 7: Once done, save the job. You will be redirected to the job homepage. Click Build with Parameters to see the input fields and run the build.

Build trigger created by env

Step 8: Select a value for Env (Development, Staging, or Production) as displayed above and trigger the build. After the build completes, click the build number to see the results. Select Console Output to view detailed information about the build execution as shown below.

Console Output displayed

The console output will show the messages from your shell commands, confirming which stage and environment were used for that run.

Conclusion

In conclusion, we were able create parameterized builds using Jenkins which makes creating jobs much more flexible. We used inputs like environment or version before running the job, therefore enabling it's reuse for different situations. Parameterized builds therefore saves time, and makes automation smoother and easier to manage.

Top comments (0)