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:
ChristopherHX 2023-08-08 16:14:46 +02:00 committed by GitHub
parent 3939f48e6d
commit 0b4c67a4aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 10 deletions

View file

@ -3,12 +3,14 @@ package runner
import ( import (
"context" "context"
"fmt" "fmt"
"runtime"
"strings" "strings"
"github.com/kballard/go-shellquote" "github.com/kballard/go-shellquote"
"github.com/nektos/act/pkg/common" "github.com/nektos/act/pkg/common"
"github.com/nektos/act/pkg/container" "github.com/nektos/act/pkg/container"
"github.com/nektos/act/pkg/lookpath"
"github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/model"
) )
@ -138,6 +140,22 @@ func (sr *stepRun) setupShellCommand(ctx context.Context) (name, script string,
return name, script, err 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) { func (sr *stepRun) setupShell(ctx context.Context) {
rc := sr.RunContext rc := sr.RunContext
step := sr.Step step := sr.Step
@ -152,13 +170,25 @@ func (sr *stepRun) setupShell(ctx context.Context) {
step.Shell = rc.Run.Workflow.Defaults.Run.Shell step.Shell = rc.Run.Workflow.Defaults.Run.Shell
} }
// current GitHub Runner behaviour is that default is `sh`, if step.Shell == "" {
// but if it's not container it validates with `which` command if _, ok := rc.JobContainer.(*container.HostEnvironment); ok {
// if `bash` is available, and provides `bash` if it is shellWithFallback := []string{"bash", "sh"}
// for now I'm going to leave below logic, will address it in different PR // Don't use bash on windows by default, if not using a docker container
// https://github.com/actions/runner/blob/9a829995e02d2db64efb939dc2f283002595d4d9/src/Runner.Worker/Handlers/ScriptHandler.cs#L87-L91 if runtime.GOOS == "windows" {
if rc.Run.Job().Container() != nil { shellWithFallback = []string{"pwsh", "powershell"}
if rc.Run.Job().Container().Image != "" && step.Shell == "" { }
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" step.Shell = "sh"
} }
} }

View file

@ -1,8 +1,5 @@
on: on:
push: push:
defaults:
run:
shell: pwsh
jobs: jobs:
test: test:
runs-on: windows-latest runs-on: windows-latest