33import com .google .common .base .Strings ;
44import com .google .common .io .Files ;
55import com .google .common .io .Resources ;
6+ import edu .umd .cs .findbugs .annotations .NonNull ;
67import hudson .EnvVars ;
78import hudson .Extension ;
89import hudson .FilePath ;
910import hudson .Launcher ;
1011import hudson .model .AbstractProject ;
12+ import hudson .model .Result ;
1113import hudson .model .Run ;
1214import hudson .model .TaskListener ;
1315import hudson .tasks .BuildStepDescriptor ;
3133import org .waveywaves .jenkins .plugins .tekton .client .logwatch .PipelineRunLogWatch ;
3234import org .waveywaves .jenkins .plugins .tekton .client .logwatch .TaskRunLogWatch ;
3335
34- import javax .annotation .Nonnull ;
3536import java .io .ByteArrayInputStream ;
3637import java .io .File ;
3738import java .io .IOException ;
3839import java .io .InputStream ;
3940import java .io .PrintStream ;
41+ import java .io .PrintWriter ;
42+ import java .io .StringWriter ;
4043import java .net .URL ;
4144import java .nio .charset .StandardCharsets ;
4245import java .util .List ;
@@ -111,7 +114,7 @@ public String getClusterName() {
111114 return clusterName ;
112115 }
113116
114- protected String createWithResourceSpecificClient (TektonResourceType resourceType , InputStream inputStream ) {
117+ protected String createWithResourceSpecificClient (TektonResourceType resourceType , InputStream inputStream ) throws Exception {
115118 switch (resourceType ) {
116119 case task :
117120 return createTask (inputStream );
@@ -126,7 +129,7 @@ protected String createWithResourceSpecificClient(TektonResourceType resourceTyp
126129 }
127130 }
128131
129- public String createTaskRun (InputStream inputStream ) {
132+ public String createTaskRun (InputStream inputStream ) throws Exception {
130133 if (taskRunClient == null ) {
131134 TektonClient tc = (TektonClient ) tektonClient ;
132135 setTaskRunClient (tc .v1beta1 ().taskRuns ());
@@ -188,7 +191,7 @@ public String createPipeline(InputStream inputStream) {
188191 return resourceName ;
189192 }
190193
191- public String createPipelineRun (InputStream inputStream ) {
194+ public String createPipelineRun (InputStream inputStream ) throws Exception {
192195 if (pipelineRunClient == null ) {
193196 TektonClient tc = (TektonClient ) tektonClient ;
194197 setPipelineRunClient (tc .v1beta1 ().pipelineRuns ());
@@ -210,38 +213,38 @@ public String createPipelineRun(InputStream inputStream) {
210213 return resourceName ;
211214 }
212215
213- public void streamTaskRunLogsToConsole (TaskRun taskRun ) {
216+ public void streamTaskRunLogsToConsole (TaskRun taskRun ) throws Exception {
214217 synchronized (consoleLogger ) {
215218 KubernetesClient kc = (KubernetesClient ) kubernetesClient ;
216219 TektonClient tc = (TektonClient ) tektonClient ;
217220 Thread logWatchTask = null ;
218- try {
219- TaskRunLogWatch logWatch = new TaskRunLogWatch ( kc , tc , taskRun , consoleLogger );
220- logWatchTask = new Thread ( logWatch );
221- logWatchTask .start ();
222- logWatchTask . join ();
223- } catch ( Exception e ) {
224- logger . warning ( "Exception occurred " + e . toString ()) ;
221+ TaskRunLogWatch logWatch = new TaskRunLogWatch ( kc , tc , taskRun , consoleLogger );
222+ logWatchTask = new Thread ( logWatch );
223+ logWatchTask . start ( );
224+ logWatchTask .join ();
225+ Exception e = logWatch . getException ();
226+ if ( e != null ) {
227+ throw e ;
225228 }
226229 }
227230 }
228231
229- public void streamPipelineRunLogsToConsole (PipelineRun pipelineRun ) {
232+ public void streamPipelineRunLogsToConsole (PipelineRun pipelineRun ) throws Exception {
230233 KubernetesClient kc = (KubernetesClient ) kubernetesClient ;
231234 TektonClient tc = (TektonClient ) tektonClient ;
232235 Thread logWatchTask ;
233- try {
234- PipelineRunLogWatch logWatch = new PipelineRunLogWatch ( kc , tc , pipelineRun , consoleLogger );
235- logWatchTask = new Thread ( logWatch );
236- logWatchTask .start ();
237- logWatchTask . join ();
238- } catch ( Exception e ) {
239- logger . warning ( "Exception occurred " + e . toString ()) ;
236+ PipelineRunLogWatch logWatch = new PipelineRunLogWatch ( kc , tc , pipelineRun , consoleLogger );
237+ logWatchTask = new Thread ( logWatch );
238+ logWatchTask . start ( );
239+ logWatchTask .join ();
240+ Exception e = logWatch . getException ();
241+ if ( e != null ) {
242+ throw e ;
240243 }
241244 }
242245
243246 @ Override
244- public void perform (@ Nonnull Run <?, ?> run , @ Nonnull FilePath workspace , @ Nonnull Launcher launcher , @ Nonnull TaskListener listener ) throws InterruptedException , IOException {
247+ public void perform (@ NonNull Run <?, ?> run , @ NonNull FilePath workspace , @ NonNull EnvVars envVars , @ NonNull Launcher launcher , @ NonNull TaskListener listener ) throws InterruptedException , IOException {
245248 consoleLogger = listener .getLogger ();
246249
247250 String clusterName = getClusterName ();
@@ -260,11 +263,10 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnul
260263 throw new IOException ("no kubernetesClient for cluster " + clusterName );
261264 }
262265 }
263- EnvVars envVars = run .getEnvironment (listener );
264- runCreate (workspace , envVars );
266+ runCreate (run , workspace , envVars );
265267 }
266268
267- protected String runCreate (FilePath workspace , EnvVars envVars ) {
269+ protected String runCreate (Run <?, ?> run , FilePath workspace , EnvVars envVars ) {
268270 URL url = null ;
269271 byte [] data = null ;
270272 File inputFile = null ;
@@ -291,13 +293,32 @@ protected String runCreate(FilePath workspace, EnvVars envVars) {
291293 createdResourceName = createWithResourceSpecificClient (resourceType , new ByteArrayInputStream (data ));
292294 }
293295 }
294- } catch (Exception e ) {
295- logger .warning ("possible URL related Exception has occurred " + e .toString ());
296+ } catch (Throwable e ) {
297+ logMessage ("Failed: " + e .getMessage ());
298+ StringWriter buffer = new StringWriter ();
299+ PrintWriter writer = new PrintWriter (buffer );
300+ e .printStackTrace (writer );
301+ writer .close ();
302+ logMessage (buffer .toString ());
303+
304+ logger .warning ("Caught: " + e .toString ());
296305 e .printStackTrace ();
306+
307+ run .setResult (Result .FAILURE );
297308 }
298309 return createdResourceName ;
299310 }
300311
312+ protected void logMessage (String text ) {
313+ synchronized (this .consoleLogger ) {
314+ try {
315+ this .consoleLogger .write ((text + "\n " ).getBytes (StandardCharsets .UTF_8 ));
316+ } catch (IOException e ) {
317+ logger .warning ("failed to log to console: " + e );
318+ }
319+ }
320+ }
321+
301322 /**
302323 * Performs any conversion on the Tekton resources before we apply it to Kubernetes
303324 */
0 commit comments