Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow local actions outside the workspace #2108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions pkg/runner/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
model.ActionRunsUsingDocker,
model.ActionRunsUsingNode12,
model.ActionRunsUsingNode16,
model.ActionRunsUsingNode20,

Check warning on line 179 in pkg/runner/action.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/action.go#L179

Added line #L179 was not covered by tests
model.ActionRunsUsingComposite,
}, action.Runs.Using))
}
Expand Down Expand Up @@ -262,8 +262,8 @@
if localAction {
buildContext, err = rc.JobContainer.GetContainerArchive(ctx, contextDir+"/.")
if err != nil {
return err
}

Check warning on line 266 in pkg/runner/action.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/action.go#L265-L266

Added lines #L265 - L266 were not covered by tests
defer buildContext.Close()
}
prepImage = container.NewDockerBuildExecutor(container.NewDockerBuildExecutorInput{
Expand Down Expand Up @@ -363,8 +363,8 @@
binds, mounts := rc.GetBindsAndMounts()
networkMode := fmt.Sprintf("container:%s", rc.jobContainerName())
if rc.IsHostEnv(ctx) {
networkMode = "default"
}

Check warning on line 367 in pkg/runner/action.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/action.go#L366-L367

Added lines #L366 - L367 were not covered by tests
stepContainer := container.NewContainer(&container.NewContainerInput{
Cmd: cmd,
Entrypoint: entrypoint,
Expand Down Expand Up @@ -412,31 +412,23 @@
actionName := ""
containerActionDir := "."
if step.Type() != model.StepTypeUsesActionRemote {
actionName = getOsSafeRelativePath(actionDir, rc.Config.Workdir)
containerActionDir = rc.JobContainer.ToContainerPath(rc.Config.Workdir) + "/" + actionName
actionName = "./" + actionName
actionName = "./" + getOsSafeRelativePath(actionDir, rc.Config.Workdir)
containerActionDir = rc.JobContainer.ToContainerPath(actionDir)
} else if step.Type() == model.StepTypeUsesActionRemote {
actionName = getOsSafeRelativePath(actionDir, rc.ActionCacheDir())
containerActionDir = rc.JobContainer.GetActPath() + "/actions/" + actionName
}

if actionName == "" {
actionName = filepath.Base(actionDir)
if runtime.GOOS == "windows" {
actionName = strings.ReplaceAll(actionName, "\\", "/")
}
}
return actionName, containerActionDir
}

func getOsSafeRelativePath(s, prefix string) string {
actionName := strings.TrimPrefix(s, prefix)
if runtime.GOOS == "windows" {
actionName = strings.ReplaceAll(actionName, "\\", "/")
func getOsSafeRelativePath(s, basepath string) string {
if relpath, err := filepath.Rel(basepath, s); err == nil {
if runtime.GOOS == "windows" {
relpath = strings.ReplaceAll(relpath, "\\", "/")
}

Check warning on line 428 in pkg/runner/action.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/action.go#L427-L428

Added lines #L427 - L428 were not covered by tests
return relpath
}
actionName = strings.TrimPrefix(actionName, "/")

return actionName
return filepath.Base(s)

Check warning on line 431 in pkg/runner/action.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/action.go#L431

Added line #L431 was not covered by tests
}

func shouldRunPreStep(step actionStep) common.Conditional {
Expand Down
7 changes: 5 additions & 2 deletions pkg/runner/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"context"
"fmt"
"io"
"io/fs"
"strings"
Expand Down Expand Up @@ -224,8 +225,10 @@ func TestActionRunner(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()

actionDir := fmt.Sprintf("%s/dir", tt.step.getRunContext().ActionCacheDir())

cm := &containerMock{}
cm.On("CopyDir", "/var/run/act/actions/dir/", "dir/", false).Return(func(ctx context.Context) error { return nil })
cm.On("CopyDir", "/var/run/act/actions/dir/", actionDir+"/", false).Return(func(ctx context.Context) error { return nil })

envMatcher := mock.MatchedBy(func(env map[string]string) bool {
for k, v := range tt.expectedEnv {
Expand All @@ -240,7 +243,7 @@ func TestActionRunner(t *testing.T) {

tt.step.getRunContext().JobContainer = cm

err := runActionImpl(tt.step, "dir", newRemoteAction("org/repo/path@ref"))(ctx)
err := runActionImpl(tt.step, actionDir, newRemoteAction("org/repo/path@ref"))(ctx)

assert.Nil(t, err)
cm.AssertExpectations(t)
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 @@ -231,6 +231,7 @@ func TestRunEvent(t *testing.T) {
{workdir, "local-action-dockerfile", "push", "", platforms, secrets},
{workdir, "local-action-via-composite-dockerfile", "push", "", platforms, secrets},
{workdir, "local-action-js", "push", "", platforms, secrets},
{workdir, "local-action-outside-workspace", "push", "", platforms, secrets},

// Uses
{workdir, "uses-composite", "push", "", platforms, secrets},
Expand Down
17 changes: 17 additions & 0 deletions pkg/runner/testdata/local-action-outside-workspace/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: local-action-outside-workspace
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: mkdir ../action-outside-workspace
- run: |
cat <<-ACTIONEOF > ../action-outside-workspace/action.yml
name: test
runs:
using: composite
steps:
- run: echo hello
shell: bash
ACTIONEOF
- uses: ./../action-outside-workspace
Loading