Type Conversion Issues and Solutions for Boolean Parameter Passing in Jenkins Pipeline

Dec 05, 2025 · Programming · 14 views · 7.8

Keywords: Jenkins Pipeline | Boolean Parameter Passing | Type Conversion

Abstract: This article provides an in-depth analysis of type conversion errors when passing boolean parameters to downstream jobs in Jenkins pipelines. By examining the root cause of ClassCastException, it explains the type differences between strings and boolean values in Groovy and presents effective solutions using the Boolean.valueOf() method. The article also compares various parameter passing approaches, including the BooleanParameterValue class and booleanParam shorthand syntax, helping developers avoid common pitfalls and optimize pipeline scripts.

Problem Background and Error Analysis

When using Jenkins v2.1's integrated delivery pipeline feature, developers often need to pass user parameters from parameterized builds to downstream jobs. Typical scenarios include parameter passing between build and deployment stages. The original pipeline script example is as follows:

node: {
    stage 'build'
    build job: 'build', parameters: [[$class: 'StringParameterValue', name: 'target', value: target], [$class: 'ListSubversionTagsParameterValue', name: 'release', tag: release], [$class: 'BooleanParameterValue', name: 'update_composer', value: update_composer]]

    stage 'deploy'
    build job: 'deploy', parameters: [[$class: 'StringParameterValue', name: 'target', value: target]]
}

This script throws java.lang.ClassCastException: hudson.model.BooleanParameterValue.value expects boolean but received class java.lang.String when passing the boolean parameter update_composer. The root cause of this error lies in type mismatch of parameter values in Groovy scripts.

Core Mechanism of Type Conversion Issues

In Jenkins pipelines, when parameters are passed through Groovy scripts, all user-input parameters are treated as string types by default. Even if a boolean-type checkbox is selected in the frontend interface, the actual value passed to the pipeline script remains in string representation (such as "true" or "false"). The BooleanParameterValue class, when instantiated, expects to receive a primitive boolean value (boolean), not a string.

This type mismatch causes the JVM to fail when attempting to cast a string to a boolean at runtime, resulting in ClassCastException. To understand the essence of this problem, it's important to distinguish between data types in Groovy:

Primary Solution: Explicit Type Conversion

According to the best answer, the most direct solution is to use the Boolean.valueOf() method for explicit type conversion:

build job: 'build', parameters: [
    [$class: 'BooleanParameterValue', name: 'update_composer', value: Boolean.valueOf(update_composer)]
]

Boolean.valueOf() is a static method in the Java standard library that accepts a string parameter and returns the corresponding boolean value. The method works as follows:

  1. If the input string is "true" (case-insensitive), it returns true
  2. Any other string (including "false") returns false
  3. If the input is null, it returns false

This conversion ensures that what is passed to the BooleanParameterValue constructor is indeed a boolean type value, thus avoiding type conversion errors.

Alternative Approaches and Syntax Simplification

In addition to using Boolean.valueOf() for explicit conversion, there are several other methods for passing boolean parameters:

Using BooleanParameterValue Class Directly with Boolean Values

If the parameter value is already of boolean type, it can be used directly:

build job: 'downstream_job_name', parameters: [
    [$class: 'BooleanParameterValue', name: 'parameter_name', value: false]
], wait: true

This approach is suitable for hard-coded boolean values or variables that have already been correctly converted to boolean type.

Using booleanParam Shorthand Syntax

Jenkins Pipeline DSL provides a more concise syntax:

build job: 'downstream_job_name', parameters: [
    booleanParam(name: 'parameter_name', value: false)
]

This syntax still creates a BooleanParameterValue instance under the hood but offers a more readable API. It's important to note that the parameter passed to value must also be of boolean type; if it's a string variable, type conversion is still required first.

Best Practices for Parameter Passing

When handling parameter passing in Jenkins pipelines, it's recommended to follow these best practices:

  1. Always Perform Explicit Type Checking: Ensure that value types match target parameter types before passing parameters.
  2. Use Appropriate Conversion Methods: Beyond Boolean.valueOf(), pay attention to other type conversions, such as using Integer.parseInt() for string-to-number conversion or String.valueOf() for reverse conversion.
  3. Consider Parameter Validation: Add validation logic before passing critical parameters to ensure parameter value validity.
  4. Document Parameter Types: Add comments in pipeline scripts to clarify the expected type of each parameter.

Limitations Discussion

Regarding whether there's a more concise way to automatically pass all pipeline parameters to downstream jobs, current technical limitations include:

Conclusion

Type conversion issues when passing boolean parameters in Jenkins pipelines are common but easily solvable challenges. By understanding the type differences between strings and boolean values in Groovy and using Boolean.valueOf() for explicit conversion, developers can avoid ClassCastException errors. While there's currently no perfect method to automatically pass all parameters to downstream jobs, following type-safe best practices and using simplified DSL syntax enables the creation of more robust and maintainable pipeline scripts.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.