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

时间:2021-06-02 21:26:24

标签: jenkins jenkins-pipeline jenkins-groovy

使用声明性管道语法,我希望能够根据存储库数组定义参数,以便在开始构建时,用户可以选中/取消选中作业运行时不应包含的存储库。< /p>

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`
}

当然,您不能在 script 块中包含 parameters,因此这不起作用。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用 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")
                 }
            }
    } 
    
    
}

如果你想做多选,你也可以使用插件Active Choices Parameter
https://plugins.jenkins.io/uno-choice/#documentation
您可以访问管道语法并按以下方式配置以生成代码片段并将其放入 jenkins 文件中:
enter image description here

enter image description here

enter image description here


复制代码片段并将其粘贴到开头的 jenkinsfile 中。