Reputation: 1752
I need to run a sh
to fetch the git tags from a Jenkins script.
The sh is:
def tags = sh script: "git ls-remote --tags --refs ssh://[email protected] | cut -d'/' -f3", returnStdout: true
But it seems not to work. No idea why as Jenkins does not complain but branchNames
is empty.
Following what I'm trying to run:
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://[email protected] | cut -d'/' -f3", returnStdout: true
for (tag in tags) {
branchNames.push(tag)
}
return branchNames
''',
usePredefinedVariables: true
]
]
]
]
])
I can call the sh
before and it works:
stage ('installer') {
println "Checking Installer tags"
def tags = sh(returnStdout: true, script: "git ls-remote --tags --refs ssh://[email protected] | cut -d'/' -f3")
println "Installer tags:"
println tags
but then I don't know how to pass the variable tags
to the script '''<>'''
.
Any help would be appreciated.
Upvotes: 1
Views: 3165
Reputation: 5149
You first need to get the tags as a string into a global variable. Inside the choices script you need then to parse that string and create an array def tags = '$tags'.split(/\r?\n/)
. And to reference that variable in the script (which is a string itself inside the pipeline scirpt) you need to use double-quotes.
For a scripted pipeline something like this should work:
def tags = ""
node {
stage ('installer') {
println "Checking Installer tags"
tags = sh(returnStdout: true, script: "git ls-remote --tags --refs ssh://[email protected]: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
]
]
]
]
])
or if you want to add some groovy sugar to the script:
scriptText: """
def branchNames = ['master']
def tags = '$tags'.split('\n')
tags.each {tag ->
branchNames.push(tag)
}
branchNames
""",
or, going further:
scriptText: """
def branchNames = ['master']
def tags = '$tags'.split(/\r?\n/)
branchNames.addAll(tags)
""",
Upvotes: 1