Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

groovy execute intermittently returns no output nor error

Tags:

jenkins

groovy

I'm trying to use a Groovy script (to run with jenkins-cli.jar) to verify some git information.

To this end, I have the following simple groovy script:

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");

I expect this to construct a command line git show-ref origin/master --hash and execute that in the working dir of my git repo. I expect the output of this to be the current SHA of my origin/master branch in this repo 10d94325c8fc1050f3e362d2fbb9f9041e6b9360, and the code asserts this to be the case.

Most of the time, this code runs exactly as expected, but sometimes for no reason I can see, the output is blank:

 > 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)

Based on the zero exit code, I assume that the command has executed normally, but I don't understand why the output hasn't been received. I thought that it might be because I don't understand how waitFor is supposed to work, but I've tried placing the proc.waitFor() line at various points in the function with no difference.

Running a similar script to verify that "pwd".execute().text.trim().equals("/") seems to always return correctly.

Any ideas?

like image 659
laffoyb Avatar asked Sep 25 '22 11:09

laffoyb


1 Answers

I found a way that works. Rather than calling waitFor and consumeProcessOutput one after the other (in any order), the method waitForProcessOutput will reliably deliver the shell output to my groovy script.

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");

This code will give me what I want. I don't understand why a combination of waitFor and consumeProcessOutput doesn't do the same thing though.

like image 107
laffoyb Avatar answered Oct 12 '22 11:10

laffoyb