diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchBuildCompletePlugin.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchBuildCompletePlugin.java index f0604ab33ceec..e0588ed440c57 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchBuildCompletePlugin.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchBuildCompletePlugin.java @@ -32,7 +32,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Optional; import javax.inject.Inject; @@ -142,6 +144,8 @@ public void execute(BuildFinishedFlowAction.Parameters parameters) throws FileNo System.out.println("Generating buildscan link for artifact..."); + // Output should be in the format: "\n" + // and multiple artifacts could be returned Process process = new ProcessBuilder( "buildkite-agent", "artifact", @@ -150,7 +154,7 @@ public void execute(BuildFinishedFlowAction.Parameters parameters) throws FileNo "--step", System.getenv("BUILDKITE_JOB_ID"), "--format", - "%i" + "%i %c" ).start(); process.waitFor(); String processOutput; @@ -159,7 +163,17 @@ public void execute(BuildFinishedFlowAction.Parameters parameters) throws FileNo } catch (IOException e) { processOutput = ""; } - String artifactUuid = processOutput.trim(); + + // Sort them by timestamp, and grab the most recent one + Optional artifact = Arrays.stream(processOutput.trim().split("\n")).map(String::trim).min((a, b) -> { + String[] partsA = a.split(" "); + String[] partsB = b.split(" "); + // ISO-8601 timestamps can be sorted lexicographically + return partsB[1].compareTo(partsA[1]); + }); + + // Grab just the UUID from the artifact + String artifactUuid = artifact.orElse("").split(" ")[0]; System.out.println("Artifact UUID: " + artifactUuid); if (artifactUuid.isEmpty() == false) {