Skip to content

Commit

Permalink
feat: cmd support for windows (#1941)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
ChristopherHX and mergify[bot] authored Aug 8, 2023
1 parent 6468dd7 commit 8314095
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
6 changes: 5 additions & 1 deletion pkg/container/host_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,12 @@ func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline st
}

func (e *HostEnvironment) Exec(command []string /*cmdline string, */, env map[string]string, user, workdir string) common.Executor {
return e.ExecWithCmdLine(command, "", env, user, workdir)
}

func (e *HostEnvironment) ExecWithCmdLine(command []string, cmdline string, env map[string]string, user, workdir string) common.Executor {
return func(ctx context.Context) error {
if err := e.exec(ctx, command, "" /*cmdline*/, env, user, workdir); err != nil {
if err := e.exec(ctx, command, cmdline, env, user, workdir); err != nil {
select {
case <-ctx.Done():
return fmt.Errorf("this step has been cancelled: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func (s *Step) ShellCommand() string {
case "sh":
shellCommand = "sh -e {0}"
case "cmd":
shellCommand = "%ComSpec% /D /E:ON /V:OFF /S /C \"CALL \"{0}\"\""
shellCommand = "cmd /D /E:ON /V:OFF /S /C \"CALL \"{0}\"\""
case "powershell":
shellCommand = "powershell -command . '{0}'"
default:
Expand Down
1 change: 1 addition & 0 deletions pkg/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ func TestRunEventHostEnvironment(t *testing.T) {
tables = append(tables, []TestJobFileInfo{
{workdir, "windows-prepend-path", "push", "", platforms, secrets},
{workdir, "windows-add-env", "push", "", platforms, secrets},
{workdir, "windows-shell-cmd", "push", "", platforms, secrets},
}...)
} else {
platforms := map[string]string{
Expand Down
7 changes: 6 additions & 1 deletion pkg/runner/step_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type stepRun struct {
Step *model.Step
RunContext *RunContext
cmd []string
cmdline string
env map[string]string
WorkingDirectory string
}
Expand All @@ -34,6 +35,9 @@ func (sr *stepRun) main() common.Executor {
sr.setupShellCommandExecutor(),
func(ctx context.Context) error {
sr.getRunContext().ApplyExtraPath(ctx, &sr.env)
if he, ok := sr.getRunContext().JobContainer.(*container.HostEnvironment); ok && he != nil {
return he.ExecWithCmdLine(sr.cmd, sr.cmdline, sr.env, "", sr.WorkingDirectory)(ctx)
}
return sr.getRunContext().JobContainer.Exec(sr.cmd, sr.env, "", sr.WorkingDirectory)(ctx)
},
))
Expand Down Expand Up @@ -135,7 +139,8 @@ func (sr *stepRun) setupShellCommand(ctx context.Context) (name, script string,

rc := sr.getRunContext()
scriptPath := fmt.Sprintf("%s/%s", rc.JobContainer.GetActPath(), name)
sr.cmd, err = shellquote.Split(strings.Replace(scCmd, `{0}`, scriptPath, 1))
sr.cmdline = strings.Replace(scCmd, `{0}`, scriptPath, 1)
sr.cmd, err = shellquote.Split(sr.cmdline)

return name, script, err
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/runner/testdata/windows-shell-cmd/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
on:
push:
jobs:
test:
runs-on: windows-latest
steps:
- run: |
echo Hi
shell: cmd

0 comments on commit 8314095

Please sign in to comment.