groovy执行间歇性地返回没有输出也没有错误

时间:2016-02-03 10:36:31

标签: jenkins groovy

我尝试使用Groovy脚本(与jenkins-cli.jar一起运行)来验证一些git信息。

为此,我有以下简单的groovy脚本:

def getBranchSha = { branch ->
  def wd = new File('/my/git/repo');
  def sout = new StringBuffer(), serr = new StringBuffer();
  def gitcmd = 'git show-ref ' + branch + ' --hash';
  def proc = gitcmd.execute(null, wd);
  proc.consumeProcessOutput(sout, serr);
  proc.waitFor();

  def sha = sout.toString().trim();
  out.println("exitCode: " + proc.exitValue() + "\terror: " + serr.toString());
  out.println("input: " + branch + "\toutput: \"" + sha + "\"");
  assert sha.equals("10d94325c8fc1050f3e362d2fbb9f9041e6b9360");
}

getBranchSha("origin/master");

我希望这构造一个命令行git show-ref origin/master --hash并在我的git repo的工作目录中执行它。我希望在这个repo 10d94325c8fc1050f3e362d2fbb9f9041e6b9360中,它的输出是我的origin / master分支的当前SHA,并且代码断言是这种情况。

大多数情况下,此代码完全按预期运行,但有时我无法看到,输出为空白:

 > while [ $? -eq 0 ] ; do java -jar jenkins-cli.jar -s http://localhost:8080 groovy tst.groovy ; done
exitCode: 0     error:
input: origin/master    output: "10d94325c8fc1050f3e362d2fbb9f9041e6b9360"
exitCode: 0     error:
input: origin/master    output: "10d94325c8fc1050f3e362d2fbb9f9041e6b9360"

...

exitCode: 0     error:
input: origin/master    output: "10d94325c8fc1050f3e362d2fbb9f9041e6b9360"
exitCode: 0     error:
input: origin/master    output: ""
java.lang.SecurityException: Rejected: org.codehaus.groovy.runtime.powerassert.PowerAssertionError
        at hudson.remoting.ClassFilter.check(ClassFilter.java:20)
        at hudson.remoting.MultiClassLoaderSerializer$Input.resolveClass(MultiClassLoaderSerializer.java:111)
        at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1612)
        at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
        at hudson.remoting.UserRequest.deserialize(UserRequest.java:184)
        at hudson.remoting.UserResponse.retrieve(UserRequest.java:217)
        at hudson.remoting.Channel.call(Channel.java:781)
        at hudson.remoting.RemoteInvocationHandler.invoke(RemoteInvocationHandler.java:250)
        at com.sun.proxy.$Proxy3.main(Unknown Source)
        at hudson.cli.CLI.execute(CLI.java:338)
        at hudson.cli.CLI._main(CLI.java:509)
        at hudson.cli.CLI.main(CLI.java:390)

基于零退出代码,我假设命令已正常执行,但我不明白为什么输出没有被接收。我认为这可能是因为我不明白waitFor应该如何工作,但我已经尝试将proc.waitFor()行放在函数的各个点上而没有区别。

运行类似的脚本以验证"pwd".execute().text.trim().equals("/")似乎始终正确返回。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我找到了一种有效的方法。方法waitFor将可靠地将shell输出传递给我的groovy脚本,而不是一个接一个地调用consumeProcessOutputwaitForProcessOutput(以任何顺序)。

def getBranchSha = { branch ->
  def wd = new File('/my/git/repo');
  def sout = new StringBuffer(), serr = new StringBuffer();
  def gitcmd = 'git show-ref ' + branch + ' --hash';
  def proc = gitcmd.execute(null, wd);
  proc.waitForProcessOutput(sout, serr);

  def sha = sout.toString().trim();
  out.println("exitCode: " + proc.exitValue() + "\terror: " + serr.toString());
  out.println("input: " + branch + "\toutput: \"" + sha + "\"");
  assert sha.equals("10d94325c8fc1050f3e362d2fbb9f9041e6b9360");
}

getBranchSha("origin/master");

这段代码会给我我想要的东西。我不明白为什么waitForconsumeProcessOutput的组合不会做同样的事情。