Skip to content

Commit 2e117a4

Browse files
bug/issue #2448 - manage special bash options when no shell is defined (#2449)
* bash without "-o pipefail" option when "bash" is not explicitely defined in the workflow * bonus: fix inverted expected and actual in TestGetGitHubContext assertions
1 parent 013c0d4 commit 2e117a4

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
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 `yaml:"-"`
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/run_context_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,15 @@ func TestGetGitHubContext(t *testing.T) {
387387
owner = o
388388
}
389389

390-
assert.Equal(t, ghc.RunID, "1")
391-
assert.Equal(t, ghc.RunNumber, "1")
392-
assert.Equal(t, ghc.RetentionDays, "0")
393-
assert.Equal(t, ghc.Actor, actor)
394-
assert.Equal(t, ghc.Repository, repo)
395-
assert.Equal(t, ghc.RepositoryOwner, owner)
396-
assert.Equal(t, ghc.RunnerPerflog, "/dev/null")
397-
assert.Equal(t, ghc.Token, rc.Config.Secrets["GITHUB_TOKEN"])
398-
assert.Equal(t, ghc.Job, "job1")
390+
assert.Equal(t, "1", ghc.RunID)
391+
assert.Equal(t, "1", ghc.RunNumber)
392+
assert.Equal(t, "0", ghc.RetentionDays)
393+
assert.Equal(t, actor, ghc.Actor)
394+
assert.Equal(t, repo, ghc.Repository)
395+
assert.Equal(t, owner, ghc.RepositoryOwner)
396+
assert.Equal(t, "/dev/null", ghc.RunnerPerflog)
397+
assert.Equal(t, rc.Config.Secrets["GITHUB_TOKEN"], ghc.Token)
398+
assert.Equal(t, "job1", ghc.Job)
399399
}
400400

401401
func TestGetGithubContextRef(t *testing.T) {

pkg/runner/step_run.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,18 @@ 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
170+
} else {
171+
step.WorkflowShell = step.Shell
170172
}
171173

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

174-
if step.Shell == "" {
175-
step.Shell = rc.Run.Workflow.Defaults.Run.Shell
176+
if step.WorkflowShell == "" {
177+
step.WorkflowShell = rc.Run.Workflow.Defaults.Run.Shell
176178
}
177179

178-
if step.Shell == "" {
180+
if step.WorkflowShell == "" {
179181
if _, ok := rc.JobContainer.(*container.HostEnvironment); ok {
180182
shellWithFallback := []string{"bash", "sh"}
181183
// Don't use bash on windows by default, if not using a docker container
@@ -196,6 +198,8 @@ func (sr *stepRun) setupShell(ctx context.Context) {
196198
// Currently only linux containers are supported, use sh by default like actions/runner
197199
step.Shell = "sh"
198200
}
201+
} else {
202+
step.Shell = step.WorkflowShell
199203
}
200204
}
201205

0 commit comments

Comments
 (0)