@@ -87,21 +87,34 @@ public void perform(@NonNull Run<?, ?> run, @NonNull FilePath workspace, @NonNul
87
87
builder = builder .toWindowsCommand ();
88
88
}
89
89
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 );
97
97
}
98
- addBuildInfoActionIfNeeded (new JenkinsBuildInfoLog (listener ), run , taskOutputStream );
99
98
} catch (Exception e ) {
100
99
String errorMessage = "Couldn't execute 'jf' command. " + ExceptionUtils .getRootCauseMessage (e );
101
100
throw new RuntimeException (errorMessage , e );
102
101
}
103
102
}
104
103
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
+
105
118
/**
106
119
* Get JFrog CLI path in agent, according to the JFROG_BINARY_PATH environment variable.
107
120
* 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
162
175
}
163
176
FilePath jfrogHomeTempDir = Utils .createAndGetJfrogCliHomeTempDir (workspace , String .valueOf (run .getNumber ()));
164
177
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 );
166
179
// Configure all servers, skip if all server ids have already been configured.
167
180
if (shouldConfig (jfrogHomeTempDir )) {
168
181
logIfNoToolProvided (env , listener );
@@ -190,7 +203,7 @@ private boolean shouldConfig(FilePath jfrogHomeTempDir) throws IOException, Inte
190
203
/**
191
204
* Locally configure all servers that was configured in the Jenkins UI.
192
205
*/
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 {
194
207
// Config all servers using the 'jf c add' command.
195
208
List <JFrogPlatformInstance > jfrogInstances = JFrogPlatformBuilder .getJFrogPlatformInstances ();
196
209
if (jfrogInstances != null && jfrogInstances .size () > 0 ) {
@@ -202,10 +215,7 @@ private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryP
202
215
builder = builder .toWindowsCommand ();
203
216
}
204
217
// 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 );
209
219
}
210
220
}
211
221
}
@@ -238,20 +248,13 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan
238
248
*
239
249
* @param log - Task logger
240
250
* @param run - The Jenkins project
241
- * @param taskOutputStream - Task's output stream
251
+ * @param originalTaskOutput - Task's output stream
242
252
*/
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 ) {
250
254
// 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 , "{" , "}" );
253
256
if (taskOutput == null ) {
254
- logIllegalBuildPublishOutput (log , taskOutputStream );
257
+ logIllegalBuildPublishOutput (log , originalTaskOutput );
255
258
return ;
256
259
}
257
260
@@ -260,11 +263,11 @@ void addBuildInfoActionIfNeeded(Log log, Run<?, ?> run, ByteArrayOutputStream ta
260
263
try {
261
264
buildInfoOutputModel = mapper .readValue ("{" + taskOutput + "}" , BuildInfoOutputModel .class );
262
265
if (buildInfoOutputModel == null ) {
263
- logIllegalBuildPublishOutput (log , taskOutputStream );
266
+ logIllegalBuildPublishOutput (log , originalTaskOutput );
264
267
return ;
265
268
}
266
269
} catch (JsonProcessingException e ) {
267
- logIllegalBuildPublishOutput (log , taskOutputStream );
270
+ logIllegalBuildPublishOutput (log , originalTaskOutput );
268
271
log .warn (ExceptionUtils .getRootCauseMessage (e ));
269
272
return ;
270
273
}
@@ -276,8 +279,14 @@ void addBuildInfoActionIfNeeded(Log log, Run<?, ?> run, ByteArrayOutputStream ta
276
279
}
277
280
}
278
281
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 );
281
290
}
282
291
283
292
@ Symbol ("jf" )
0 commit comments