Skip to main content

Command Palette

Search for a command to run...

How to Use String and Boolean Parameters Effectively

Published
3 min read
How to Use String and Boolean Parameters Effectively

In the world of DevOps, flexibility is key. Hardcoding values into your CI/CD pipelines can make them rigid and difficult to maintain. If you want to change a branch name, an environment variable, or skip a test suite, you shouldn't have to commit code changes to your Jenkinsfile every time.

This is where Jenkins Pipeline Parameters come in.

In this guide, we will explore two of the most essential parameter types—String and Boolean—and how they can make your pipelines dynamic, reusable, and user-friendly.


1. String Parameter: Dynamic Text Input

A String Parameter is the workhorse of user input in Jenkins. It allows users to enter single-line text at build time, which the pipeline then uses as a variable. This is perfect for inputs that change frequently but don't follow a strict pattern.

Why use it?

  • Dynamic Branch Selection: Build any Git branch (e.g., feature/login, hotfix/v1) without modifying code.

  • Versioning: Pass specific version numbers (e.g., v2.1.0) for release builds.

  • Environment Targets: Specify where to deploy (e.g., dev, staging).

Syntax

The syntax defines the parameter's name, a default value (to prevent errors), and a description for the UI.

Groovy

string(
    name: 'STRING_PARAM',
    defaultValue: 'default value',
    description: 'Enter a string value'
)

Real-World Example: Selecting a Git Branch

Here is a pipeline that lets the user define exactly which Git branch to checkout and build.

Groovy

pipeline {
    agent any

    parameters {
        string(
            name: 'BRANCH_NAME',
            defaultValue: 'main',
            description: 'Enter the Git branch to build'
        )
    }

    stages {
        stage('Git Checkout') {
            steps {
                // The parameter is accessed via params.BRANCH_NAME
                git branch: "${params.BRANCH_NAME}",
                    url: 'https://github.com/divy1436/Boardgame.git'
            }
        }

        stage('Build') {
            steps {
                echo "Building branch: ${params.BRANCH_NAME}"
                sh 'mvn package'
            }
        }
    }
}

2. Boolean Parameter: The On/Off Switch

While String parameters handle text, Boolean Parameters handle logic. They provide a simple checkbox in the Jenkins UI that passes a true or false value to the pipeline. This is primarily used to toggle specific stages or actions on and off.

Why use it?

  • Conditional Testing: Run expensive test suites only when needed.

  • Deployment Controls: Enable or disable a "Deploy to Production" stage.

  • Debug Mode: Turn on verbose logging for troubleshooting.

Syntax

The defaultValue determines if the box is checked (true) or unchecked (false) by default.

Groovy

booleanParam(
    name: 'BOOLEAN_PARAM',
    defaultValue: true,
    description: 'Enable or disable something'
)

Real-World Example: Conditional Test Execution

In this example, the "Test" stage only runs if the user checks the box. If unchecked, the pipeline skips straight to the end, saving time.

Groovy

pipeline {
    agent any

    parameters {
        booleanParam(
            name: 'RUN_TESTS',
            defaultValue: true,
            description: 'Enable or disable test execution'
        )
    }

    stages {
        stage('Build') {
            steps {
                sh 'mvn package'
            }
        }

        stage('Test') {
            // The 'when' block checks the parameter value
            when {
                expression { params.RUN_TESTS }
            }
            steps {
                sh 'mvn test'
            }
        }
    }
}

Summary: String vs. Boolean

Feature

String Parameter

Boolean Parameter

Input Type

Text Box

Checkbox

Data Type

String

Boolean (true/false)

Best Use Case

Branch names, Versions, IPs

Feature flags, Toggles, Skipping stages

Default Behavior

Accepts any text

Checked or Unchecked

Conclusion

Using parameters transforms a static script into a flexible automation tool. By implementing String Parameters, you eliminate hardcoded values, and with Boolean Parameters, you gain granular control over the execution flow.

Start adding these to your Jenkinsfile today to make your builds safer, faster, and much easier for your team to use!