Fix error detection in executeGitCommand
1. proc.consumeProcessErrorStream does not work correctly: it creates a thread to asynchronously copy the content of the stream to the string buffer, and does not provide any way to know when it's done. As a result, sometimes the buffer is correctly filled in after consumeProcessErrorStream returns, sometimes it's not. 2. Checking stderr to know if there was an error is not a good idea: a process can run just fine and give output on stderr (logs, basically), and a process can fail without giving any output on stderr. Checking the status code is more reliable.
This commit is contained in:
parent
cfeee5059b
commit
8363df3495
|
@ -1,3 +1,5 @@
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
|
|
||||||
import groovy.json.JsonSlurper
|
import groovy.json.JsonSlurper
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -321,13 +323,28 @@ task ciRelease() {
|
||||||
ciRelease.dependsOn updateChangeLogFile, release
|
ciRelease.dependsOn updateChangeLogFile, release
|
||||||
release.mustRunAfter updateChangeLogFile
|
release.mustRunAfter updateChangeLogFile
|
||||||
|
|
||||||
void executeGitCommand(String command, String errorMessage){
|
static void executeGitCommand(String command, String errorMessage){
|
||||||
def proc = command.execute()
|
def proc = command.execute()
|
||||||
def errorsStringBuffer = new StringBuffer()
|
def code = proc.waitFor()
|
||||||
proc.waitFor()
|
def stdout = inputStreamToString( proc.getInputStream() )
|
||||||
proc.consumeProcessErrorStream( errorsStringBuffer )
|
def stderr = inputStreamToString( proc.getErrorStream() )
|
||||||
if ( errorsStringBuffer.toString( ) != "" ) {
|
if ( code != 0 ) {
|
||||||
throw new GradleException( errorMessage + " " + b );
|
throw new GradleException( errorMessage + "\n\nstdout:\n" + stdout + "\n\nstderr:\n" + stderr )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static String inputStreamToString(InputStream inputStream) {
|
||||||
|
inputStream.withCloseable { ins ->
|
||||||
|
new BufferedInputStream(ins).withCloseable { bis ->
|
||||||
|
new ByteArrayOutputStream().withCloseable { buf ->
|
||||||
|
int result = bis.read();
|
||||||
|
while (result != -1) {
|
||||||
|
buf.write((byte) result);
|
||||||
|
result = bis.read();
|
||||||
|
}
|
||||||
|
return buf.toString( StandardCharsets.UTF_8.name());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue