From e30943e57bfa89c0567cf0847cd1588a0d35c080 Mon Sep 17 00:00:00 2001 From: Avinal Kumar Date: Thu, 2 Feb 2023 21:19:25 +0530 Subject: [PATCH] reflect state of started pipeline/task when using --showlog - this change fixes an issue where using --showlog with tkn start pipeline/task does not return a non-zero value when created taskrun/pipelinerun fails. - refer #1820 Signed-off-by: Avinal Kumar --- pkg/cmd/pipeline/start.go | 19 ++++++++++++++++++- pkg/cmd/task/start.go | 18 +++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/pipeline/start.go b/pkg/cmd/pipeline/start.go index 6c88d6a1ac..ef30e01fbc 100644 --- a/pkg/cmd/pipeline/start.go +++ b/pkg/cmd/pipeline/start.go @@ -24,6 +24,8 @@ import ( "strings" "time" + "knative.dev/pkg/apis" + "github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2/terminal" "github.com/spf13/cobra" @@ -414,7 +416,22 @@ func (opt *startOptions) startPipeline(pipelineStart *v1beta1.Pipeline) error { Params: opt.cliparams, AllSteps: false, } - return prcmd.Run(runLogOpts) + if err = prcmd.Run(runLogOpts); err != nil { + return err + } + + // Fetch the PipelineRun to get the PipelineRun status + prLatest, err := pipelinerun.GetPipelineRun(pipelineRunGroupResource, cs, prCreated.Name, prCreated.Namespace) + if err != nil { + return err + } + + // If PipelineRun status is not Succeeded, print the PipelineRun status + if prLatest.Status.GetCondition(apis.ConditionSucceeded).IsFalse() { + return fmt.Errorf("pipelinerun %s did not succeed", prLatest.Name) + } + + return nil } func (opt *startOptions) getInput(pipeline *v1beta1.Pipeline) error { diff --git a/pkg/cmd/task/start.go b/pkg/cmd/task/start.go index 6c96f93b4e..b746703322 100644 --- a/pkg/cmd/task/start.go +++ b/pkg/cmd/task/start.go @@ -425,7 +425,23 @@ func startTask(opt startOptions, args []string) error { Params: opt.cliparams, AllSteps: false, } - return taskrun.Run(runLogOpts) + + if err := taskrun.Run(runLogOpts); err != nil { + return err + } + + // fetch the taskrun again to get the latest status + trLatest, err := traction.GetTaskRun(taskrunGroupResource, cs, trCreated.Name, trCreated.Namespace) + if err != nil { + return err + } + + // check if the created taskrun succeed or not + if !trLatest.IsSuccessful() { + return fmt.Errorf("taskRun %s did not succeed", trCreated.Name) + } + + return nil } func printTaskRun(output string, s *cli.Stream, tr interface{}) error {