【问题标题】:How to create dynamic checkbox parameter in Jenkins pipeline?如何在 Jenkins 管道中创建动态复选框参数?
【发布时间】:2020-09-20 17:26:08
【问题描述】:

我发现了如何从这个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,那么最后一个回显应该打印 Release scope selected: first,third 或以下也可以,所以我可以遍历并找到 trueRelease scope selected: {first: true, second: false, third: true}

【问题讨论】:

    标签: jenkins groovy jenkins-pipeline pipeline jenkins-groovy


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-07
        • 2022-10-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多