2020-02-23 17:02:01 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-02-23 18:36:44 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-02-23 17:02:01 -06:00
|
|
|
"regexp"
|
2020-02-24 12:56:49 -06:00
|
|
|
"runtime"
|
2020-02-23 17:02:01 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
"github.com/nektos/act/pkg/container"
|
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepContext contains info about current job
|
|
|
|
type StepContext struct {
|
|
|
|
RunContext *RunContext
|
|
|
|
Step *model.Step
|
|
|
|
Env map[string]string
|
|
|
|
Cmd []string
|
2020-02-23 18:36:44 -06:00
|
|
|
Action *model.Action
|
2020-02-23 17:02:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *StepContext) execJobContainer() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
return sc.RunContext.execJobContainer(sc.Cmd, sc.Env)(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Executor for a step context
|
|
|
|
func (sc *StepContext) Executor() common.Executor {
|
|
|
|
rc := sc.RunContext
|
|
|
|
step := sc.Step
|
|
|
|
|
|
|
|
switch step.Type() {
|
|
|
|
case model.StepTypeRun:
|
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
sc.setupEnv(),
|
|
|
|
sc.setupShellCommand(),
|
|
|
|
sc.execJobContainer(),
|
|
|
|
)
|
|
|
|
|
|
|
|
case model.StepTypeUsesDockerURL:
|
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
sc.setupEnv(),
|
|
|
|
sc.runUsesContainer(),
|
|
|
|
)
|
|
|
|
|
2020-02-23 18:36:44 -06:00
|
|
|
case model.StepTypeUsesActionLocal:
|
2020-02-24 00:34:48 -06:00
|
|
|
actionDir := filepath.Join(rc.Config.Workdir, step.Uses)
|
2020-02-23 18:36:44 -06:00
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
sc.setupEnv(),
|
2020-02-24 00:34:48 -06:00
|
|
|
sc.setupAction(actionDir),
|
|
|
|
sc.runAction(actionDir),
|
|
|
|
)
|
|
|
|
case model.StepTypeUsesActionRemote:
|
|
|
|
remoteAction := newRemoteAction(step.Uses)
|
|
|
|
if remoteAction.Org == "actions" && remoteAction.Repo == "checkout" {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
common.Logger(ctx).Debugf("Skipping actions/checkout")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 18:38:49 -06:00
|
|
|
actionDir := fmt.Sprintf("%s/%s", rc.ActionCacheDir(), strings.ReplaceAll(step.Uses, "/", "-"))
|
2020-02-24 00:34:48 -06:00
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
common.NewGitCloneExecutor(common.NewGitCloneExecutorInput{
|
|
|
|
URL: remoteAction.CloneURL(),
|
|
|
|
Ref: remoteAction.Ref,
|
|
|
|
Dir: actionDir,
|
|
|
|
}),
|
|
|
|
sc.setupEnv(),
|
|
|
|
sc.setupAction(actionDir),
|
|
|
|
sc.runAction(actionDir),
|
2020-02-23 18:36:44 -06:00
|
|
|
)
|
2020-02-23 17:02:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return common.NewErrorExecutor(fmt.Errorf("Unable to determine how to run job:%s step:%+v", rc.Run, step))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *StepContext) setupEnv() common.Executor {
|
|
|
|
rc := sc.RunContext
|
|
|
|
job := rc.Run.Job()
|
|
|
|
step := sc.Step
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
var env map[string]string
|
2020-02-25 00:35:08 -06:00
|
|
|
c := job.Container()
|
|
|
|
if c != nil {
|
|
|
|
env = mergeMaps(rc.GetEnv(), c.Env, step.GetEnv())
|
2020-02-23 17:02:01 -06:00
|
|
|
} else {
|
|
|
|
env = mergeMaps(rc.GetEnv(), step.GetEnv())
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range env {
|
|
|
|
env[k] = rc.ExprEval.Interpolate(v)
|
|
|
|
}
|
|
|
|
sc.Env = rc.withGithubEnv(env)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *StepContext) setupShellCommand() common.Executor {
|
|
|
|
rc := sc.RunContext
|
|
|
|
step := sc.Step
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
var script strings.Builder
|
|
|
|
|
|
|
|
_, err := script.WriteString(fmt.Sprintf("PATH=\"%s:${PATH}\"\n", strings.Join(rc.ExtraPath, ":")))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
run := rc.ExprEval.Interpolate(step.Run)
|
|
|
|
|
|
|
|
if _, err = script.WriteString(run); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scriptName := fmt.Sprintf("workflow/%s", step.ID)
|
|
|
|
log.Debugf("Wrote command '%s' to '%s'", run, scriptName)
|
|
|
|
containerPath := fmt.Sprintf("/github/%s", scriptName)
|
|
|
|
sc.Cmd = strings.Fields(strings.Replace(step.ShellCommand(), "{0}", containerPath, 1))
|
|
|
|
return rc.JobContainer.Copy("/github/", &container.FileEntry{
|
|
|
|
Name: scriptName,
|
|
|
|
Mode: 755,
|
|
|
|
Body: script.String(),
|
|
|
|
})(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *StepContext) newStepContainer(ctx context.Context, image string, cmd []string, entrypoint []string) container.Container {
|
|
|
|
rc := sc.RunContext
|
|
|
|
step := sc.Step
|
|
|
|
rawLogger := common.Logger(ctx).WithField("raw_output", true)
|
2020-02-24 14:48:12 -06:00
|
|
|
logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool {
|
2020-02-23 17:02:01 -06:00
|
|
|
if rc.Config.LogOutput {
|
|
|
|
rawLogger.Infof(s)
|
|
|
|
} else {
|
|
|
|
rawLogger.Debugf(s)
|
|
|
|
}
|
2020-02-24 14:48:12 -06:00
|
|
|
return true
|
2020-02-23 17:02:01 -06:00
|
|
|
})
|
|
|
|
envList := make([]string, 0)
|
|
|
|
for k, v := range sc.Env {
|
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
2020-02-24 00:34:48 -06:00
|
|
|
stepEE := sc.NewExpressionEvaluator()
|
|
|
|
for i, v := range cmd {
|
|
|
|
cmd[i] = stepEE.Interpolate(v)
|
|
|
|
}
|
|
|
|
for i, v := range entrypoint {
|
|
|
|
entrypoint[i] = stepEE.Interpolate(v)
|
|
|
|
}
|
2020-02-24 12:56:49 -06:00
|
|
|
|
|
|
|
bindModifiers := ""
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
bindModifiers = ":delegated"
|
|
|
|
}
|
|
|
|
|
2020-02-24 14:48:12 -06:00
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", "/toolcache"))
|
2020-02-24 12:56:49 -06:00
|
|
|
|
2020-02-24 19:48:21 -06:00
|
|
|
binds := []string{
|
|
|
|
fmt.Sprintf("%s:%s", "/var/run/docker.sock", "/var/run/docker.sock"),
|
|
|
|
}
|
|
|
|
if rc.Config.BindWorkdir {
|
|
|
|
binds = append(binds, fmt.Sprintf("%s:%s%s", rc.Config.Workdir, "/github/workspace", bindModifiers))
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:02:01 -06:00
|
|
|
stepContainer := container.NewContainer(&container.NewContainerInput{
|
|
|
|
Cmd: cmd,
|
|
|
|
Entrypoint: entrypoint,
|
|
|
|
WorkingDir: "/github/workspace",
|
|
|
|
Image: image,
|
|
|
|
Name: createContainerName(rc.jobContainerName(), step.ID),
|
|
|
|
Env: envList,
|
|
|
|
Mounts: map[string]string{
|
|
|
|
rc.jobContainerName(): "/github",
|
2020-02-24 14:48:12 -06:00
|
|
|
"act-toolcache": "/toolcache",
|
2020-02-24 18:38:49 -06:00
|
|
|
"act-actions": "/actions",
|
2020-02-23 17:02:01 -06:00
|
|
|
},
|
2020-02-24 19:48:21 -06:00
|
|
|
Binds: binds,
|
2020-02-23 17:02:01 -06:00
|
|
|
Stdout: logWriter,
|
|
|
|
Stderr: logWriter,
|
|
|
|
})
|
|
|
|
return stepContainer
|
|
|
|
}
|
|
|
|
func (sc *StepContext) runUsesContainer() common.Executor {
|
|
|
|
rc := sc.RunContext
|
|
|
|
step := sc.Step
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
image := strings.TrimPrefix(step.Uses, "docker://")
|
2020-02-24 00:34:48 -06:00
|
|
|
cmd := strings.Fields(step.With["args"])
|
|
|
|
entrypoint := strings.Fields(step.With["entrypoint"])
|
2020-02-23 17:02:01 -06:00
|
|
|
stepContainer := sc.newStepContainer(ctx, image, cmd, entrypoint)
|
|
|
|
|
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
stepContainer.Pull(rc.Config.ForcePull),
|
|
|
|
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers),
|
|
|
|
stepContainer.Create(),
|
|
|
|
stepContainer.Start(true),
|
|
|
|
).Finally(
|
|
|
|
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers),
|
|
|
|
)(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 00:34:48 -06:00
|
|
|
func (sc *StepContext) setupAction(actionDir string) common.Executor {
|
2020-02-23 17:02:01 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
f, err := os.Open(filepath.Join(actionDir, "action.yml"))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
f, err = os.Open(filepath.Join(actionDir, "action.yaml"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-23 18:36:44 -06:00
|
|
|
sc.Action, err = model.ReadAction(f)
|
|
|
|
log.Debugf("Read action %v from '%s'", sc.Action, f.Name())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 17:02:01 -06:00
|
|
|
|
2020-02-24 00:34:48 -06:00
|
|
|
func (sc *StepContext) runAction(actionDir string) common.Executor {
|
2020-02-23 18:36:44 -06:00
|
|
|
rc := sc.RunContext
|
|
|
|
step := sc.Step
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
action := sc.Action
|
|
|
|
log.Debugf("About to run action %v", action)
|
2020-02-23 17:02:01 -06:00
|
|
|
for inputID, input := range action.Inputs {
|
|
|
|
envKey := regexp.MustCompile("[^A-Z0-9-]").ReplaceAllString(strings.ToUpper(inputID), "_")
|
|
|
|
envKey = fmt.Sprintf("INPUT_%s", envKey)
|
2020-02-23 18:36:44 -06:00
|
|
|
if _, ok := sc.Env[envKey]; !ok {
|
|
|
|
sc.Env[envKey] = input.Default
|
2020-02-23 17:02:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 12:56:49 -06:00
|
|
|
actionName := ""
|
|
|
|
containerActionDir := "."
|
2020-02-24 18:38:49 -06:00
|
|
|
if step.Type() == model.StepTypeUsesActionLocal {
|
|
|
|
actionName = strings.TrimPrefix(strings.TrimPrefix(actionDir, rc.Config.Workdir), string(filepath.Separator))
|
2020-02-24 12:56:49 -06:00
|
|
|
containerActionDir = "/github/workspace"
|
2020-02-24 18:38:49 -06:00
|
|
|
} else if step.Type() == model.StepTypeUsesActionRemote {
|
|
|
|
actionName = strings.TrimPrefix(strings.TrimPrefix(actionDir, rc.ActionCacheDir()), string(filepath.Separator))
|
|
|
|
containerActionDir = "/actions"
|
2020-02-24 12:56:49 -06:00
|
|
|
}
|
|
|
|
|
2020-02-24 18:38:49 -06:00
|
|
|
log.Debugf("actionDir=%s Workdir=%s ActionCacheDir=%s actionName=%s containerActionDir=%s", actionDir, rc.Config.Workdir, rc.ActionCacheDir(), actionName, containerActionDir)
|
|
|
|
|
2020-02-23 17:02:01 -06:00
|
|
|
switch action.Runs.Using {
|
|
|
|
case model.ActionRunsUsingNode12:
|
2020-02-24 18:38:49 -06:00
|
|
|
if step.Type() == model.StepTypeUsesActionRemote {
|
|
|
|
err := rc.JobContainer.CopyDir(containerActionDir+string(filepath.Separator), actionDir)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 12:56:49 -06:00
|
|
|
return rc.execJobContainer([]string{"node", fmt.Sprintf("%s/%s/%s", containerActionDir, actionName, action.Runs.Main)}, sc.Env)(ctx)
|
2020-02-23 17:02:01 -06:00
|
|
|
case model.ActionRunsUsingDocker:
|
2020-02-23 18:36:44 -06:00
|
|
|
var prepImage common.Executor
|
|
|
|
var image string
|
2020-02-23 17:02:01 -06:00
|
|
|
if strings.HasPrefix(action.Runs.Image, "docker://") {
|
2020-02-23 18:36:44 -06:00
|
|
|
image = strings.TrimPrefix(action.Runs.Image, "docker://")
|
2020-02-23 17:02:01 -06:00
|
|
|
} else {
|
2020-02-24 12:56:49 -06:00
|
|
|
image = fmt.Sprintf("%s:%s", regexp.MustCompile("[^a-zA-Z0-9]").ReplaceAllString(actionName, "-"), "latest")
|
|
|
|
image = fmt.Sprintf("act-%s", strings.TrimLeft(image, "-"))
|
2020-02-24 00:34:48 -06:00
|
|
|
contextDir := filepath.Join(actionDir, action.Runs.Main)
|
2020-02-23 18:36:44 -06:00
|
|
|
prepImage = container.NewDockerBuildExecutor(container.NewDockerBuildExecutorInput{
|
2020-02-23 17:02:01 -06:00
|
|
|
ContextDir: contextDir,
|
2020-02-23 18:36:44 -06:00
|
|
|
ImageTag: image,
|
|
|
|
})
|
2020-02-23 17:02:01 -06:00
|
|
|
}
|
2020-02-23 18:36:44 -06:00
|
|
|
|
|
|
|
cmd := strings.Fields(step.With["args"])
|
|
|
|
if len(cmd) == 0 {
|
|
|
|
cmd = action.Runs.Args
|
|
|
|
}
|
|
|
|
entrypoint := strings.Fields(step.With["entrypoint"])
|
|
|
|
if len(entrypoint) == 0 {
|
|
|
|
entrypoint = action.Runs.Entrypoint
|
|
|
|
}
|
|
|
|
stepContainer := sc.newStepContainer(ctx, image, cmd, entrypoint)
|
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
prepImage,
|
|
|
|
stepContainer.Pull(rc.Config.ForcePull),
|
|
|
|
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers),
|
|
|
|
stepContainer.Create(),
|
|
|
|
stepContainer.Start(true),
|
|
|
|
).Finally(
|
|
|
|
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers),
|
|
|
|
)(ctx)
|
2020-02-23 17:02:01 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteAction struct {
|
|
|
|
Org string
|
|
|
|
Repo string
|
|
|
|
Path string
|
|
|
|
Ref string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ra *remoteAction) CloneURL() string {
|
|
|
|
return fmt.Sprintf("https://github.com/%s/%s", ra.Org, ra.Repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRemoteAction(action string) *remoteAction {
|
|
|
|
r := regexp.MustCompile(`^([^/@]+)/([^/@]+)(/([^@]*))?(@(.*))?$`)
|
|
|
|
matches := r.FindStringSubmatch(action)
|
|
|
|
|
|
|
|
ra := new(remoteAction)
|
|
|
|
ra.Org = matches[1]
|
|
|
|
ra.Repo = matches[2]
|
|
|
|
ra.Path = ""
|
|
|
|
ra.Ref = "master"
|
|
|
|
if len(matches) >= 5 {
|
|
|
|
ra.Path = matches[4]
|
|
|
|
}
|
|
|
|
if len(matches) >= 7 {
|
|
|
|
ra.Ref = matches[6]
|
|
|
|
}
|
|
|
|
return ra
|
|
|
|
}
|