Skip to content

Commit 89116d2

Browse files
committed
Reduce the amount of duplicate logging when using kubernetes agents
Only the logging of the build publish command will get duplicated on kubernetes agents
1 parent 5f26fa7 commit 89116d2

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

src/main/java/io/jenkins/plugins/jfrog/JfStep.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,34 @@ public void perform(@NonNull Run<?, ?> run, @NonNull FilePath workspace, @NonNul
8787
builder = builder.toWindowsCommand();
8888
}
8989

90-
try (ByteArrayOutputStream taskOutputStream = new ByteArrayOutputStream()) {
91-
JfTaskListener jfTaskListener = new JfTaskListener(listener, taskOutputStream);
92-
Launcher.ProcStarter jfLauncher = setupJFrogEnvironment(run, env, launcher, jfTaskListener, workspace, jfrogBinaryPath, isWindows);
93-
// Running the 'jf' command
94-
int exitValue = jfLauncher.cmds(builder).join();
95-
if (exitValue != 0) {
96-
throw new RuntimeException("Running 'jf' command failed with exit code " + exitValue);
90+
try {
91+
Launcher.ProcStarter procStarter = setupJFrogEnvironment(run, env, launcher, listener, workspace, jfrogBinaryPath, isWindows);
92+
if (isBuildPublish()) {
93+
String stdOut = executeJfCommandReturningStdOut(procStarter, builder, listener);
94+
addBuildInfoActionIfNeeded(new JenkinsBuildInfoLog(listener), run, stdOut);
95+
} else {
96+
executeJfCommand(procStarter, builder);
9797
}
98-
addBuildInfoActionIfNeeded(new JenkinsBuildInfoLog(listener), run, taskOutputStream);
9998
} catch (Exception e) {
10099
String errorMessage = "Couldn't execute 'jf' command. " + ExceptionUtils.getRootCauseMessage(e);
101100
throw new RuntimeException(errorMessage, e);
102101
}
103102
}
104103

104+
private static String executeJfCommandReturningStdOut(Launcher.ProcStarter procStarter, ArgumentListBuilder builder, @NonNull TaskListener listener) throws IOException, InterruptedException {
105+
ByteArrayOutputStream taskOutputStream = new ByteArrayOutputStream();
106+
JfTaskListener jfTaskListener = new JfTaskListener(listener, taskOutputStream);
107+
executeJfCommand(procStarter.stdout(jfTaskListener), builder);
108+
return taskOutputStream.toString(StandardCharsets.UTF_8);
109+
}
110+
111+
private static void executeJfCommand(Launcher.ProcStarter procStarter, ArgumentListBuilder builder) throws IOException, InterruptedException {
112+
int exitValue = procStarter.cmds(builder).join();
113+
if (exitValue != 0) {
114+
throw new RuntimeException("Running 'jf' command failed with exit code " + exitValue);
115+
}
116+
}
117+
105118
/**
106119
* Get JFrog CLI path in agent, according to the JFROG_BINARY_PATH environment variable.
107120
* The JFROG_BINARY_PATH also can be set implicitly in Declarative Pipeline by choosing the JFrog CLI tool or
@@ -162,7 +175,7 @@ public Launcher.ProcStarter setupJFrogEnvironment(Run<?, ?> run, EnvVars env, La
162175
}
163176
FilePath jfrogHomeTempDir = Utils.createAndGetJfrogCliHomeTempDir(workspace, String.valueOf(run.getNumber()));
164177
CliEnvConfigurator.configureCliEnv(env, jfrogHomeTempDir.getRemote(), jfrogCliConfigEncryption);
165-
Launcher.ProcStarter jfLauncher = launcher.launch().envs(env).pwd(workspace).stdout(listener);
178+
Launcher.ProcStarter jfLauncher = launcher.launch().envs(env).stdout(listener).pwd(workspace);
166179
// Configure all servers, skip if all server ids have already been configured.
167180
if (shouldConfig(jfrogHomeTempDir)) {
168181
logIfNoToolProvided(env, listener);
@@ -190,7 +203,7 @@ private boolean shouldConfig(FilePath jfrogHomeTempDir) throws IOException, Inte
190203
/**
191204
* Locally configure all servers that was configured in the Jenkins UI.
192205
*/
193-
private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryPath, boolean isWindows, Job<?, ?> job) throws IOException, InterruptedException {
206+
private void configAllServers(Launcher.ProcStarter procStarter, String jfrogBinaryPath, boolean isWindows, Job<?, ?> job) throws IOException, InterruptedException {
194207
// Config all servers using the 'jf c add' command.
195208
List<JFrogPlatformInstance> jfrogInstances = JFrogPlatformBuilder.getJFrogPlatformInstances();
196209
if (jfrogInstances != null && jfrogInstances.size() > 0) {
@@ -202,10 +215,7 @@ private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryP
202215
builder = builder.toWindowsCommand();
203216
}
204217
// Running 'jf' command
205-
int exitValue = launcher.cmds(builder).join();
206-
if (exitValue != 0) {
207-
throw new RuntimeException("Running 'jf' command failed with exit code " + exitValue);
208-
}
218+
executeJfCommand(procStarter, builder);
209219
}
210220
}
211221
}
@@ -238,20 +248,13 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan
238248
*
239249
* @param log - Task logger
240250
* @param run - The Jenkins project
241-
* @param taskOutputStream - Task's output stream
251+
* @param originalTaskOutput - Task's output stream
242252
*/
243-
void addBuildInfoActionIfNeeded(Log log, Run<?, ?> run, ByteArrayOutputStream taskOutputStream) {
244-
if (args.length < 2 ||
245-
!args[0].equals("rt") ||
246-
!equalsAny(args[1], "bp", "build-publish")) {
247-
return;
248-
}
249-
253+
void addBuildInfoActionIfNeeded(Log log, Run<?, ?> run, String originalTaskOutput) {
250254
// Search for '{' and '}' in the output of 'jf rt build-publish'
251-
String taskOutput = taskOutputStream.toString(StandardCharsets.UTF_8);
252-
taskOutput = substringBetween(taskOutput, "{", "}");
255+
String taskOutput = substringBetween(originalTaskOutput, "{", "}");
253256
if (taskOutput == null) {
254-
logIllegalBuildPublishOutput(log, taskOutputStream);
257+
logIllegalBuildPublishOutput(log, originalTaskOutput);
255258
return;
256259
}
257260

@@ -260,11 +263,11 @@ void addBuildInfoActionIfNeeded(Log log, Run<?, ?> run, ByteArrayOutputStream ta
260263
try {
261264
buildInfoOutputModel = mapper.readValue("{" + taskOutput + "}", BuildInfoOutputModel.class);
262265
if (buildInfoOutputModel == null) {
263-
logIllegalBuildPublishOutput(log, taskOutputStream);
266+
logIllegalBuildPublishOutput(log, originalTaskOutput);
264267
return;
265268
}
266269
} catch (JsonProcessingException e) {
267-
logIllegalBuildPublishOutput(log, taskOutputStream);
270+
logIllegalBuildPublishOutput(log, originalTaskOutput);
268271
log.warn(ExceptionUtils.getRootCauseMessage(e));
269272
return;
270273
}
@@ -276,8 +279,14 @@ void addBuildInfoActionIfNeeded(Log log, Run<?, ?> run, ByteArrayOutputStream ta
276279
}
277280
}
278281

279-
private void logIllegalBuildPublishOutput(Log log, ByteArrayOutputStream taskOutputStream) {
280-
log.warn("Illegal build-publish output: " + taskOutputStream.toString(StandardCharsets.UTF_8));
282+
boolean isBuildPublish() {
283+
return args.length >= 2 &&
284+
args[0].equals("rt") &&
285+
equalsAny(args[1], "bp", "build-publish");
286+
}
287+
288+
private void logIllegalBuildPublishOutput(Log log, String taskOutput) {
289+
log.warn("Illegal build-publish output: " + taskOutput);
281290
}
282291

283292
@Symbol("jf")

src/test/java/io/jenkins/plugins/jfrog/AddBuildInfoActionTest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
import org.mockito.junit.MockitoRule;
1717
import org.mockito.junit.jupiter.MockitoExtension;
1818

19-
import java.io.ByteArrayOutputStream;
20-
import java.io.IOException;
21-
import java.nio.charset.StandardCharsets;
2219
import java.util.stream.Stream;
2320

2421
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -55,7 +52,7 @@ private static Stream<Arguments> positiveDataProvider() {
5552

5653
@ParameterizedTest
5754
@MethodSource("positiveDataProvider")
58-
public void addBuildInfoActionPositiveTest(String command, String output) throws IOException {
55+
public void addBuildInfoActionPositiveTest(String command, String output) {
5956
doNothing().when(run).addAction(valueCapture.capture());
6057
runCliCommand(command, output);
6158

@@ -75,15 +72,15 @@ private static Stream<Arguments> negativeDataProvider() {
7572

7673
@ParameterizedTest
7774
@MethodSource("negativeDataProvider")
78-
public void addBuildInfoActionNegativeTest(String command, String output) throws IOException {
75+
public void addBuildInfoActionNegativeTest(String command, String output) {
7976
runCliCommand(command, output);
8077
Mockito.verify(run, never()).addAction(isA(Action.class));
8178
}
8279

83-
private void runCliCommand(String command, String output) throws IOException {
84-
try (ByteArrayOutputStream taskOutputStream = new ByteArrayOutputStream()) {
85-
taskOutputStream.writeBytes(output.getBytes(StandardCharsets.UTF_8));
86-
new JfStep(command).addBuildInfoActionIfNeeded(new NullLog(), run, taskOutputStream);
80+
private void runCliCommand(String command, String output) {
81+
JfStep jfStep = new JfStep(command);
82+
if (jfStep.isBuildPublish()) {
83+
jfStep.addBuildInfoActionIfNeeded(new NullLog(), run, output);
8784
}
8885
}
8986
}

0 commit comments

Comments
 (0)