我需要在groovy脚本下面的jenkinsfile中使用Active Choices反应参数:
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("\\^\\{\\}",'')}}''']]],
])
])
这会在参数中给出“错误”输出
请注意,该常规脚本是从git动态获取分支名称
我还尝试了另一件事,将这个时髦的脚本放入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'
]
]],
])
])