diff --git a/pkg/runner/step_run.go b/pkg/runner/step_run.go index 9d887e7..53de414 100644 --- a/pkg/runner/step_run.go +++ b/pkg/runner/step_run.go @@ -3,12 +3,14 @@ package runner import ( "context" "fmt" + "runtime" "strings" "github.com/kballard/go-shellquote" "github.com/nektos/act/pkg/common" "github.com/nektos/act/pkg/container" + "github.com/nektos/act/pkg/lookpath" "github.com/nektos/act/pkg/model" ) @@ -138,6 +140,22 @@ func (sr *stepRun) setupShellCommand(ctx context.Context) (name, script string, return name, script, err } +type localEnv struct { + env map[string]string +} + +func (l *localEnv) Getenv(name string) string { + if runtime.GOOS == "windows" { + for k, v := range l.env { + if strings.EqualFold(name, k) { + return v + } + } + return "" + } + return l.env[name] +} + func (sr *stepRun) setupShell(ctx context.Context) { rc := sr.RunContext step := sr.Step @@ -152,13 +170,25 @@ func (sr *stepRun) setupShell(ctx context.Context) { step.Shell = rc.Run.Workflow.Defaults.Run.Shell } - // current GitHub Runner behaviour is that default is `sh`, - // but if it's not container it validates with `which` command - // if `bash` is available, and provides `bash` if it is - // for now I'm going to leave below logic, will address it in different PR - // https://github.com/actions/runner/blob/9a829995e02d2db64efb939dc2f283002595d4d9/src/Runner.Worker/Handlers/ScriptHandler.cs#L87-L91 - if rc.Run.Job().Container() != nil { - if rc.Run.Job().Container().Image != "" && step.Shell == "" { + if step.Shell == "" { + if _, ok := rc.JobContainer.(*container.HostEnvironment); ok { + shellWithFallback := []string{"bash", "sh"} + // Don't use bash on windows by default, if not using a docker container + if runtime.GOOS == "windows" { + shellWithFallback = []string{"pwsh", "powershell"} + } + step.Shell = shellWithFallback[0] + lenv := &localEnv{env: map[string]string{}} + for k, v := range sr.env { + lenv.env[k] = v + } + sr.getRunContext().ApplyExtraPath(ctx, &lenv.env) + _, err := lookpath.LookPath2(shellWithFallback[0], lenv) + if err != nil { + step.Shell = shellWithFallback[1] + } + } else if containerImage := rc.containerImage(ctx); containerImage != "" { + // Currently only linux containers are supported, use sh by default like actions/runner step.Shell = "sh" } } diff --git a/pkg/runner/testdata/windows-add-env/push.yml b/pkg/runner/testdata/windows-add-env/push.yml index bd233bb..7d6a81d 100644 --- a/pkg/runner/testdata/windows-add-env/push.yml +++ b/pkg/runner/testdata/windows-add-env/push.yml @@ -1,8 +1,5 @@ on: push: -defaults: - run: - shell: pwsh jobs: test: runs-on: windows-latest