prefer pwsh on windows if found (#1942)
* prefer pwsh on windows if found prefer bash over sh if found One windows test no longer defines a default shell to test if it's pwsh * add dep --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
parent
3939f48e6d
commit
0b4c67a4aa
2 changed files with 37 additions and 10 deletions
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
3
pkg/runner/testdata/windows-add-env/push.yml
vendored
3
pkg/runner/testdata/windows-add-env/push.yml
vendored
|
@ -1,8 +1,5 @@
|
|||
on:
|
||||
push:
|
||||
defaults:
|
||||
run:
|
||||
shell: pwsh
|
||||
jobs:
|
||||
test:
|
||||
runs-on: windows-latest
|
||||
|
|
Loading…
Add table
Reference in a new issue