Skip to content

Commit

Permalink
fix job output vars on vm
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <zhaoyu@koderover.com>
  • Loading branch information
PetrusZ committed Jun 18, 2024
1 parent 25c40ce commit 881ea5f
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 22 deletions.
2 changes: 2 additions & 0 deletions pkg/cli/zadig-agent/internal/agent/job/job_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ func (e *JobExecutor) AfterExecute() error {

func (e *JobExecutor) getJobOutputVars() ([]*job.JobOutput, error) {
outputs := []*job.JobOutput{}
log.Debugf("job %s, outputs: %v", e.Job.JobName, e.JobCtx.Outputs)
for _, outputName := range e.JobCtx.Outputs {
fileContents, err := ioutil.ReadFile(filepath.Join(e.Dirs.JobOutputsDir, outputName))
if os.IsNotExist(err) {
Expand All @@ -386,6 +387,7 @@ func (e *JobExecutor) getJobOutputVars() ([]*job.JobOutput, error) {

value := strings.TrimSpace(string(fileContents))
outputs = append(outputs, &job.JobOutput{Name: outputName, Value: value})
log.Debugf("job %s, output %s: %s", e.Job.JobName, outputName, value)
}
return outputs, nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/zadig-agent/internal/agent/step/script/step_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ func generateScript(spec *StepShellSpec, dirs *types.AgentWorkDirs, jobOutput []
if len(spec.Scripts) == 0 {
return "", nil
}
log.Debugf("envmaps: %+v", envmaps)
scripts := []string{}
scripts = append(scripts, spec.Scripts...)
scripts = helper.ReplaceEnvArrWithValue(scripts, envmaps)

// add job output to script
if len(jobOutput) > 0 {
log.Debugf("jobOutputsDir: %s, joboutput: %v", dirs.JobOutputsDir, jobOutput)
scripts = append(scripts, outputScript(dirs.JobOutputsDir, jobOutput)...)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
krkubeclient "github.com/koderover/zadig/v2/pkg/tool/kube/client"
"github.com/koderover/zadig/v2/pkg/tool/kube/informer"
"github.com/koderover/zadig/v2/pkg/tool/kube/updater"
"github.com/koderover/zadig/v2/pkg/tool/log"
"github.com/koderover/zadig/v2/pkg/types/step"
)

Expand Down Expand Up @@ -419,6 +420,7 @@ func getVMJobOutputFromJobDB(jobID, jobName string, job *commonmodels.JobTask, w
func BuildJobExcutorContext(jobTaskSpec *commonmodels.JobTaskFreestyleSpec, job *commonmodels.JobTask, workflowCtx *commonmodels.WorkflowTaskCtx, logger *zap.SugaredLogger) *JobContext {
var envVars, secretEnvVars []string
for _, env := range jobTaskSpec.Properties.Envs {
log.Debugf("env: %v", env)
if env.IsCredential {
secretEnvVars = append(secretEnvVars, strings.Join([]string{env.Key, env.Value}, "="))
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func instantiateStepCtl(step *commonmodels.StepTask, workflowCtx *commonmodels.W
case config.StepShell:
stepCtl, err = NewShellCtl(step, logger)
case config.StepPowerShell:
stepCtl, err = NewBatchFileCtl(step, logger)
stepCtl, err = NewPowerShellCtl(step, logger)
case config.StepBatchFile:
stepCtl, err = NewBatchFileCtl(step, logger)
case config.StepDockerBuild:
Expand Down
12 changes: 2 additions & 10 deletions pkg/microservice/aslan/core/workflow/service/workflow/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,21 +660,13 @@ func getOutputKey(jobKey string, outputs []*commonmodels.Output) []string {
}

// generate script to save outputs variable to file
func outputScript(outputs []*commonmodels.Output, scriptType types.ScriptType) []string {
func outputScript(outputs []*commonmodels.Output, infrastructure string) []string {
resp := []string{}
if scriptType == "" || scriptType == types.ScriptTypeShell {
if infrastructure == "" || infrastructure == setting.JobK8sInfrastructure {
resp = []string{"set +ex"}
for _, output := range outputs {
resp = append(resp, fmt.Sprintf("echo $%s > %s", output.Name, path.Join(job.JobOutputDir, output.Name)))
}
} else if scriptType == types.ScriptTypeBatchFile {
for _, output := range outputs {
resp = append(resp, fmt.Sprintf("echo %%%s%% > %s", output.Name, path.Join(job.JobOutputDir, output.Name)))
}
} else if scriptType == types.ScriptTypePowerShell {
for _, output := range outputs {
resp = append(resp, fmt.Sprintf("$env:%s > \"%s\"", output.Name, path.Join(job.JobOutputDir, output.Name)))
}
}
return resp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func (j *BuildJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
scripts = append(scripts, strings.Split(replaceWrapLine(buildInfo.Scripts), "\n")...)
} else {
scripts = append([]string{dockerLoginCmd}, strings.Split(replaceWrapLine(buildInfo.Scripts), "\n")...)
scripts = append(scripts, outputScript(outputs, buildInfo.ScriptType)...)
scripts = append(scripts, outputScript(outputs, jobTask.Infrastructure)...)
}
scriptStep := &commonmodels.StepTask{
JobName: jobTask.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (j *FreeStyleJob) stepsToStepTasks(step []*commonmodels.Step) []*commonmode
if err := commonmodels.IToi(stepTask.Spec, stepTaskSpec); err != nil {
continue
}
stepTaskSpec.Scripts = append(strings.Split(replaceWrapLine(stepTaskSpec.Script), "\n"), outputScript(j.spec.Outputs, "")...)
stepTaskSpec.Scripts = append(strings.Split(replaceWrapLine(stepTaskSpec.Script), "\n"), outputScript(j.spec.Outputs, j.spec.Properties.Infrastructure)...)
stepTask.Spec = stepTaskSpec
// add debug step before shell step
debugBeforeStep := &commonmodels.StepTask{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,21 +514,21 @@ func (j *ScanningJob) toJobTask(scanning *commonmodels.ScanningModule, taskID in
scriptStep.Name = scanning.Name + "-shell"
scriptStep.StepType = config.StepShell
scriptStep.Spec = &step.StepShellSpec{
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, scanningInfo.ScriptType)...),
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, jobTask.Infrastructure)...),
SkipPrepare: true,
}
} else if scanningInfo.ScriptType == types.ScriptTypeBatchFile {
scriptStep.Name = scanning.Name + "-batchfile"
scriptStep.StepType = config.StepBatchFile
scriptStep.Spec = &step.StepBatchFileSpec{
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, scanningInfo.ScriptType)...),
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, jobTask.Infrastructure)...),
SkipPrepare: true,
}
} else if scanningInfo.ScriptType == types.ScriptTypePowerShell {
scriptStep.Name = scanning.Name + "-powershell"
scriptStep.StepType = config.StepPowerShell
scriptStep.Spec = &step.StepPowerShellSpec{
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, scanningInfo.ScriptType)...),
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, jobTask.Infrastructure)...),
SkipPrepare: true,
}
}
Expand Down Expand Up @@ -642,19 +642,19 @@ func (j *ScanningJob) toJobTask(scanning *commonmodels.ScanningModule, taskID in
scriptStep.Name = scanning.Name + "-shell"
scriptStep.StepType = config.StepShell
scriptStep.Spec = &step.StepShellSpec{
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, scanningInfo.ScriptType)...),
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, jobTask.Infrastructure)...),
}
} else if scanningInfo.ScriptType == types.ScriptTypeBatchFile {
scriptStep.Name = scanning.Name + "-batchfile"
scriptStep.StepType = config.StepBatchFile
scriptStep.Spec = &step.StepBatchFileSpec{
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, scanningInfo.ScriptType)...),
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, jobTask.Infrastructure)...),
}
} else if scanningInfo.ScriptType == types.ScriptTypePowerShell {
scriptStep.Name = scanning.Name + "-powershell"
scriptStep.StepType = config.StepPowerShell
scriptStep.Spec = &step.StepPowerShellSpec{
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, scanningInfo.ScriptType)...),
Scripts: append(strings.Split(replaceWrapLine(scanningInfo.Script), "\n"), outputScript(scanningInfo.Outputs, jobTask.Infrastructure)...),
}
}
jobTaskSpec.Steps = append(jobTaskSpec.Steps, scriptStep)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,19 +484,19 @@ func (j *TestingJob) toJobtask(testing *commonmodels.TestModule, defaultS3 *comm
scriptStep.Name = testing.Name + "-shell"
scriptStep.StepType = config.StepShell
scriptStep.Spec = &step.StepShellSpec{
Scripts: append(strings.Split(replaceWrapLine(testingInfo.Scripts), "\n"), outputScript(testingInfo.Outputs, testingInfo.ScriptType)...),
Scripts: append(strings.Split(replaceWrapLine(testingInfo.Scripts), "\n"), outputScript(testingInfo.Outputs, jobTask.Infrastructure)...),
}
} else if testingInfo.ScriptType == types.ScriptTypeBatchFile {
scriptStep.Name = testing.Name + "-batchfile"
scriptStep.StepType = config.StepBatchFile
scriptStep.Spec = &step.StepBatchFileSpec{
Scripts: append(strings.Split(replaceWrapLine(testingInfo.Scripts), "\n"), outputScript(testingInfo.Outputs, testingInfo.ScriptType)...),
Scripts: append(strings.Split(replaceWrapLine(testingInfo.Scripts), "\n"), outputScript(testingInfo.Outputs, jobTask.Infrastructure)...),
}
} else if testingInfo.ScriptType == types.ScriptTypePowerShell {
scriptStep.Name = testing.Name + "-powershell"
scriptStep.StepType = config.StepPowerShell
scriptStep.Spec = &step.StepPowerShellSpec{
Scripts: append(strings.Split(replaceWrapLine(testingInfo.Scripts), "\n"), outputScript(testingInfo.Outputs, testingInfo.ScriptType)...),
Scripts: append(strings.Split(replaceWrapLine(testingInfo.Scripts), "\n"), outputScript(testingInfo.Outputs, jobTask.Infrastructure)...),
}
}
jobTaskSpec.Steps = append(jobTaskSpec.Steps, scriptStep)
Expand Down

0 comments on commit 881ea5f

Please sign in to comment.