【发布时间】:2018-12-18 00:48:30
【问题描述】:
我需要运行sh 来从 Jenkins 脚本中获取 git 标签。
sh 是:
def tags = sh script: "git ls-remote --tags --refs ssh://git@bitbucket.xxx.git | cut -d'/' -f3", returnStdout: true
但它似乎不起作用。不知道为什么詹金斯没有抱怨,但branchNames 是空的。
按照我正在尝试运行的内容:
ProjectUtils.addProperties([
[$class: 'ParametersDefinitionProperty',
parameterDefinitions: [
[$class: 'ExtensibleChoiceParameterDefinition',
name: 'INSTALLER_BRANCH', description: 'The set of installers to be deployed.',
editable: false,
choiceListProvider: [$class: 'SystemGroovyChoiceListProvider',
scriptText: '''
def branchNames = ['master']
def tags = sh script: "git ls-remote --tags --refs ssh://git@bitbucket.xxx.git | cut -d'/' -f3", returnStdout: true
for (tag in tags) {
branchNames.push(tag)
}
return branchNames
''',
usePredefinedVariables: true
]
]
]
]
])
我之前可以拨打sh,它可以工作:
stage ('installer') {
println "Checking Installer tags"
def tags = sh(returnStdout: true, script: "git ls-remote --tags --refs ssh://git@bitbucket.xxx.git | cut -d'/' -f3")
println "Installer tags:"
println tags
但是我不知道如何将变量tags 传递给脚本'''<>'''。
任何帮助将不胜感激。
【问题讨论】:
-
两个建议,不确定是否回答了 cmets,您可以尝试在脚本中添加 .trim()
def tags = sh(' script: "git ls-remote --tags --refs ssh://git@bitbucket.xxx.git | cut -d'/' -f3", returnStdout: true').trim()如果这不起作用,请将脚本更改为 Gstring """"" " 所以你可以使用插值,将标签作为 ${tags} -
谢谢,但它们都不起作用
-
不幸的是,我对声明式管道了解不多,但我知道你不能在选择参数脚本中使用像 sh 这样的管道步骤。剩下的就是选项 2。您需要在 Jenkinsfile 中全局声明 tags 变量。比在脚本中使用像“”“$tags”””这样的双引号来使用它。
标签: git jenkins sh jenkins-pipeline