In this article, I will explain how to create parameterized pipelines in Jenkins. A parameterized pipeline allows us to set needed parameters dynamically at build time. Before continuing reading, let’s read previous parts if you didn’t yet.

Follow our social media:

https://www.linkedin.com/in/ssbostan

https://www.linkedin.com/company/kubedemy

https://www.youtube.com/@kubedemy

https://telegram.me/kubedemy

Jenkins Pipeline Parameters:

A set of various parameters can be defined in the pipelines. All of them should be defined in the parameters section of the pipeline.

After running the pipeline for the first time, Build with Parameters is shown in the pipeline menu. From now on, when you want to build, you should set parameters before that start. Along with parameter definition, you can also set their default values.

Pipeline Parameter Types:

Jenkins supports a set of different parameter types. You should note that, in addition to native parameter types, some plugins may add new types. You should read the documents of that plugins to know how their work. In this section, I will introduce native Jenkins parameter types.

booleanParam type lets you define boolean parameters. You can set the default value for this type. Note that all parameters have the description argument you can use to describe the purpose of that parameter goal.

booleanParam(
    name: "NAME",
    defaultValue: true,
    description: "DESCRIPTION"
)

string type allows you to define single-line strings. In addition to default value and description, it supports an additional argument trim to remove white spaces on both sides of the entered value.

string(
    name: "NAME",
    defaultValue: "VALUE",
    trim: true,
    description: "DESCRIPTION"
)

text lets you define multi-line string texts.

text(
    name: "NAME",
    defaultValue: "VALUE",
    description: "DESCRIPTION"
)

password enables you to define password input on the build page. The value of this type is not shown – concealed – on both the build page and the pipeline console.

password(
    name: "NAME",
    defaultValue: "VALUE",
    description: "DESCRIPTION"
)

choice is for defining a multi-choice drop-down menu with a set of pre-defined values. You can define this type with a list of values.

choice(
    name: "NAME",
    choices: ["VALUE1", "VALUE2", "VALUE3"],
    description: "DESCRIPTION"
)

Using Parameter Value in the Pipeline:

Parameter values can be accessed using params helper. In addition to this preferred method, they can be accessed through the environment variables described previously in the previous article. To access the parameter value, use $params.NAME or ${params.NAME} syntax. Parameters can be accessed within all pipeline stages.

echo "Hello $params.NAME"

By using parameters, you can create dynamic pipelines. Imagine you want to create a pipeline that can be worked on multiple environments – development, staging, production – and these environments have different configurations, or you wish to supply configurations at build time. With the parameterized pipeline, you can achieve these goals. How we can do that and make dynamic pipelines will be introduced in future articles.

You can find all tutorial materials in the following GitHub repository:

https://github.com/ssbostan/jenkins-tutorial

If you like this series of articles, please share them and write your thoughts as comments here. Your feedback encourages me to complete this massively planned program.

Follow my LinkedIn https://www.linkedin.com/in/ssbostan

Follow Kubedemy LinkedIn https://www.linkedin.com/company/kubedemy

Follow Kubedemy Telegram https://telegram.me/kubedemy

Leave a Reply

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