Skip to content

Commit

Permalink
Fix action step on windows (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham149 authored May 3, 2023
1 parent 39cc178 commit dcf4168
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion plugin/github/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func diffEnv(before, after string) map[string]string {
// Base64 decode env values
diff := make(map[string]string)
for k, v := range diffB64 {
data, err := base64.StdEncoding.DecodeString(v)
data, err := base64.RawURLEncoding.DecodeString(v)
if err == nil {
diff[k] = string(data)
} else {
Expand Down
59 changes: 54 additions & 5 deletions plugin/github/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"runtime"

"github.com/pkg/errors"
"golang.org/x/exp/slog"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -96,11 +97,19 @@ func getWorkflowEvent() string {
}

func prePostStep(name, envFile string) step {
log := slog.Default()

script, err := dotenvScript(envFile)
if err != nil {
log.Warn(fmt.Sprintf("failed to create pre/post-step script: %s", err))
script = "--version"
}

var cmd string
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
cmd = fmt.Sprintf("python3 -c 'import os; import base64; [print(k+\"=\"+str(base64.b64encode(bytes(v, \"utf-8\")), \"utf-8\")) for k, v in os.environ.items()]' > %s", envFile)
cmd = fmt.Sprintf("python3 %s", script)
} else {
cmd = fmt.Sprintf("python -c 'import os; import base64; [print(k+\"=\"+str(base64.b64encode(bytes(v, \"utf-8\")), \"utf-8\")) for k, v in os.environ.items()]' > %s", envFile)
cmd = fmt.Sprintf("python %s", script)
}
s := step{
Name: name,
Expand All @@ -116,14 +125,18 @@ func getOutputVariables(prevStepId, outputFile string, outputVars []string) step
skip := len(outputFile) == 0 || len(outputVars) == 0
cmd := ""
for _, outputVar := range outputVars {
cmd += fmt.Sprintf("print(\"%s\"+\"=\"+\"${{ steps.%s.outputs.%s }}\"); ", outputVar, prevStepId, outputVar)
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 if runtime.GOOS == "windows" {
cmd = fmt.Sprintf("python -c \"%s\"", outputVarWinScript(
outputVars, prevStepId, 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",
Run: cmd,
Expand All @@ -134,3 +147,39 @@ func getOutputVariables(prevStepId, outputFile string, outputVars []string) step
}
return s
}

func dotenvScript(envFile string) (string, error) {
script := fmt.Sprintf(`
import os
import base64
out = ""
for k, v in os.environ.items():
if "(" not in k and ")" not in k:
out = out + "{}={}\n".format(k, str(base64.urlsafe_b64encode(bytes(v, "utf-8")), "utf-8"))
with open(r"%s", "wb") as text_file:
text_file.write(bytes(out, "UTF-8"))
`, envFile)

file, err := ioutil.TempFile("", "")
if err != nil {
return "", err
}
defer file.Close()

file.WriteString(script)
return file.Name(), nil
}

func outputVarWinScript(outputVars []string, prevStepId, outputFile string) string {
script := ""
for idx, outputVar := range outputVars {
prefix := "out = "
if idx > 0 {
prefix += "out + "
}
script += fmt.Sprintf("%s'%s=${{ steps.%s.outputs.%s }}\\n';", prefix, outputVar, stepId, outputVar)
}
script += fmt.Sprintf("f = open('%s', 'wb'); f.write(bytes(out, 'UTF-8')); f.close()", outputFile)
return script
}

0 comments on commit dcf4168

Please sign in to comment.