Skip to content

Commit cd6e3d7

Browse files
authored
fix: Compose 2.21.0 returns newline-separated JSONs instead of single JSON array (#422)
1 parent fdc7e46 commit cd6e3d7

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,18 @@ abstract class ComposeUp extends DefaultTask {
214214
String processesAsString = composeExecutor.get().execute('ps', '--format', 'json')
215215
String processesState = processesAsString
216216
try {
217-
// Status field contains something like "Up 8 seconds", so we have to strip the duration.
218-
Object[] processes = new JsonSlurper().parseText(processesAsString)
217+
// Since Docker Compose 2.21.0, the output is not one JSON array but newline-separated JSONs.
218+
Map<String, Object>[] processes
219+
if (processesAsString.startsWith('[')) {
220+
processes = new JsonSlurper().parseText(processesAsString)
221+
} else {
222+
processes = processesAsString.split('\\R').collect { new JsonSlurper().parseText(it) }
223+
}
219224
List<Object> transformed = processes.collect {
220-
if (it.Status.startsWith('Up ')) it.Status = 'Up'
225+
// Status field contains something like "Up 8 seconds", so we have to strip the duration.
226+
if (it.containsKey('Status') && it.Status.startsWith('Up ')) it.Status = 'Up'
227+
it.remove('RunningFor') // It also contains a duration information.
228+
it.remove('Labels') // The order of labels is not stable.
221229
it
222230
}
223231
processesState = transformed.join('\t')

0 commit comments

Comments
 (0)