Skip to content

Commit

Permalink
[ci] Attach correct build artifact link to build scan when multiple a…
Browse files Browse the repository at this point in the history
…re uploaded (elastic#105530)

(cherry picked from commit e568f70)
  • Loading branch information
brianseeders committed Feb 23, 2024
1 parent fc3c2f8 commit 3f3841a
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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: "<UUID><space><ISO-8601-timestamp>\n"
// and multiple artifacts could be returned
Process process = new ProcessBuilder(
"buildkite-agent",
"artifact",
Expand All @@ -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;
Expand All @@ -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<String> 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) {
Expand Down

0 comments on commit 3f3841a

Please sign in to comment.