如何在声明性管道(Jenkinsfile)中配置动态参数?

如何在声明性管道(Jenkinsfile)中配置动态参数?,jenkins,jenkins-pipeline,jenkins-groovy,Jenkins,Jenkins Pipeline,Jenkins Groovy,使用声明性管道语法,我希望能够基于repo数组定义参数,以便在启动构建时,用户可以选中/取消选中作业运行时不应包含的repo final String[] repos = [ 'one', 'two', 'three', ] pipeline { parameters { booleanParam(name: ...) // static param // now include a booleanParam for each item in the `rep

使用声明性管道语法,我希望能够基于repo数组定义参数,以便在启动构建时,用户可以选中/取消选中作业运行时不应包含的repo

final String[] repos = [
  'one',
  'two',
  'three',
]

pipeline {
  parameters {
    booleanParam(name: ...) // static param

    // now include a booleanParam for each item in the `repos` array
    // like this but it's not allowed
    script {
      repos.each {
        booleanParam(name: it, defaultValue: true, description: "Include the ${it} repo in the release?")
      }
    }
  }

  // later on, I'll loop through each repo and do stuff only if its value in `params` is `true`
}

当然,在
参数
块中不能有
脚本
,因此这不起作用。如何实现这一点?

您可以使用Jenkins的
CHOICE
参数,用户可以在该参数中选择存储库

pipeline {
    agent any
    parameters
    {
     choice(name: "REPOS", choices: ['REPO1', 'REPO2', 'REPO3'])
    }
    stages {
           stage ('stage 1') {
                 steps { 
                   // the repository selected by user will be printed
                    println("$params.REPOS")
                 }
            }
    } 
    
    
}
如果要进行多选,还可以使用plugin
Active Choices参数


您可以访问管道语法并以下面的方式进行配置以生成代码段,您可以将其放入jenkins文件中:


复制代码片段并在开始时将其粘贴到Jenkins文件中