@@ -46,35 +46,21 @@ func NewCdStage(gitManager helper.GitManager, dockerHelper helper.DockerHelper,
46
46
}
47
47
}
48
48
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 )
51
51
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 )
67
54
}
68
- return exitCode
55
+ * exitCode = impl .sendCDCompletionEvent (ciCdRequest , resp , err )
56
+ return
69
57
}
70
58
71
- func (impl * CdStage ) HandleCDEvent (ciCdRequest * helper.CiCdTriggerEvent , exitCode * int ) {
59
+ func (impl * CdStage ) handleCDEvent (ciCdRequest * helper.CiCdTriggerEvent ) ( * helper. HandleCdEventResponse , error ) {
72
60
var artifactUploaded bool
73
61
var err error
74
62
var allPluginArtifacts * helper.PluginArtifacts
75
- defer func () {
76
- * exitCode = deferCDEvent (ciCdRequest .CommonWorkflowRequest , artifactUploaded , err )
77
- }()
63
+
78
64
allPluginArtifacts , err = impl .runCDStages (ciCdRequest )
79
65
if err != nil {
80
66
log .Println ("cd stage error: " , err )
@@ -89,21 +75,11 @@ func (impl *CdStage) HandleCDEvent(ciCdRequest *helper.CiCdTriggerEvent, exitCod
89
75
err = artifactUploadErr
90
76
}
91
77
}
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
107
83
}
108
84
109
85
func collectAndUploadCDArtifacts (cdRequest * helper.CommonWorkflowRequest ) (artifactUploaded bool , err error ) {
@@ -230,3 +206,43 @@ func (impl *CdStage) runCDStages(ciCdRequest *helper.CiCdTriggerEvent) (*helper.
230
206
}
231
207
return allPluginArtifacts , nil
232
208
}
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
+ }
0 commit comments