diff --git a/pkg/config/application.go b/pkg/config/application.go index bc277b0fea..3b31099f54 100644 --- a/pkg/config/application.go +++ b/pkg/config/application.go @@ -225,6 +225,7 @@ type PipelineStage struct { WaitStageOptions *WaitStageOptions WaitApprovalStageOptions *WaitApprovalStageOptions AnalysisStageOptions *AnalysisStageOptions + ScriptRunStageOptions *ScriptRunStageOptions K8sPrimaryRolloutStageOptions *K8sPrimaryRolloutStageOptions K8sCanaryRolloutStageOptions *K8sCanaryRolloutStageOptions @@ -291,6 +292,12 @@ func (s *PipelineStage) UnmarshalJSON(data []byte) error { if len(gs.With) > 0 { err = json.Unmarshal(gs.With, s.AnalysisStageOptions) } + case model.StageScriptRun: + s.ScriptRunStageOptions = &ScriptRunStageOptions{} + if len(gs.With) > 0 { + err = json.Unmarshal(gs.With, s.ScriptRunStageOptions) + } + case model.StageK8sPrimaryRollout: s.K8sPrimaryRolloutStageOptions = &K8sPrimaryRolloutStageOptions{} if len(gs.With) > 0 { @@ -485,6 +492,21 @@ func (a *AnalysisStageOptions) Validate() error { return nil } +// ScriptRunStageOptions contains all configurable values for a SCRIPT_RUN stage. +type ScriptRunStageOptions struct { + Env map[string]string `json:"env"` + Run string `json:"run"` + OnRollback string `json:"onRollback"` +} + +// Validate checks the required fields of ScriptRunStageOptions. +func (s *ScriptRunStageOptions) Validate() error { + if s.Run == "" { + return fmt.Errorf("SCRIPT_RUN stage requires run field") + } + return nil +} + type AnalysisTemplateRef struct { Name string `json:"name"` AppArgs map[string]string `json:"appArgs"` diff --git a/pkg/config/application_test.go b/pkg/config/application_test.go index f92d4d03a3..1d48ccd126 100644 --- a/pkg/config/application_test.go +++ b/pkg/config/application_test.go @@ -702,3 +702,33 @@ func TestCustomSyncConfig(t *testing.T) { }) } } + +// TODO: Add testcases for other kinds of applications. +func TestScriptSycConfiguration(t *testing.T) { + testcases := []struct { + name string + opts ScriptRunStageOptions + wantErr bool + }{ + { + name: "valid", + opts: ScriptRunStageOptions{ + Run: "echo 'hello world'", + }, + wantErr: false, + }, + { + name: "invalid", + opts: ScriptRunStageOptions{ + Run: "", + }, + wantErr: true, + }, + } + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + err := tc.opts.Validate() + assert.Equal(t, tc.wantErr, err != nil) + }) + } +} diff --git a/pkg/model/stage.go b/pkg/model/stage.go index edffef63be..6b079bb947 100644 --- a/pkg/model/stage.go +++ b/pkg/model/stage.go @@ -27,6 +27,9 @@ const ( // StageAnalysis represents the waiting state for analysing // the application status based on metrics, log, http request... StageAnalysis Stage = "ANALYSIS" + // StageScriptRun represents a state where + // the specified script will be executed. + StageScriptRun Stage = "SCRIPT_RUN" // StageK8sSync represents the state where // all resources should be synced with the Git state.