How to pass boolean parameter value in pipeline to downstream jobs?

JenkinsGroovyJenkins Pipeline

Jenkins Problem Overview


I'm using Jenkins v2.1 with the integrated delivery pipeline feature (https://jenkins.io/solutions/pipeline/) to orchestrate two existing builds (build and deploy).

In my parameterized build I have 3 user parameters setup, which also needs to be selectable in the pipeline.

The pipeline script 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 works correctly except for the BooleanParameterValue. When I build the pipeline the following error is thrown:

java.lang.ClassCastException: hudson.model.BooleanParameterValue.value expects boolean but received class java.lang.String

How can I resolve this typecasting error? Or even better, is there a less cumbersome way in which I can just pass ALL the pipeline parameters to the downstream job.

Jenkins Solutions


Solution 1 - Jenkins

In addition to Jesse Glick answer, if you want to pass string parameter then use:

build job: 'your-job-name', 
    parameters: [
        string(name: 'passed_build_number_param', value: String.valueOf(BUILD_NUMBER)),
        string(name: 'complex_param', value: 'prefix-' + String.valueOf(BUILD_NUMBER))
    ]

Solution 2 - Jenkins

Assuming

value: update_composer

was the issue, try

value: Boolean.valueOf(update_composer)

> is there a less cumbersome way in which I can just pass ALL the pipeline parameters to the downstream job

Not that I know of, at least not without using Jenkins API calls and disabling the Groovy sandbox.

Solution 3 - Jenkins

like Jesse Jesse Glick and abguy said you can enumerate string into Boolean type:

Boolean.valueOf(string_variable)

or the opposite Boolean into string:

String.valueOf(boolean_variable)

in my case I had to to downstream Boolean parameter to another job. So for this you will need the use the class BooleanParameterValue :

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

Solution 4 - Jenkins

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

(cf. https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/#-build-%20build%20a%20job)

Solution 5 - Jenkins

Jenkins "boolean" parameters are really just a shortcut for the "choice parameter" type with the choices hardcoded to the strings "true" and "false", and with a checkbox to set the string variable. But in the end, it is just that: a string variable, with nothing to do with a true boolean. That's why you need to convert the string to a boolean if you don't want to do a string comparison like:

if (myBoolean == "true")

Solution 6 - Jenkins

Not sure if this answers this question. But I was looking for something else. Highly recommend see this 2 minute video. If you wanted to get into more details then see docs - Handling Parameters and this link

And then if you have something like blue ocean, the choices would look something like this:

enter image description here

As discussed in the video, Jenkins is blue because it's using Blue Ocean Plugin

You define and access your variables like this:

pipeline {
    agent any

    parameters {
    string(defaultValue: "TEST", description: 'What environment?', name: 'userFlag')
    choice(choices: ['TESTING', 'STAGING', 'PRODUCTION'], description: 'Select field for target environment', name: 'DEPLOY_ENV')
    }

    stages {
        stage("foo") {
            steps {
                echo "flag: ${params.userFlag}"
                echo "flag: ${params.DEPLOY_ENV}"
            }
        }
    }
}

Automated builds will pick up the default params. But if you do it manually then you get the option to choose.

And then assign values like this:

enter image description here

Solution 7 - Jenkins

Things are much easier nowadays: the builtin Snippet Generator supports the 'build' step (I don't know since when though).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBram GerritsenView Question on Stackoverflow
Solution 1 - JenkinsabguyView Answer on Stackoverflow
Solution 2 - JenkinsJesse GlickView Answer on Stackoverflow
Solution 3 - JenkinsdsaydonView Answer on Stackoverflow
Solution 4 - JenkinsRomain DEQUIDTView Answer on Stackoverflow
Solution 5 - JenkinsCosmoView Answer on Stackoverflow
Solution 6 - JenkinsmfaaniView Answer on Stackoverflow
Solution 7 - JenkinsRomanView Answer on Stackoverflow