【问题标题】:Jenkins pipeline, how to execute a sh from a script詹金斯管道,如何从脚本执行 sh
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,但我无法对其进行测试,因为def tags = sh(returnStdout: true, script: "git ls-remote --tags --refs ssh://git@bitbucket.xxx.git | cut -d'/' -f3") 需要权限并且我已经失去了太多时间。我解决了添加一个简单的文本字段,我们可以在其中手动编写标签。
猜你喜欢
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 2017-08-01
相关资源
最近更新 更多