如何在 Jenkins 管道中创建动态复选框参数?

How to create dynamic checkbox parameter in Jenkins pipeline?

我从这个

中找到了如何动态创建输入参数
    agent any
    stages {
        stage("Release scope") {
            steps {
                script {
                    // This list is going to come from a file, and is going to be big.
                    // for example purpose, I am creating a file with 3 items in it.
                    sh "echo \"first\nsecond\nthird\" > ${WORKSPACE}/list"

                    // Load the list into a variable
                    env.LIST = readFile (file: "${WORKSPACE}/list")

                    // Show the select input
                    env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
                            parameters: [choice(name: 'CHOOSE_RELEASE', choices: env.LIST, description: 'What are the choices?')]
                }
                echo "Release scope selected: ${env.RELEASE_SCOPE}"
            }
        }
    }
}

这允许我们只选择一个,因为它是一个 choice 参数,如何使用相同的列表来创建复选框参数,以便用户可以根据需要选择多个?例如:如果用户选择 firstthird,则应打印最后一个 echo Release scope selected: first,third 或以下也很好,所以我可以遍历并找到 trueRelease scope selected: {first: true, second: false, third: true}

我可以使用 extendedChoice 如下

    agent any
    stages {
        stage("Release scope") {
            steps {
                script {
                    // This list is going to come from a file, and is going to be big.
                    // for example purpose, I am creating a file with 3 items in it.
                    sh "echo \"first\nsecond\nthird\" > ${WORKSPACE}/list"

                    // Load the list into a variable
                    env.LIST = readFile("${WORKSPACE}/list").replaceAll(~/\n/, ",")

                    env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
                            parameters: [extendedChoice(
                            name: 'ArchitecturesCh',
                            defaultValue: "${env.BUILD_ARCHS}",
                            multiSelectDelimiter: ',',
                            type: 'PT_CHECKBOX',
                            value: env.LIST
                      )]
                      // Show the select input
                      env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
                            parameters: [choice(name: 'CHOOSE_RELEASE', choices: env.LIST, description: 'What are the choices?')]
                }
                echo "Release scope selected: ${env.RELEASE_SCOPE}"
            }
        }
    }
}

有一个booleanParamhttps://www.jenkins.io/doc/book/pipeline/syntax/#parameters

parameters {
    booleanParam(
        name: 'MY_BOOLEAN',
        defaultValue: true,
        description: 'My boolean')
    }
}

它的名字很奇怪,因为所有其他参数类型中都没有“Param”名称。例如:stringchoice、等等