Skip to content

Commit 3a9d402

Browse files
bug/issue nektos#2448 - manage special bash options when no shell is defined
1 parent f75a2d8 commit 3a9d402

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

pkg/model/workflow.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ type Step struct {
572572
Uses string `yaml:"uses"`
573573
Run string `yaml:"run"`
574574
WorkingDirectory string `yaml:"working-directory"`
575+
// WorkflowShell is the shell really configured in the job, directly at step level or higher in defaults.run.shell
576+
WorkflowShell string
575577
Shell string `yaml:"shell"`
576578
Env yaml.Node `yaml:"env"`
577579
With map[string]string `yaml:"with"`
@@ -614,8 +616,14 @@ func (s *Step) ShellCommand() string {
614616

615617
//Reference: https://github.com/actions/runner/blob/8109c962f09d9acc473d92c595ff43afceddb347/src/Runner.Worker/Handlers/ScriptHandlerHelpers.cs#L9-L17
616618
switch s.Shell {
617-
case "", "bash":
618-
shellCommand = "bash --noprofile --norc -e -o pipefail {0}"
619+
case "":
620+
shellCommand = "bash -e {0}"
621+
case "bash":
622+
if s.WorkflowShell == "" {
623+
shellCommand = "bash -e {0}"
624+
} else {
625+
shellCommand = "bash --noprofile --norc -e -o pipefail {0}"
626+
}
619627
case "pwsh":
620628
shellCommand = "pwsh -command . '{0}'"
621629
case "python":

pkg/model/workflow_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,18 @@ func TestReadWorkflow_Strategy(t *testing.T) {
397397
func TestStep_ShellCommand(t *testing.T) {
398398
tests := []struct {
399399
shell string
400+
workflowShell string
400401
want string
401402
}{
402-
{"pwsh -v '. {0}'", "pwsh -v '. {0}'"},
403-
{"pwsh", "pwsh -command . '{0}'"},
404-
{"powershell", "powershell -command . '{0}'"},
403+
{"pwsh -v '. {0}'", "", "pwsh -v '. {0}'"},
404+
{"pwsh", "", "pwsh -command . '{0}'"},
405+
{"powershell", "", "powershell -command . '{0}'"},
406+
{"bash", "", "bash -e {0}"},
407+
{"bash", "bash", "bash --noprofile --norc -e -o pipefail {0}"},
405408
}
406409
for _, tt := range tests {
407410
t.Run(tt.shell, func(t *testing.T) {
408-
got := (&Step{Shell: tt.shell}).ShellCommand()
411+
got := (&Step{Shell: tt.shell, WorkflowShell: tt.workflowShell}).ShellCommand()
409412
assert.Equal(t, got, tt.want)
410413
})
411414
}

pkg/runner/step_run.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,16 @@ func (sr *stepRun) setupShell(ctx context.Context) {
166166
step := sr.Step
167167

168168
if step.Shell == "" {
169-
step.Shell = rc.Run.Job().Defaults.Run.Shell
169+
step.WorkflowShell = rc.Run.Job().Defaults.Run.Shell
170170
}
171171

172-
step.Shell = rc.NewExpressionEvaluator(ctx).Interpolate(ctx, step.Shell)
172+
step.WorkflowShell = rc.NewExpressionEvaluator(ctx).Interpolate(ctx, step.Shell)
173173

174-
if step.Shell == "" {
175-
step.Shell = rc.Run.Workflow.Defaults.Run.Shell
174+
if step.WorkflowShell == "" {
175+
step.WorkflowShell = rc.Run.Workflow.Defaults.Run.Shell
176176
}
177177

178-
if step.Shell == "" {
178+
if step.WorkflowShell == "" {
179179
if _, ok := rc.JobContainer.(*container.HostEnvironment); ok {
180180
shellWithFallback := []string{"bash", "sh"}
181181
// Don't use bash on windows by default, if not using a docker container
@@ -196,6 +196,8 @@ func (sr *stepRun) setupShell(ctx context.Context) {
196196
// Currently only linux containers are supported, use sh by default like actions/runner
197197
step.Shell = "sh"
198198
}
199+
} else {
200+
step.Shell = step.WorkflowShell
199201
}
200202
}
201203

0 commit comments

Comments
 (0)