首页 > 解决方案 > 詹金斯管道,如何从脚本执行 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给脚本'''<>'''

任何帮助,将不胜感激。

标签: gitjenkinsshjenkins-pipeline

解决方案


您首先需要将标签作为字符串放入全局变量中。在选择脚本中,您需要解析该字符串并创建一个数组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)
                    """,

推荐阅读