【问题标题】:How to run python commands using groovy in Jenkins slaves' Script Console?如何在 Jenkins slaves 的脚本控制台中使用 groovy 运行 python 命令?
【发布时间】:2015-12-31 03:26:08
【问题描述】:

我需要在 Jenkins 的奴隶脚本控制台之一上运行像 python -c "print('hello')" 这样简单的任意东西。这是我正在尝试的:

def cmd = 'python -c "print(\'hello\')"'
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = cmd.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout\nerr> $serr"

但是,得到空输出:

out> 
err> 

有没有办法在 Groovy 中获取 python 的输出?

【问题讨论】:

    标签: python jenkins groovy


    【解决方案1】:

    尝试将命令分成数组

    def cmdArray = ["python", "-c", "print('hello')"]
    def cmd = cmdArray.execute()
    cmd.waitForOrKill(1000)
    println cmd.text
    

    不知道为什么你的版本不工作。

    【讨论】:

    • 我怀疑python创建子进程来运行代码,groovy无法跟踪(子进程)......但这只是我的猜测。不知道如何检查它。
    【解决方案2】:

    这非常适合我:

    def cmd = 'python -c "print(\'hello\')"'
    def proc = cmd.execute()
    proc.waitFor()
    println "return code: ${ proc.exitValue()}"
    println "stderr: ${proc.err.text}"
    println "stdout: ${proc.in.text}"
    

    使用“执行 Groovy 脚本”(不是“执行系统 ​​groovy 脚本”)

    【讨论】:

    • 这给了我:out> err> return code: 0 stderr: stdout:
    • 您使用的是执行系统 ​​Groovy 脚本而不是执行 Groovy 脚本吗? (我编辑了我的答案)
    【解决方案3】:

    Groovy 执行 shell & python 命令

    要在上面提供的答案中添加一个更重要的信息,请考虑 stdoutstderr 用于执行它的 python cmd 或脚本。

    Groovy 添加了execute 方法,使执行shell 变得相当容易,eg: python -c cmd:

    groovy:000> "python -c print('hello_world')".execute()
    ===> java.lang.UNIXProcess@2f62ea70
    

    但是,如果您希望将String 关联到 cmd 标准输出 (stdout) 和/或标准错误 (stderr),那么上面引用的代码没有结果输出

    因此,为了获得 Groovy 执行进程的 cmd 输出,请始终尝试使用:

    String bashCmd = "python -c print('hello_world')"
    def proc = bashCmd.execute()
    def cmdOtputStream = new StringBuffer()
    proc.waitForProcessOutput(cmdOtputStream, System.err)
    print cmdOtputStream.toString()
    

    而不是

    def cmdOtputStream = proc.in.text
    print cmdOtputStream.toString()
    

    通过这种方式,我们在 Groovy 中执行命令后捕获输出,因为后者是阻塞调用 (check ref for reason)。

    带有executeBashCommand func 的完整示例

    String bashCmd1 = "python -c print('hello_world')"
    println "bashCmd1: ${bashCmd1}"
    String bashCmdStdOut = executeBashCommand(bashCmd1)
    print "[DEBUG] cmd output: ${bashCmdStdOut}\n"
    
    
    String bashCmd2 = "sh aws_route53_tests_int.sh"
    println "bashCmd2: ${bashCmd2}"
    bashCmdStdOut = executeBashCommand(bashCmd2)
    print "[DEBUG] cmd output: ${bashCmdStdOut}\n"
    
    def static executeBashCommand(shCmd){
        def proc = shCmd.execute()
        def outputStream = new StringBuffer()
        proc.waitForProcessOutput(outputStream, System.err)
        return outputStream.toString().trim()
    }
    

    输出

    bashCmd1: python -c print('hello_world')
    [DEBUG] cmd output: hello_world
    bashCmd2: sh aws_route53_tests_int.sh
    [DEBUG] cmd output: hello world script
    

    注意1:如上面的代码(bashCmd2)示例所示,对于更复杂的python脚本,您应该通过.sh bash shell脚本执行它。

    注意2:所有示例均已在

    下测试
    $ groovy -v
    Groovy Version: 2.4.11 JVM: 1.8.0_191 Vendor: Oracle Corporation OS: Linux
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 2018-02-05
      相关资源
      最近更新 更多