2022-03-23 05:13:00 +08:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/kballard/go-shellquote"
|
2023-02-16 23:34:51 +08:00
|
|
|
|
2022-03-23 05:13:00 +08:00
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
"github.com/nektos/act/pkg/container"
|
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepRun struct {
|
2023-03-28 20:24:03 +08:00
|
|
|
Step *model.Step
|
|
|
|
RunContext *RunContext
|
|
|
|
cmd []string
|
|
|
|
env map[string]string
|
|
|
|
WorkingDirectory string
|
2022-03-23 05:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *stepRun) pre() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *stepRun) main() common.Executor {
|
|
|
|
sr.env = map[string]string{}
|
2022-05-24 21:36:06 +08:00
|
|
|
return runStepExecutor(sr, stepStageMain, common.NewPipelineExecutor(
|
2022-03-23 05:13:00 +08:00
|
|
|
sr.setupShellCommandExecutor(),
|
|
|
|
func(ctx context.Context) error {
|
2023-02-04 21:35:13 +08:00
|
|
|
sr.getRunContext().ApplyExtraPath(ctx, &sr.env)
|
2023-03-28 20:24:03 +08:00
|
|
|
return sr.getRunContext().JobContainer.Exec(sr.cmd, sr.env, "", sr.WorkingDirectory)(ctx)
|
2022-03-23 05:13:00 +08:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *stepRun) post() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *stepRun) getRunContext() *RunContext {
|
|
|
|
return sr.RunContext
|
|
|
|
}
|
|
|
|
|
2022-10-07 05:58:16 +08:00
|
|
|
func (sr *stepRun) getGithubContext(ctx context.Context) *model.GithubContext {
|
|
|
|
return sr.getRunContext().getGithubContext(ctx)
|
|
|
|
}
|
|
|
|
|
2022-03-23 05:13:00 +08:00
|
|
|
func (sr *stepRun) getStepModel() *model.Step {
|
|
|
|
return sr.Step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *stepRun) getEnv() *map[string]string {
|
|
|
|
return &sr.env
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
func (sr *stepRun) getIfExpression(context context.Context, stage stepStage) string {
|
2022-05-24 21:36:06 +08:00
|
|
|
return sr.Step.If.Value
|
|
|
|
}
|
|
|
|
|
2022-03-23 05:13:00 +08:00
|
|
|
func (sr *stepRun) setupShellCommandExecutor() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
2022-05-12 03:06:05 +08:00
|
|
|
scriptName, script, err := sr.setupShellCommand(ctx)
|
2022-03-23 05:13:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-17 05:29:45 +08:00
|
|
|
rc := sr.getRunContext()
|
|
|
|
return rc.JobContainer.Copy(rc.JobContainer.GetActPath(), &container.FileEntry{
|
2022-03-23 05:13:00 +08:00
|
|
|
Name: scriptName,
|
2023-02-16 23:34:51 +08:00
|
|
|
Mode: 0o755,
|
2022-03-23 05:13:00 +08:00
|
|
|
Body: script,
|
|
|
|
})(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getScriptName(rc *RunContext, step *model.Step) string {
|
|
|
|
scriptName := step.ID
|
|
|
|
for rcs := rc; rcs.Parent != nil; rcs = rcs.Parent {
|
|
|
|
scriptName = fmt.Sprintf("%s-composite-%s", rcs.Parent.CurrentStep, scriptName)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("workflow/%s", scriptName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Currently we just ignore top level keys, BUT we should return proper error on them
|
|
|
|
// BUTx2 I leave this for when we rewrite act to use actionlint for workflow validation
|
|
|
|
// so we return proper errors before any execution or spawning containers
|
|
|
|
// it will error anyway with:
|
|
|
|
// OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "${{": executable file not found in $PATH: unknown
|
2022-05-12 03:06:05 +08:00
|
|
|
func (sr *stepRun) setupShellCommand(ctx context.Context) (name, script string, err error) {
|
2022-06-17 23:55:21 +08:00
|
|
|
logger := common.Logger(ctx)
|
|
|
|
sr.setupShell(ctx)
|
|
|
|
sr.setupWorkingDirectory(ctx)
|
2022-03-23 05:13:00 +08:00
|
|
|
|
|
|
|
step := sr.Step
|
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
script = sr.RunContext.NewStepExpressionEvaluator(ctx, sr).Interpolate(ctx, step.Run)
|
2022-03-23 05:13:00 +08:00
|
|
|
|
|
|
|
scCmd := step.ShellCommand()
|
|
|
|
|
|
|
|
name = getScriptName(sr.RunContext, step)
|
|
|
|
|
|
|
|
// Reference: https://github.com/actions/runner/blob/8109c962f09d9acc473d92c595ff43afceddb347/src/Runner.Worker/Handlers/ScriptHandlerHelpers.cs#L47-L64
|
|
|
|
// Reference: https://github.com/actions/runner/blob/8109c962f09d9acc473d92c595ff43afceddb347/src/Runner.Worker/Handlers/ScriptHandlerHelpers.cs#L19-L27
|
|
|
|
runPrepend := ""
|
|
|
|
runAppend := ""
|
|
|
|
switch step.Shell {
|
|
|
|
case "bash", "sh":
|
|
|
|
name += ".sh"
|
|
|
|
case "pwsh", "powershell":
|
|
|
|
name += ".ps1"
|
|
|
|
runPrepend = "$ErrorActionPreference = 'stop'"
|
|
|
|
runAppend = "if ((Test-Path -LiteralPath variable:/LASTEXITCODE)) { exit $LASTEXITCODE }"
|
|
|
|
case "cmd":
|
|
|
|
name += ".cmd"
|
|
|
|
runPrepend = "@echo off"
|
|
|
|
case "python":
|
|
|
|
name += ".py"
|
|
|
|
}
|
|
|
|
|
|
|
|
script = fmt.Sprintf("%s\n%s\n%s", runPrepend, script, runAppend)
|
|
|
|
|
2022-05-12 03:06:05 +08:00
|
|
|
if !strings.Contains(script, "::add-mask::") && !sr.RunContext.Config.InsecureSecrets {
|
2022-06-17 23:55:21 +08:00
|
|
|
logger.Debugf("Wrote command \n%s\n to '%s'", script, name)
|
2022-05-12 03:06:05 +08:00
|
|
|
} else {
|
2022-06-17 23:55:21 +08:00
|
|
|
logger.Debugf("Wrote add-mask command to '%s'", name)
|
2022-05-12 03:06:05 +08:00
|
|
|
}
|
2022-03-23 05:13:00 +08:00
|
|
|
|
2022-11-17 05:29:45 +08:00
|
|
|
rc := sr.getRunContext()
|
|
|
|
scriptPath := fmt.Sprintf("%s/%s", rc.JobContainer.GetActPath(), name)
|
2022-03-23 05:13:00 +08:00
|
|
|
sr.cmd, err = shellquote.Split(strings.Replace(scCmd, `{0}`, scriptPath, 1))
|
|
|
|
|
|
|
|
return name, script, err
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
func (sr *stepRun) setupShell(ctx context.Context) {
|
2022-03-23 05:13:00 +08:00
|
|
|
rc := sr.RunContext
|
|
|
|
step := sr.Step
|
|
|
|
|
|
|
|
if step.Shell == "" {
|
|
|
|
step.Shell = rc.Run.Job().Defaults.Run.Shell
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
step.Shell = rc.NewExpressionEvaluator(ctx).Interpolate(ctx, step.Shell)
|
2022-03-23 05:13:00 +08:00
|
|
|
|
|
|
|
if step.Shell == "" {
|
|
|
|
step.Shell = rc.Run.Workflow.Defaults.Run.Shell
|
|
|
|
}
|
|
|
|
|
|
|
|
// current GitHub Runner behaviour is that default is `sh`,
|
|
|
|
// but if it's not container it validates with `which` command
|
|
|
|
// if `bash` is available, and provides `bash` if it is
|
|
|
|
// for now I'm going to leave below logic, will address it in different PR
|
|
|
|
// https://github.com/actions/runner/blob/9a829995e02d2db64efb939dc2f283002595d4d9/src/Runner.Worker/Handlers/ScriptHandler.cs#L87-L91
|
|
|
|
if rc.Run.Job().Container() != nil {
|
|
|
|
if rc.Run.Job().Container().Image != "" && step.Shell == "" {
|
|
|
|
step.Shell = "sh"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
func (sr *stepRun) setupWorkingDirectory(ctx context.Context) {
|
2022-03-23 05:13:00 +08:00
|
|
|
rc := sr.RunContext
|
|
|
|
step := sr.Step
|
2023-03-28 20:24:03 +08:00
|
|
|
workingdirectory := ""
|
2022-03-23 05:13:00 +08:00
|
|
|
|
|
|
|
if step.WorkingDirectory == "" {
|
2023-03-28 20:24:03 +08:00
|
|
|
workingdirectory = rc.Run.Job().Defaults.Run.WorkingDirectory
|
|
|
|
} else {
|
|
|
|
workingdirectory = step.WorkingDirectory
|
2022-03-23 05:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// jobs can receive context values, so we interpolate
|
2023-03-28 20:24:03 +08:00
|
|
|
workingdirectory = rc.NewExpressionEvaluator(ctx).Interpolate(ctx, workingdirectory)
|
2022-03-23 05:13:00 +08:00
|
|
|
|
|
|
|
// but top level keys in workflow file like `defaults` or `env` can't
|
2023-03-28 20:24:03 +08:00
|
|
|
if workingdirectory == "" {
|
|
|
|
workingdirectory = rc.Run.Workflow.Defaults.Run.WorkingDirectory
|
2022-03-23 05:13:00 +08:00
|
|
|
}
|
2023-03-28 20:24:03 +08:00
|
|
|
sr.WorkingDirectory = workingdirectory
|
2022-03-23 05:13:00 +08:00
|
|
|
}
|