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

6mzjoqzu  于 2023-02-18  发布在  Jenkins
关注(0)|答案(2)|浏览(342)

我发现了如何从这个SO answer动态创建输入参数

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}

xxe27gdn

xxe27gdn1#

我可以使用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}"
            }
        }
    }
}
n9vozmp4

n9vozmp42#

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

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

它的名字很奇怪,因为所有其他的param类型都没有“Param”这个名字。stringchoice等等。

相关问题