git - 詹金斯管道,如何从脚本执行 sh
问题描述
我需要运行 ash
从 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
给脚本'''<>'''
。
任何帮助,将不胜感激。
解决方案
您首先需要将标签作为字符串放入全局变量中。在选择脚本中,您需要解析该字符串并创建一个数组def tags = '$tags'.split(/\r?\n/)
。并且要在脚本中引用该变量(它本身是管道脚本中的一个字符串),您需要使用双引号。
对于脚本化的管道,这样的东西应该可以工作:
def tags = ""
node {
stage ('installer') {
println "Checking Installer tags"
tags = sh(returnStdout: true, script: "git ls-remote --tags --refs ssh://git@bitbucket.gameforge.com:7999/gfclient/gfclient-installer.git | cut -d'/' -f3")
println "Installer tags:"
println tags
}
}
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 = '$tags'.split('\n')
for (tag in tags) {
branchNames.push(tag)
}
return branchNames
""",
usePredefinedVariables: true
]
]
]
]
])
或者如果你想在脚本中添加一些时髦的糖:
scriptText: """
def branchNames = ['master']
def tags = '$tags'.split('\n')
tags.each {tag ->
branchNames.push(tag)
}
branchNames
""",
或者,更进一步:
scriptText: """
def branchNames = ['master']
def tags = '$tags'.split(/\r?\n/)
branchNames.addAll(tags)
""",
推荐阅读
- php - 无法在 Laravel 中获取和更新数据库列(使用资源控制器和集体表单)
- ios - 平均 NewRelic 上的 CPU 使用率
- python-3.x - 如何更改熊猫数据框中列的日期时间格式
- android - CastRemoteDisplayLocalService 可以在自定义接收器中使用吗?
- lua - 以下代码中的“模式”是什么意思?
- flutter - Flutter:当孩子溢出时改变堆栈的大小
- batch-file - 如何为网络驱动器映射创建批处理文件
- webhooks - 在环聊聊天室中启用 webhook
- r - 如何处理“text_field 必须引用字符模式列”?
- javascript - 如何运行多个异步 api 函数,然后在全部完成后调用另一个函数?