Skip to content

Commit e61240c

Browse files
authored
Merge pull request #131 from devtron-labs/pre-cd-fix
fix: Auto trigger even if pre-cd fails
2 parents 69cdda9 + 9ced301 commit e61240c

File tree

4 files changed

+61
-47
lines changed

4 files changed

+61
-47
lines changed

ci-runner/executor/stage/cdStages.go

+53-37
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,21 @@ func NewCdStage(gitManager helper.GitManager, dockerHelper helper.DockerHelper,
4646
}
4747
}
4848

49-
func deferCDEvent(cdRequest *helper.CommonWorkflowRequest, artifactUploaded bool, err error) (exitCode int) {
50-
log.Println(util.DEVTRON, "defer CD stage data.", "err: ", err, "artifactUploaded: ", artifactUploaded)
49+
func (impl *CdStage) HandleCDEvent(ciCdRequest *helper.CiCdTriggerEvent, exitCode *int) {
50+
resp, err := impl.handleCDEvent(ciCdRequest)
5151
if err != nil {
52-
exitCode = workFlow.DefaultErrorCode
53-
var stageError *helper.CdStageError
54-
if errors.As(err, &stageError) {
55-
// update artifact uploaded status
56-
if !stageError.IsArtifactUploaded() {
57-
stageError = stageError.WithArtifactUploaded(artifactUploaded)
58-
}
59-
} else {
60-
stageError = helper.NewCdStageError(fmt.Errorf(workFlow.CdStageFailed.String(), cdRequest.GetCdStageType(), err)).
61-
WithArtifactUploaded(artifactUploaded)
62-
}
63-
// send ci failure event, for ci failure notification
64-
sendCDFailureEvent(cdRequest, stageError)
65-
// populate stage error
66-
util.PopulateStageError(stageError.ErrorMessage())
52+
//log error and send completion event
53+
log.Println("cd stage error: ", err)
6754
}
68-
return exitCode
55+
*exitCode = impl.sendCDCompletionEvent(ciCdRequest, resp, err)
56+
return
6957
}
7058

71-
func (impl *CdStage) HandleCDEvent(ciCdRequest *helper.CiCdTriggerEvent, exitCode *int) {
59+
func (impl *CdStage) handleCDEvent(ciCdRequest *helper.CiCdTriggerEvent) (*helper.HandleCdEventResponse, error) {
7260
var artifactUploaded bool
7361
var err error
7462
var allPluginArtifacts *helper.PluginArtifacts
75-
defer func() {
76-
*exitCode = deferCDEvent(ciCdRequest.CommonWorkflowRequest, artifactUploaded, err)
77-
}()
63+
7864
allPluginArtifacts, err = impl.runCDStages(ciCdRequest)
7965
if err != nil {
8066
log.Println("cd stage error: ", err)
@@ -89,21 +75,11 @@ func (impl *CdStage) HandleCDEvent(ciCdRequest *helper.CiCdTriggerEvent, exitCod
8975
err = artifactUploadErr
9076
}
9177
}
92-
// IsVirtualExecution run flag indicates that cd stage is running in virtual mode.
93-
// specifically for isolated environment type, for IsVirtualExecution we don't send success event.
94-
// but failure event is sent in case of error.
95-
if err == nil && !ciCdRequest.CommonWorkflowRequest.IsVirtualExecution {
96-
log.Println(util.DEVTRON, " event")
97-
event := adaptor.NewCdCompleteEvent(ciCdRequest.CommonWorkflowRequest).
98-
WithPluginArtifacts(allPluginArtifacts).
99-
WithIsArtifactUploaded(artifactUploaded)
100-
err = helper.SendCDEvent(ciCdRequest.CommonWorkflowRequest, event)
101-
if err != nil {
102-
log.Println(err)
103-
}
104-
log.Println(util.DEVTRON, " /event")
105-
}
106-
return
78+
79+
return &helper.HandleCdEventResponse{
80+
PluginArtifacts: allPluginArtifacts,
81+
IsArtifactUploaded: artifactUploaded,
82+
}, err
10783
}
10884

10985
func collectAndUploadCDArtifacts(cdRequest *helper.CommonWorkflowRequest) (artifactUploaded bool, err error) {
@@ -230,3 +206,43 @@ func (impl *CdStage) runCDStages(ciCdRequest *helper.CiCdTriggerEvent) (*helper.
230206
}
231207
return allPluginArtifacts, nil
232208
}
209+
210+
func (impl *CdStage) sendCDCompletionEvent(ciCdRequest *helper.CiCdTriggerEvent, handleCdEventResp *helper.HandleCdEventResponse, err error) (exitCode int) {
211+
log.Println(util.DEVTRON, "CD stage completion data.", "artifactUploaded: ", handleCdEventResp.IsArtifactUploaded, "err ", err)
212+
if err != nil {
213+
exitCode = workFlow.DefaultErrorCode
214+
var stageError *helper.CdStageError
215+
if errors.As(err, &stageError) {
216+
// update artifact uploaded status
217+
if !stageError.IsArtifactUploaded() {
218+
stageError = stageError.WithArtifactUploaded(handleCdEventResp.IsArtifactUploaded)
219+
}
220+
} else {
221+
stageError = helper.NewCdStageError(fmt.Errorf(workFlow.CdStageFailed.String(), ciCdRequest.CommonWorkflowRequest.GetCdStageType(), err)).
222+
WithArtifactUploaded(handleCdEventResp.IsArtifactUploaded)
223+
}
224+
// send cd failure event, for ci failure notification
225+
event := adaptor.NewCdCompleteEvent(ciCdRequest.CommonWorkflowRequest, true).
226+
WithIsArtifactUploaded(handleCdEventResp.IsArtifactUploaded)
227+
e := helper.SendCDEvent(ciCdRequest.CommonWorkflowRequest, event)
228+
if e != nil {
229+
log.Println(e)
230+
}
231+
// populate stage error
232+
util.PopulateStageError(stageError.ErrorMessage())
233+
} else if err == nil && !ciCdRequest.CommonWorkflowRequest.IsVirtualExecution {
234+
// IsVirtualExecution run flag indicates that cd stage is running in virtual mode.
235+
// specifically for isolated environment type, for IsVirtualExecution we don't send success event.
236+
// but failure event is sent in case of error.
237+
// send cd success event
238+
event := adaptor.NewCdCompleteEvent(ciCdRequest.CommonWorkflowRequest, false).
239+
WithPluginArtifacts(handleCdEventResp.PluginArtifacts).
240+
WithIsArtifactUploaded(handleCdEventResp.IsArtifactUploaded)
241+
err := helper.SendCDEvent(ciCdRequest.CommonWorkflowRequest, event)
242+
if err != nil {
243+
log.Println(err)
244+
}
245+
}
246+
log.Println(util.DEVTRON, "cd stage completion event sent")
247+
return exitCode
248+
}

ci-runner/executor/stage/ciStages.go

-9
Original file line numberDiff line numberDiff line change
@@ -554,15 +554,6 @@ func sendCIFailureEvent(ciRequest *helper.CommonWorkflowRequest, err *helper.CiS
554554
}
555555
}
556556

557-
func sendCDFailureEvent(ciRequest *helper.CommonWorkflowRequest, err *helper.CdStageError) {
558-
event := adaptor.NewCdCompleteEvent(ciRequest).
559-
WithIsArtifactUploaded(err.IsArtifactUploaded())
560-
e := helper.SendCDEvent(ciRequest, event)
561-
if e != nil {
562-
log.Println(e)
563-
}
564-
}
565-
566557
func (impl *CiStage) pushArtifact(ciCdRequest *helper.CiCdTriggerEvent, dest string, digest string, metrics *helper.CIMetrics, artifactUploaded bool) error {
567558
imageRetryCountValue := ciCdRequest.CommonWorkflowRequest.ImageRetryCount
568559
imageRetryIntervalValue := ciCdRequest.CommonWorkflowRequest.ImageRetryInterval

ci-runner/helper/EventHelper.go

+6
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ type CiCdTriggerEvent struct {
340340
CommonWorkflowRequest *CommonWorkflowRequest `json:"commonWorkflowRequest"`
341341
}
342342

343+
type HandleCdEventResponse struct {
344+
PluginArtifacts *PluginArtifacts
345+
IsArtifactUploaded bool
346+
}
347+
343348
type ExtEnvRequest struct {
344349
OrchestratorHost string `json:"orchestratorHost"`
345350
OrchestratorToken string `json:"orchestratorToken"`
@@ -458,6 +463,7 @@ type CdStageCompleteEvent struct {
458463
PluginArtifactStage string `json:"pluginArtifactStage"`
459464
PluginArtifacts *PluginArtifacts `json:"pluginArtifacts"`
460465
IsArtifactUploaded bool `json:"isArtifactUploaded"`
466+
IsFailed bool `json:"isFailed"` //new flag as isFailed for backward compatibility
461467
}
462468

463469
func (event *CdStageCompleteEvent) WithPluginArtifacts(pluginArtifacts *PluginArtifacts) *CdStageCompleteEvent {

ci-runner/helper/adaptor/CiStageAdaptor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func NewCiCompleteEvent(ciRequest *helper.CommonWorkflowRequest) *helper.CiCompl
2323
return event
2424
}
2525

26-
func NewCdCompleteEvent(cdRequest *helper.CommonWorkflowRequest) *helper.CdStageCompleteEvent {
26+
func NewCdCompleteEvent(cdRequest *helper.CommonWorkflowRequest, isFailed bool) *helper.CdStageCompleteEvent {
2727
event := &helper.CdStageCompleteEvent{
2828
CiProjectDetails: cdRequest.CiProjectDetails,
2929
CdPipelineId: cdRequest.CdPipelineId,
@@ -33,6 +33,7 @@ func NewCdCompleteEvent(cdRequest *helper.CommonWorkflowRequest) *helper.CdStage
3333
TriggeredBy: cdRequest.TriggeredBy,
3434
PluginRegistryArtifactDetails: cdRequest.RegistryDestinationImageMap,
3535
PluginArtifactStage: cdRequest.PluginArtifactStage,
36+
IsFailed: isFailed,
3637
}
3738
return event
3839
}

0 commit comments

Comments
 (0)