【问题标题】:Groovy - Jenkins Pipeline - Groovy CPS doesn't go trough .eachLine methodGroovy - Jenkins Pipeline - Groovy CPS 不通过 .each Line 方法
【发布时间】:2020-03-23 09:38:53
【问题描述】:

我正在尝试在 Jenkins Pipeline 脚本中运行此代码:

def getTags = { svnurl ->
    def command = ["svn","ls","${svnurl}"];
    def proc = command.execute()
    proc.waitFor()

    proc.in.eachLine {
        println(it)
    }    
}

getTags('http://svnurlexample.net/');

结果应该是 svn 位置的文件夹列表,但我得到的是一个错误:

[管道]回显:

1.0.0/

预计会调用 java.lang.ProcessImpl$ProcessPipeInputStream.eachLine 但最终捕获 org.jenkinsci.plugins.workflow.cps.CpsClosure2.call

是 proc.in.eachLine 引起了这个问题,好像 Groovy 找到了该位置的第一个文件夹但无法处理其余的并报告错误。

【问题讨论】:

  • 几个长镜头:尝试使用@NonCPS 进行注释,或者这可能是因为execute 方法将始终来自Jenkins master(此代码似乎独立于构建代理,但以防万一来自环境的影响在这里)。
  • 我认为 .eachLine 是 CPS 上下文中不支持的方法之一。正如马特建议的那样,要用@NonCPS 注释getTags,你必须把它变成一个函数。
  • 您好,感谢您的回复。你能写一个函数示例吗?

标签: jenkins svn groovy jenkins-pipeline


【解决方案1】:

这对我有用:

@NonCPS
def getTags (svnurl) {
    def command = ["svn","ls","${svnurl}"];
    def proc = command.execute()
    proc.waitFor()

    proc.in.eachLine {
        println(it)
    }    
}

getTags('http://svnurlexample.net/');

【讨论】:

    猜你喜欢
    • 2021-07-23
    • 2017-02-24
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    相关资源
    最近更新 更多