From 6c632946beff4facd3c50a7a09a718d063ddf369 Mon Sep 17 00:00:00 2001 From: Casey Lee Date: Mon, 24 Feb 2020 10:56:49 -0800 Subject: [PATCH] unit tests pass --- .github/workflows/push.yml | 2 +- pkg/runner/run_context.go | 30 +++++++++++++++++++++---- pkg/runner/runner_test.go | 6 ++++- pkg/runner/step_context.go | 45 ++++++++++++++++++++++++++++---------- 4 files changed, 66 insertions(+), 17 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index d1937db1..87d51e09 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -7,4 +7,4 @@ jobs: steps: - uses: actions/checkout@v2 - uses: ./.github/workflows/check - #- uses: ./.github/workflows/integration + - uses: ./.github/workflows/integration diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index 10fe85cd..cd302cb9 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "regexp" + "runtime" "strings" "github.com/nektos/act/pkg/container" @@ -72,18 +73,38 @@ func (rc *RunContext) startJobContainer() common.Executor { common.Logger(ctx).Infof("\U0001f680 Start image=%s", image) name := rc.jobContainerName() + envList := make([]string, 0) + bindModifiers := "" + if runtime.GOOS == "darwin" { + bindModifiers = ":delegated" + } + + hostWorkdir := os.Getenv("ACT_HOST_WORKDIR") + if hostWorkdir == "" { + hostWorkdir = rc.Config.Workdir + } + envList = append(envList, fmt.Sprintf("%s=%s", "ACT_HOST_WORKDIR", hostWorkdir)) + + hostActionCache := os.Getenv("ACT_HOST_ACTIONCACHE") + if hostActionCache == "" { + hostActionCache = rc.ActionCacheDir() + } + envList = append(envList, fmt.Sprintf("%s=%s", "ACT_HOST_ACTIONCACHE", hostActionCache)) + rc.JobContainer = container.NewContainer(&container.NewContainerInput{ Cmd: nil, Entrypoint: []string{"/usr/bin/tail", "-f", "/dev/null"}, WorkingDir: "/github/workspace", Image: image, Name: name, + Env: envList, Mounts: map[string]string{ name: "/github", }, + Binds: []string{ - fmt.Sprintf("%s:%s", rc.Config.Workdir, "/github/workspace"), - fmt.Sprintf("%s:%s", rc.ActionDir(), "/github/home/.act"), + fmt.Sprintf("%s:%s%s", hostWorkdir, "/github/workspace", bindModifiers), + fmt.Sprintf("%s:%s%s", hostActionCache, "/github/home/.cache/act", bindModifiers), fmt.Sprintf("%s:%s", "/var/run/docker.sock", "/var/run/docker.sock"), }, Stdout: logWriter, @@ -118,8 +139,8 @@ func (rc *RunContext) stopJobContainer() common.Executor { } } -// ActionDir is for rc -func (rc *RunContext) ActionDir() string { +// ActionCacheDir is for rc +func (rc *RunContext) ActionCacheDir() string { var xdgCache string var ok bool if xdgCache, ok = os.LookupEnv("XDG_CACHE_HOME"); !ok { @@ -336,6 +357,7 @@ func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string { env["GITHUB_RUN_ID"] = github.RunID env["GITHUB_RUN_NUMBER"] = github.RunNumber env["GITHUB_ACTION"] = github.Action + env["GITHUB_ACTIONS"] = "true" env["GITHUB_ACTOR"] = github.Actor env["GITHUB_REPOSITORY"] = github.Repository env["GITHUB_EVENT_NAME"] = github.EventName diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index e4fb880f..3be32aec 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -3,6 +3,7 @@ package runner import ( "context" "fmt" + "path/filepath" "testing" "github.com/nektos/act/pkg/model" @@ -59,8 +60,11 @@ func TestRunEvent(t *testing.T) { platforms := map[string]string{ "ubuntu-latest": "node:12.6-buster-slim", } + + workdir, err := filepath.Abs("testdata") + assert.NilError(t, err, table.workflowPath) runnerConfig := &Config{ - Workdir: "testdata", + Workdir: workdir, EventName: table.eventName, Platforms: platforms, ReuseContainers: false, diff --git a/pkg/runner/step_context.go b/pkg/runner/step_context.go index 9600afde..479998f4 100644 --- a/pkg/runner/step_context.go +++ b/pkg/runner/step_context.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "regexp" + "runtime" "strings" "github.com/nektos/act/pkg/common" @@ -64,7 +65,7 @@ func (sc *StepContext) Executor() common.Executor { } } - actionDir := rc.ActionDir() + actionDir := fmt.Sprintf("%s/%s", rc.ActionCacheDir(), remoteAction.Repo) return common.NewPipelineExecutor( common.NewGitCloneExecutor(common.NewGitCloneExecutorInput{ URL: remoteAction.CloneURL(), @@ -150,6 +151,24 @@ func (sc *StepContext) newStepContainer(ctx context.Context, image string, cmd [ for i, v := range entrypoint { entrypoint[i] = stepEE.Interpolate(v) } + + bindModifiers := "" + if runtime.GOOS == "darwin" { + bindModifiers = ":delegated" + } + + hostWorkdir := os.Getenv("ACT_HOST_WORKDIR") + if hostWorkdir == "" { + hostWorkdir = rc.Config.Workdir + } + envList = append(envList, fmt.Sprintf("%s=%s", "ACT_HOST_WORKDIR", hostWorkdir)) + + hostActionCache := os.Getenv("ACT_HOST_ACTIONCACHE") + if hostActionCache == "" { + hostActionCache = rc.ActionCacheDir() + } + envList = append(envList, fmt.Sprintf("%s=%s", "ACT_HOST_ACTIONCACHE", hostActionCache)) + stepContainer := container.NewContainer(&container.NewContainerInput{ Cmd: cmd, Entrypoint: entrypoint, @@ -161,7 +180,7 @@ func (sc *StepContext) newStepContainer(ctx context.Context, image string, cmd [ rc.jobContainerName(): "/github", }, Binds: []string{ - fmt.Sprintf("%s:%s", rc.Config.Workdir, "/github/workspace"), + fmt.Sprintf("%s:%s%s", hostWorkdir, "/github/workspace", bindModifiers), fmt.Sprintf("%s:%s", "/var/run/docker.sock", "/var/run/docker.sock"), }, Stdout: logWriter, @@ -221,23 +240,27 @@ func (sc *StepContext) runAction(actionDir string) common.Executor { } } + actionName := "" + containerActionDir := "." + if strings.HasPrefix(actionDir, rc.Config.Workdir) { + actionName = strings.TrimPrefix(actionDir, rc.Config.Workdir) + containerActionDir = "/github/workspace" + } else if strings.HasPrefix(actionDir, rc.ActionCacheDir()) { + actionName = strings.TrimPrefix(actionDir, rc.ActionCacheDir()) + containerActionDir = "/github/home/.cache/act" + } + switch action.Runs.Using { case model.ActionRunsUsingNode12: - basePath := "." - if strings.HasPrefix(actionDir, rc.Config.Workdir) { - basePath = fmt.Sprintf("/github/workspace/%s", strings.TrimPrefix(actionDir, rc.Config.Workdir)) - } else if strings.HasPrefix(actionDir, rc.ActionDir()) { - basePath = fmt.Sprintf("/github/home/.act/%s", strings.TrimPrefix(actionDir, rc.ActionDir())) - } - return rc.execJobContainer([]string{"node", fmt.Sprintf("%s/%s", basePath, action.Runs.Main)}, sc.Env)(ctx) + return rc.execJobContainer([]string{"node", fmt.Sprintf("%s/%s/%s", containerActionDir, actionName, action.Runs.Main)}, sc.Env)(ctx) case model.ActionRunsUsingDocker: var prepImage common.Executor var image string if strings.HasPrefix(action.Runs.Image, "docker://") { image = strings.TrimPrefix(action.Runs.Image, "docker://") } else { - image = fmt.Sprintf("%s:%s", regexp.MustCompile("[^a-zA-Z0-9]").ReplaceAllString(step.Uses, "-"), "latest") - image = strings.TrimLeft(image, "-") + image = fmt.Sprintf("%s:%s", regexp.MustCompile("[^a-zA-Z0-9]").ReplaceAllString(actionName, "-"), "latest") + image = fmt.Sprintf("act-%s", strings.TrimLeft(image, "-")) contextDir := filepath.Join(actionDir, action.Runs.Main) prepImage = container.NewDockerBuildExecutor(container.NewDockerBuildExecutorInput{ ContextDir: contextDir,