diff --git a/cmd/root.go b/cmd/root.go index 143a741ce3e..bbe6917b836 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,7 @@ package cmd import ( "bufio" + "bytes" "context" "fmt" "os" @@ -19,6 +20,7 @@ import ( gitignore "github.com/sabhiram/go-gitignore" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/cobra/doc" "gopkg.in/yaml.v3" "github.com/nektos/act/pkg/artifactcache" @@ -47,6 +49,7 @@ func Execute(ctx context.Context, version string) { rootCmd.Flags().BoolP("graph", "g", false, "draw workflows") rootCmd.Flags().StringP("job", "j", "", "run a specific job ID") rootCmd.Flags().BoolP("bug-report", "", false, "Display system information for bug report") + rootCmd.Flags().BoolP("man-page", "", false, "Print a generated manual page to stdout") rootCmd.Flags().StringVar(&input.remoteName, "remote-name", "origin", "git remote name that will be used to retrieve url of git repo") rootCmd.Flags().StringArrayVarP(&input.secrets, "secret", "s", []string{}, "secret to make available to actions with optional value (e.g. -s mysecret=foo or -s mysecret)") @@ -227,6 +230,18 @@ func bugReport(ctx context.Context, version string) error { return nil } +func generateManPage(cmd *cobra.Command) error { + header := &doc.GenManHeader{ + Title: "act", + Section: "1", + Source: fmt.Sprintf("act %s", cmd.Version), + } + buf := new(bytes.Buffer) + cobra.CheckErr(doc.GenMan(cmd, header, buf)) + fmt.Print(buf.String()) + return nil +} + func readArgsFile(file string, split bool) []string { args := make([]string, 0) f, err := os.Open(file) @@ -341,6 +356,10 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str if ok, _ := cmd.Flags().GetBool("bug-report"); ok { return bugReport(ctx, cmd.Version) } + if ok, _ := cmd.Flags().GetBool("man-page"); ok { + return generateManPage(cmd) + } + if ret, err := container.GetSocketAndHost(input.containerDaemonSocket); err != nil { log.Warnf("Couldn't get a valid docker connection: %+v", err) } else { diff --git a/go.mod b/go.mod index 9f85299d726..30a1aa1636e 100644 --- a/go.mod +++ b/go.mod @@ -49,6 +49,7 @@ require ( github.com/cloudflare/circl v1.3.7 // indirect github.com/containerd/containerd v1.7.13 // indirect github.com/containerd/log v0.1.0 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect github.com/cyphar/filepath-securejoin v0.2.4 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/distribution/reference v0.5.0 // indirect @@ -80,6 +81,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/robfig/cron/v3 v3.0.1 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/skeema/knownhosts v1.2.2 // indirect github.com/stretchr/objx v0.5.2 // indirect diff --git a/go.sum b/go.sum index ee126c10502..c3fdca18947 100644 --- a/go.sum +++ b/go.sum @@ -35,6 +35,7 @@ github.com/containerd/containerd v1.7.13 h1:wPYKIeGMN8vaggSKuV1X0wZulpMz4CrgEsZd github.com/containerd/containerd v1.7.13/go.mod h1:zT3up6yTRfEUa6+GsITYIJNgSVL9NQ4x4h1RPzk0Wu4= github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= +github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0= @@ -168,6 +169,7 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI= github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs= diff --git a/pkg/model/workflow.go b/pkg/model/workflow.go index e66bf4ce949..57e8f57dc61 100644 --- a/pkg/model/workflow.go +++ b/pkg/model/workflow.go @@ -572,6 +572,8 @@ type Step struct { Uses string `yaml:"uses"` Run string `yaml:"run"` WorkingDirectory string `yaml:"working-directory"` + // WorkflowShell is the shell really configured in the job, directly at step level or higher in defaults.run.shell + WorkflowShell string `yaml:"-"` Shell string `yaml:"shell"` Env yaml.Node `yaml:"env"` With map[string]string `yaml:"with"` @@ -614,8 +616,14 @@ func (s *Step) ShellCommand() string { //Reference: https://github.com/actions/runner/blob/8109c962f09d9acc473d92c595ff43afceddb347/src/Runner.Worker/Handlers/ScriptHandlerHelpers.cs#L9-L17 switch s.Shell { - case "", "bash": - shellCommand = "bash --noprofile --norc -e -o pipefail {0}" + case "": + shellCommand = "bash -e {0}" + case "bash": + if s.WorkflowShell == "" { + shellCommand = "bash -e {0}" + } else { + shellCommand = "bash --noprofile --norc -e -o pipefail {0}" + } case "pwsh": shellCommand = "pwsh -command . '{0}'" case "python": diff --git a/pkg/model/workflow_test.go b/pkg/model/workflow_test.go index 03f29fd6967..92de8ca33fe 100644 --- a/pkg/model/workflow_test.go +++ b/pkg/model/workflow_test.go @@ -397,15 +397,18 @@ func TestReadWorkflow_Strategy(t *testing.T) { func TestStep_ShellCommand(t *testing.T) { tests := []struct { shell string + workflowShell string want string }{ - {"pwsh -v '. {0}'", "pwsh -v '. {0}'"}, - {"pwsh", "pwsh -command . '{0}'"}, - {"powershell", "powershell -command . '{0}'"}, + {"pwsh -v '. {0}'", "", "pwsh -v '. {0}'"}, + {"pwsh", "", "pwsh -command . '{0}'"}, + {"powershell", "", "powershell -command . '{0}'"}, + {"bash", "", "bash -e {0}"}, + {"bash", "bash", "bash --noprofile --norc -e -o pipefail {0}"}, } for _, tt := range tests { t.Run(tt.shell, func(t *testing.T) { - got := (&Step{Shell: tt.shell}).ShellCommand() + got := (&Step{Shell: tt.shell, WorkflowShell: tt.workflowShell}).ShellCommand() assert.Equal(t, got, tt.want) }) } diff --git a/pkg/runner/run_context_test.go b/pkg/runner/run_context_test.go index 692b4eefa75..0656aad881c 100644 --- a/pkg/runner/run_context_test.go +++ b/pkg/runner/run_context_test.go @@ -387,15 +387,15 @@ func TestGetGitHubContext(t *testing.T) { owner = o } - assert.Equal(t, ghc.RunID, "1") - assert.Equal(t, ghc.RunNumber, "1") - assert.Equal(t, ghc.RetentionDays, "0") - assert.Equal(t, ghc.Actor, actor) - assert.Equal(t, ghc.Repository, repo) - assert.Equal(t, ghc.RepositoryOwner, owner) - assert.Equal(t, ghc.RunnerPerflog, "/dev/null") - assert.Equal(t, ghc.Token, rc.Config.Secrets["GITHUB_TOKEN"]) - assert.Equal(t, ghc.Job, "job1") + assert.Equal(t, "1", ghc.RunID) + assert.Equal(t, "1", ghc.RunNumber) + assert.Equal(t, "0", ghc.RetentionDays) + assert.Equal(t, actor, ghc.Actor) + assert.Equal(t, repo, ghc.Repository) + assert.Equal(t, owner, ghc.RepositoryOwner) + assert.Equal(t, "/dev/null", ghc.RunnerPerflog) + assert.Equal(t, rc.Config.Secrets["GITHUB_TOKEN"], ghc.Token) + assert.Equal(t, "job1", ghc.Job) } func TestGetGithubContextRef(t *testing.T) { diff --git a/pkg/runner/step_run.go b/pkg/runner/step_run.go index 054ed702321..a905a20e1f6 100644 --- a/pkg/runner/step_run.go +++ b/pkg/runner/step_run.go @@ -166,16 +166,18 @@ func (sr *stepRun) setupShell(ctx context.Context) { step := sr.Step if step.Shell == "" { - step.Shell = rc.Run.Job().Defaults.Run.Shell + step.WorkflowShell = rc.Run.Job().Defaults.Run.Shell + } else { + step.WorkflowShell = step.Shell } - step.Shell = rc.NewExpressionEvaluator(ctx).Interpolate(ctx, step.Shell) + step.WorkflowShell = rc.NewExpressionEvaluator(ctx).Interpolate(ctx, step.WorkflowShell) - if step.Shell == "" { - step.Shell = rc.Run.Workflow.Defaults.Run.Shell + if step.WorkflowShell == "" { + step.WorkflowShell = rc.Run.Workflow.Defaults.Run.Shell } - if step.Shell == "" { + if step.WorkflowShell == "" { 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 @@ -196,6 +198,8 @@ func (sr *stepRun) setupShell(ctx context.Context) { // Currently only linux containers are supported, use sh by default like actions/runner step.Shell = "sh" } + } else { + step.Shell = step.WorkflowShell } }