首页 > 解决方案 > Jenkinsfile 主动选择参数插件(动态 git 分支名称)

问题描述

我需要在 jenkinsfile 中使用 Active Choices Reactive Parameter 和下面的 groovy 脚本:

def gettags = ("git ls-remote -t -h https://USER:PASS@bitbucket.org/project_name.git").execute()
return gettags.text.readLines().collect { 
  it.split()[1].replaceAll('refs/heads/', '').replaceAll('refs/tags/', '').replaceAll("\\^\\{\\}", '')
}

如何在我的 jenkinsfile 中定义这个参数?

我使用了以下代码:

properties([ 
    parameters([
        [$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', 
                  description: 'Select code branch name', 
                  name: 'code_release_branch', 
                  script: [$class: 'GroovyScript', 
                  fallbackScript: [classpath: [], sandbox: false, 
                  script: 'return ["error"]'], 
                  script: [classpath: [], sandbox: false, 
                  script: '''def gettags=("git ls-remote -t -h ssh://git@abc.com/code/code.git").execute()return gettags.text.readLines().collect{it.split([1].replaceAll('refs/heads/','').replaceAll('refs/tags/','').replaceAll("\\^\\{\\}",'')}}''']]],
    ])
])

这会在参数中给出“错误”输出

请注意,这个 groovy 脚本是从 git 动态获取分支名称

我已经尝试过将这个 groovy 脚本放入 Jenkins 托管文件中并将 id 作为参数提供给脚本的另一件事,如下所示,但仍然是同样的问题:

properties([ 
    parameters([
        [$class: 'ChoiceParameter', 
         choiceType: 'PT_SINGLE_SELECT', 
         description: 'Select code branch name', 
         name: 'code_release_branch', script: [
             $class: 'GroovyScript', 
             fallbackScript: [
                   classpath: [],
                   sandbox: false, 
                   script: 'return ["ERROR"]'
             ], 
             script: [
                   classpath: [], 
                   sandbox: false, 
                   script: '6cd2a674-c02d-44b0-87cb-b32f1fcdc0c3'
             ]
        ]],
    ])
 ])

标签: jenkinsgroovyparametersjenkins-pipelinejenkins-groovy

解决方案


推荐阅读