Skip to content

Commit

Permalink
Add output variable support for container less plugins (#123)
Browse files Browse the repository at this point in the history
* added output variables support for containerless plugins

* fix

* fix

* fix

* changes

* changes

* changes

* changes

* changes for runtest step

* changes
  • Loading branch information
raghavharness authored Feb 28, 2023
1 parent b2e937d commit 29de594
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 54 deletions.
43 changes: 5 additions & 38 deletions pipeline/runtime/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
package runtime

import (
"bufio"
b64 "encoding/base64"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/harness/lite-engine/engine/spec"
"github.com/harness/lite-engine/logstream"
Expand Down Expand Up @@ -45,11 +43,11 @@ func getOutputVarCmd(entrypoint, outputVars []string, outputFile string) string
}
for _, o := range outputVars {
if isPsh {
cmd += fmt.Sprintf("\n$val = \"%s $Env:%s\" \nAdd-Content -Path %s -Value $val", o, o, outputFile)
cmd += fmt.Sprintf("\n$val = \"%s=$Env:%s\" \nAdd-Content -Path %s -Value $val", o, o, outputFile)
} else if isPython {
cmd += fmt.Sprintf("with open('%s', 'a') as out_file:\n\tout_file.write('%s ' + os.getenv('%s') + '\\n')\n", outputFile, o, o)
cmd += fmt.Sprintf("with open('%s', 'a') as out_file:\n\tout_file.write('%s=' + os.getenv('%s') + '\\n')\n", outputFile, o, o)
} else {
cmd += fmt.Sprintf(";echo \"%s $%s\" >> %s", o, o, outputFile)
cmd += fmt.Sprintf(";echo \"%s=$%s\" >> %s", o, o, outputFile)
}
}

Expand All @@ -70,39 +68,8 @@ func isPython(entrypoint []string) bool {
return false
}

// Fetches map of env variable and value from OutputFile.
// OutputFile stores all env variable and value
func fetchOutputVariables(outputFile string, out io.Writer) (map[string]string, error) {
log := logrus.New()
log.Out = out

outputs := make(map[string]string)
f, err := os.Open(outputFile)
if err != nil {
log.WithError(err).WithField("outputFile", outputFile).Errorln("failed to open output file")
return nil, err
}
defer f.Close()

s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
sa := strings.Split(line, " ")
if len(sa) < 2 { //nolint:gomnd
log.WithField("variable", sa[0]).Warnln("output variable does not exist")
} else {
outputs[sa[0]] = line[len(sa[0])+1:]
}
}
if err := s.Err(); err != nil {
log.WithError(err).Errorln("failed to create scanner from output file")
return nil, err
}
return outputs, nil
}

// Fetches env variable exported by the step.
func fetchExportedEnvVars(envFile string, out io.Writer) map[string]string {
// Fetches variable in env file exported by the step.
func fetchExportedVarsFromEnvFile(envFile string, out io.Writer) map[string]string {
log := logrus.New()
log.Out = out

Expand Down
17 changes: 7 additions & 10 deletions pipeline/runtime/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func executeRunStep(ctx context.Context, engine *engine.Engine, r *api.StartStep
return nil, nil, nil, fmt.Errorf("output variable should not be set for unset entrypoint or command")
}

outputFile := fmt.Sprintf("%s/%s.out", pipeline.SharedVolPath, step.ID)
outputFile := fmt.Sprintf("%s/%s-output.env", pipeline.SharedVolPath, step.ID)
step.Envs["DRONE_OUTPUT"] = outputFile

if len(r.OutputVars) > 0 {
step.Command[0] += getOutputVarCmd(step.Entrypoint, r.OutputVars, outputFile)
}
Expand All @@ -47,15 +49,10 @@ func executeRunStep(ctx context.Context, engine *engine.Engine, r *api.StartStep
logrus.WithError(rerr).WithField("step", step.Name).Errorln("failed to upload report")
}

exportEnvs := fetchExportedEnvVars(exportEnvFile, out)
if len(r.OutputVars) > 0 {
if exited != nil && exited.Exited && exited.ExitCode == 0 {
outputs, err := fetchOutputVariables(outputFile, out) //nolint:govet
if err != nil {
return exited, nil, exportEnvs, err
}
return exited, outputs, exportEnvs, err
}
exportEnvs := fetchExportedVarsFromEnvFile(exportEnvFile, out)
if exited != nil && exited.Exited && exited.ExitCode == 0 {
outputs := fetchExportedVarsFromEnvFile(outputFile, out)
return exited, outputs, exportEnvs, err
}
return exited, nil, exportEnvs, err
}
9 changes: 3 additions & 6 deletions pipeline/runtime/runtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func executeRunTestStep(ctx context.Context, engine *engine.Engine, r *api.Start
return nil, nil, nil, fmt.Errorf("output variable should not be set for unset entrypoint or command")
}

outputFile := fmt.Sprintf("%s/%s.out", pipeline.SharedVolPath, step.ID)
outputFile := fmt.Sprintf("%s/%s-output.env", pipeline.SharedVolPath, step.ID)
if len(r.OutputVars) > 0 {
step.Command[0] += getOutputVarCmd(step.Entrypoint, r.OutputVars, outputFile)
}
Expand All @@ -67,13 +67,10 @@ func executeRunTestStep(ctx context.Context, engine *engine.Engine, r *api.Start
err = collectionErr
}

exportEnvs := fetchExportedEnvVars(exportEnvFile, out)
exportEnvs := fetchExportedVarsFromEnvFile(exportEnvFile, out)
if len(r.OutputVars) > 0 {
if exited != nil && exited.Exited && exited.ExitCode == 0 {
outputs, err := fetchOutputVariables(outputFile, out) //nolint:govet
if err != nil {
return exited, nil, exportEnvs, err
}
outputs := fetchExportedVarsFromEnvFile(outputFile, out)
return exited, outputs, exportEnvs, err
}
}
Expand Down

0 comments on commit 29de594

Please sign in to comment.