Конвейер Дженкинса, как выполнить sh из скрипта

Мне нужно запустить sh, чтобы получить теги git из сценария Jenkins. Ш это:

def tags = sh script: "git ls-remote --tags --refs ssh://[email protected] | 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://[email protected] | 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://[email protected] | cut -d'/' -f3")
    println  "Installer tags:"
    println  tags

но тогда я не знаю, как передать переменную tags в скрипт '''<>'''.

Любая помощь будет оценена по достоинству.


1
1 104
1

Ответ:

Сначала вам нужно получить теги в виде строки в глобальную переменную. Затем внутри сценария выбора вам нужно проанализировать эту строку и создать массив def tags = '$tags'.split(/\r?\n/). И чтобы ссылаться на эту переменную в скрипте (которая сама по себе является строкой внутри конвейера scirpt), вам нужно использовать двойные кавычки.

Для конвейера со сценарием должно работать что-то вроде этого:

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
                ]
            ]
        ]
    ]
])

или если вы хотите добавить в сценарий немного сладкого сахара:

                    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)
                    """,