Skip to content

Commit 7a86ecb

Browse files
authored
Merge pull request #100 from devtron-labs/fix-varibale-override
misc: variable override in pre/ post stage
2 parents 880f9af + 0c0e99a commit 7a86ecb

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

ci-runner/executor/StageExecutor.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type StageExecutorImpl struct {
3737
}
3838

3939
type StageExecutor interface {
40-
RunCiCdSteps(stepType helper.StepType, ciCdRequest *helper.CommonWorkflowRequest, steps []*helper.StepObject, refStageMap map[int][]*helper.StepObject, scriptEnvVariables *util2.ScriptEnvVariables, preCiStageVariable map[int]map[string]*commonBean.VariableObject) (pluginArtifacts *helper.PluginArtifacts, outVars map[int]map[string]*commonBean.VariableObject, failedStep *helper.StepObject, err error)
40+
RunCiCdSteps(stepType helper.StepType, ciCdRequest *helper.CommonWorkflowRequest, steps []*helper.StepObject, refStageMap map[int][]*helper.StepObject, scriptEnvVariables *util2.ScriptEnvVariables, preCiStageVariable map[int]map[string]*commonBean.VariableObject, resetEnvVariable bool) (pluginArtifacts *helper.PluginArtifacts, outVars map[int]map[string]*commonBean.VariableObject, failedStep *helper.StepObject, err error)
4141
RunCdStageTasks(ciContext cictx.CiContext, tasks []*helper.Task, scriptEnvVariables *util2.ScriptEnvVariables, stageType helper.PipelineType) error
4242
}
4343

@@ -48,7 +48,7 @@ func NewStageExecutorImpl(cmdExecutor helper.CommandExecutor, scriptExecutor Scr
4848
}
4949
}
5050

51-
func (impl *StageExecutorImpl) RunCiCdSteps(stepType helper.StepType, ciCdRequest *helper.CommonWorkflowRequest, steps []*helper.StepObject, refStageMap map[int][]*helper.StepObject, scriptEnvVariables *util2.ScriptEnvVariables, preCiStageVariable map[int]map[string]*commonBean.VariableObject) (*helper.PluginArtifacts, map[int]map[string]*commonBean.VariableObject, *helper.StepObject, error) {
51+
func (impl *StageExecutorImpl) RunCiCdSteps(stepType helper.StepType, ciCdRequest *helper.CommonWorkflowRequest, steps []*helper.StepObject, refStageMap map[int][]*helper.StepObject, scriptEnvVariables *util2.ScriptEnvVariables, preCiStageVariable map[int]map[string]*commonBean.VariableObject, resetEnvVariable bool) (*helper.PluginArtifacts, map[int]map[string]*commonBean.VariableObject, *helper.StepObject, error) {
5252
/*if stageType == STEP_TYPE_POST {
5353
postCiStageVariable = make(map[int]map[string]*VariableObject) // [stepId]name[]value
5454
}*/
@@ -65,7 +65,7 @@ func (impl *StageExecutorImpl) RunCiCdSteps(stepType helper.StepType, ciCdReques
6565
)
6666

6767
executeStep := func() error {
68-
refPluginArtifacts, failedStep, err = impl.RunCiCdStep(stepType, *ciCdRequest, i, step, refStageMap, scriptEnvVariables, preCiStageVariable, stageVariable)
68+
refPluginArtifacts, failedStep, err = impl.RunCiCdStep(stepType, *ciCdRequest, i, step, refStageMap, scriptEnvVariables, preCiStageVariable, stageVariable, resetEnvVariable)
6969
if err != nil {
7070
return err
7171
}
@@ -140,7 +140,7 @@ func getScriptVariables(step *helper.StepObject, scriptEnvVariables *util2.Scrip
140140

141141
func (impl *StageExecutorImpl) RunCiCdStep(stepType helper.StepType, ciCdRequest helper.CommonWorkflowRequest, index int, step *helper.StepObject,
142142
refStageMap map[int][]*helper.StepObject, scriptEnvVariables *util2.ScriptEnvVariables, preCiStageVariable map[int]map[string]*commonBean.VariableObject,
143-
stageVariable map[int]map[string]*commonBean.VariableObject) (artifacts *helper.PluginArtifacts, failedStep *helper.StepObject, err error) {
143+
stageVariable map[int]map[string]*commonBean.VariableObject, resetEnvVariable bool) (artifacts *helper.PluginArtifacts, failedStep *helper.StepObject, err error) {
144144
var vars []*commonBean.VariableObject
145145
if stepType == helper.STEP_TYPE_REF_PLUGIN {
146146
vars, err = deduceVariables(step.InputVars, scriptEnvVariables, nil, nil, stageVariable)
@@ -317,7 +317,7 @@ func (impl *StageExecutorImpl) RunCiCdStep(stepType helper.StepType, ciCdRequest
317317
}
318318
}
319319
}
320-
refPluginArtifacts, opt, _, err := impl.RunCiCdSteps(helper.STEP_TYPE_REF_PLUGIN, &ciCdRequest, steps, refStageMap, scriptEnvVariables, nil)
320+
refPluginArtifacts, opt, _, err := impl.RunCiCdSteps(helper.STEP_TYPE_REF_PLUGIN, &ciCdRequest, steps, refStageMap, scriptEnvVariables, nil, false)
321321
if err != nil {
322322
fmt.Println(err)
323323
return nil, step, err
@@ -358,6 +358,10 @@ func (impl *StageExecutorImpl) RunCiCdStep(stepType helper.StepType, ciCdRequest
358358
finalOutVarMap[out.Name] = out
359359
}
360360
stageVariable[step.Index] = finalOutVarMap
361+
// TODO Prakhar: test and restructure
362+
if resetEnvVariable {
363+
scriptEnvVariables = scriptEnvVariables.ResetExistingScriptEnv()
364+
}
361365
return pluginArtifacts, nil, nil
362366
}
363367

ci-runner/executor/stage/cdStages.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,13 @@ func (impl *CdStage) runCDStages(ciCdRequest *helper.CiCdTriggerEvent) (*helper.
191191
scriptEnvs.SystemEnv["DEST"] = ciCdRequest.CommonWorkflowRequest.CiArtifactDTO.Image
192192
scriptEnvs.SystemEnv["DIGEST"] = ciCdRequest.CommonWorkflowRequest.CiArtifactDTO.ImageDigest
193193
var stage = helper.StepType(ciCdRequest.CommonWorkflowRequest.StageType)
194-
pluginArtifacts, _, step, err := impl.stageExecutorManager.RunCiCdSteps(stage, ciCdRequest.CommonWorkflowRequest, ciCdRequest.CommonWorkflowRequest.PrePostDeploySteps, refStageMap, scriptEnvs, nil)
194+
pluginArtifacts, _, step, err := impl.stageExecutorManager.RunCiCdSteps(stage, ciCdRequest.CommonWorkflowRequest, ciCdRequest.CommonWorkflowRequest.PrePostDeploySteps, refStageMap, scriptEnvs, nil, true)
195195
if err != nil {
196196
return allPluginArtifacts, helper.NewCdStageError(err).
197197
WithFailureMessage(fmt.Sprintf(workFlow.CdStageTaskFailed.String(), ciCdRequest.CommonWorkflowRequest.GetCdStageType(), step.Name)).
198198
WithArtifactUploaded(false)
199199
}
200+
scriptEnvs = scriptEnvs.ResetExistingScriptEnv()
200201
allPluginArtifacts.MergePluginArtifact(pluginArtifacts)
201202
} else {
202203

ci-runner/executor/stage/ciStages.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func (impl *CiStage) runPreCiSteps(ciCdRequest *helper.CiCdTriggerEvent, metrics
335335
log.Println("running PRE-CI steps")
336336
}
337337
// run pre artifact processing
338-
_, preCiStageOutVariable, step, err := impl.stageExecutorManager.RunCiCdSteps(helper.STEP_TYPE_PRE, ciCdRequest.CommonWorkflowRequest, ciCdRequest.CommonWorkflowRequest.PreCiSteps, refStageMap, scriptEnvs, nil)
338+
_, preCiStageOutVariable, step, err := impl.stageExecutorManager.RunCiCdSteps(helper.STEP_TYPE_PRE, ciCdRequest.CommonWorkflowRequest, ciCdRequest.CommonWorkflowRequest.PreCiSteps, refStageMap, scriptEnvs, nil, true)
339339
preCiDuration := time.Since(start).Seconds()
340340
if err != nil {
341341
log.Println("error in running pre Ci Steps", "err", err)
@@ -344,6 +344,7 @@ func (impl *CiStage) runPreCiSteps(ciCdRequest *helper.CiCdTriggerEvent, metrics
344344
WithFailureMessage(fmt.Sprintf(workFlow.PreCiFailed.String(), step.Name)).
345345
WithArtifactUploaded(artifactUploaded)
346346
}
347+
scriptEnvs = scriptEnvs.ResetExistingScriptEnv()
347348
// considering pull images from Container repo Plugin in Pre ci steps only.
348349
// making it non-blocking if results are not available (in case of err)
349350
resultsFromPlugin, fileErr := extractOutResultsIfExists()
@@ -371,7 +372,8 @@ func (impl *CiStage) runBuildArtifact(ciCdRequest *helper.CiCdTriggerEvent, metr
371372
// build success will always be false
372373
scriptEnvs.SystemEnv[util.ENV_VARIABLE_BUILD_SUCCESS] = "false"
373374
// run post artifact processing
374-
impl.stageExecutorManager.RunCiCdSteps(helper.STEP_TYPE_POST, ciCdRequest.CommonWorkflowRequest, postCiStepsToTriggerOnCiFail, refStageMap, scriptEnvs, preCiStageOutVariable)
375+
impl.stageExecutorManager.RunCiCdSteps(helper.STEP_TYPE_POST, ciCdRequest.CommonWorkflowRequest, postCiStepsToTriggerOnCiFail, refStageMap, scriptEnvs, preCiStageOutVariable, true)
376+
scriptEnvs = scriptEnvs.ResetExistingScriptEnv()
375377
}
376378
// code-block ends
377379
err = helper.NewCiStageError(err).
@@ -416,14 +418,15 @@ func (impl *CiStage) runPostCiSteps(ciCdRequest *helper.CiCdTriggerEvent, script
416418
scriptEnvs.SystemEnv["DEST"] = dest
417419
scriptEnvs.SystemEnv["DIGEST"] = digest
418420
// run post artifact processing
419-
pluginArtifactsFromFile, _, step, err := impl.stageExecutorManager.RunCiCdSteps(helper.STEP_TYPE_POST, ciCdRequest.CommonWorkflowRequest, ciCdRequest.CommonWorkflowRequest.PostCiSteps, refStageMap, scriptEnvs, preCiStageOutVariable)
421+
pluginArtifactsFromFile, _, step, err := impl.stageExecutorManager.RunCiCdSteps(helper.STEP_TYPE_POST, ciCdRequest.CommonWorkflowRequest, ciCdRequest.CommonWorkflowRequest.PostCiSteps, refStageMap, scriptEnvs, preCiStageOutVariable, true)
420422
if err != nil {
421423
log.Println("error in running Post Ci Steps", "err", err)
422424
return nil, nil, helper.NewCiStageError(err).
423425
WithMetrics(metrics).
424426
WithFailureMessage(fmt.Sprintf(workFlow.PostCiFailed.String(), step.Name)).
425427
WithArtifactUploaded(artifactUploaded)
426428
}
429+
scriptEnvs = scriptEnvs.ResetExistingScriptEnv()
427430
//sent by orchestrator if copy container image v2 is configured
428431

429432
// considering pull images from Container repo Plugin in post ci steps also.

ci-runner/executor/util/envUtils.go

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ type ScriptEnvVariables struct {
3434
ExistingScriptEnv map[string]string
3535
}
3636

37+
func (s *ScriptEnvVariables) ResetExistingScriptEnv() *ScriptEnvVariables {
38+
s.ExistingScriptEnv = make(map[string]string)
39+
return s
40+
}
41+
3742
func getRuntimeEnvVariables(ciCdRequest *helper.CiCdTriggerEvent) map[string]string {
3843
if ciCdRequest.CommonWorkflowRequest.RuntimeEnvironmentVariables == nil {
3944
return make(map[string]string)

0 commit comments

Comments
 (0)