Skip to content

Commit

Permalink
Output variables change (#10)
Browse files Browse the repository at this point in the history
* output variable changes

* output variable changes
  • Loading branch information
raghavharness authored Feb 28, 2023
1 parent 1c16468 commit a1502cc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
ref string // plugin repository reference
sha string // plugin repository commit
kind string // plugin kind (action, bitrise, harness)
outputfile string // plugin outputfile
outputFile string // plugin outputfile
)

func main() {
Expand All @@ -38,7 +38,6 @@ func main() {
flag.StringVar(&ref, "ref", "", "plugin reference")
flag.StringVar(&sha, "sha", "", "plugin commit")
flag.StringVar(&kind, "kind", "", "plugin kind")
flag.StringVar(&outputfile, "outputfile", "", "filepath to store output variables")
flag.Parse()

// the user may specific the action plugin alias instead
Expand Down Expand Up @@ -104,6 +103,8 @@ func main() {
os.Exit(1)
}

outputFile = os.Getenv("DRONE_OUTPUT")

switch {
// execute harness plugin
case harness.Is(codedir) || kind == "harness":
Expand Down Expand Up @@ -131,7 +132,7 @@ func main() {
Environ: bitrise.Environ(
os.Environ(),
),
Outputfile: outputfile,
OutputFile: outputFile,
}
if err := execer.Exec(ctx); err != nil {
log.Error("step failed", err)
Expand All @@ -146,7 +147,7 @@ func main() {
Stdout: os.Stdout,
Stderr: os.Stderr,
Environ: os.Environ(),
Outputfile: outputfile,
OutputFile: outputFile,
}
if err := execer.Exec(ctx); err != nil {
log.Error("action step failed", err)
Expand Down
6 changes: 3 additions & 3 deletions plugin/bitrise/execer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Execer struct {
Environ []string
Stdout io.Writer
Stderr io.Writer
Outputfile string
OutputFile string
}

// Exec executes a bitrise plugin.
Expand Down Expand Up @@ -149,9 +149,9 @@ func (e *Execer) Exec(ctx context.Context) error {
}

// save to outputfile if present
if len(e.Outputfile) > 0 {
if len(e.OutputFile) > 0 {
if m, err := readEnvStore(e.Source); err == nil && len(m.Envs) > 0 {
if err = saveOutputFromEnvStore(m.Envs, e.Outputfile); err != nil {
if err = saveOutputFromEnvStore(m.Envs, e.OutputFile); err != nil {
slog.FromContext(ctx).Error("Unable to save output", err)
}
} else if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions plugin/github/execer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Execer struct {
Name string
Environ []string
TmpDir string
Outputfile string
OutputFile string
Stdout io.Writer
Stderr io.Writer
}
Expand All @@ -41,7 +41,7 @@ func (e *Execer) Exec(ctx context.Context) error {
workflowFile := filepath.Join(e.TmpDir, "workflow.yml")
beforeStepEnvFile := filepath.Join(e.TmpDir, "before.env")
afterStepEnvFile := filepath.Join(e.TmpDir, "after.env")
if err := createWorkflowFile(e.Name, envVars, workflowFile, beforeStepEnvFile, afterStepEnvFile, e.Outputfile, outputVars); err != nil {
if err := createWorkflowFile(e.Name, envVars, workflowFile, beforeStepEnvFile, afterStepEnvFile, e.OutputFile, outputVars); err != nil {
return err
}

Expand Down
16 changes: 8 additions & 8 deletions plugin/github/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (
)

func createWorkflowFile(action string, envVars map[string]string,
ymlFile, beforeStepEnvFile, afterStepEnvFile string, outputfile string, output_vars []string) error {
ymlFile, beforeStepEnvFile, afterStepEnvFile string, outputFile string, outputVars []string) error {
with, err := getWith(envVars)
if err != nil {
return err
Expand All @@ -63,7 +63,7 @@ func createWorkflowFile(action string, envVars map[string]string,
With: with,
Env: env,
},
getOutputVariables(stepId, outputfile, output_vars),
getOutputVariables(stepId, outputFile, outputVars),
prePostStep("after", afterStepEnvFile),
},
}
Expand Down Expand Up @@ -112,17 +112,17 @@ func prePostStep(name, envFile string) step {
return s
}

func getOutputVariables(prevStepId, outputfile string, output_vars []string) step {
skip := len(outputfile) == 0 || len(output_vars) == 0
func getOutputVariables(prevStepId, outputFile string, outputVars []string) step {
skip := len(outputFile) == 0 || len(outputVars) == 0
cmd := ""
for _, output_var := range output_vars {
cmd += fmt.Sprintf("print(\"%s\"+\"=\"+\"${{ steps.%s.outputs.%s }}\"); ", output_var, prevStepId, output_var)
for _, outputVar := range outputVars {
cmd += fmt.Sprintf("print(\"%s\"+\"=\"+\"${{ steps.%s.outputs.%s }}\"); ", outputVar, prevStepId, outputVar)
}

if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
cmd = fmt.Sprintf("python3 -c '%s' > %s", cmd, outputfile)
cmd = fmt.Sprintf("python3 -c '%s' > %s", cmd, outputFile)
} else {
cmd = fmt.Sprintf("python -c '%s' > %s", cmd, outputfile)
cmd = fmt.Sprintf("python -c '%s' > %s", cmd, outputFile)
}
s := step{
Name: "output variables",
Expand Down

0 comments on commit a1502cc

Please sign in to comment.