Skip to content

Commit

Permalink
Allow local actions outside the workspace
Browse files Browse the repository at this point in the history
Also simplify actionName logic and ensure it returns sensible values for actions
outside the workspace (it's only used for logging and docker image name)
  • Loading branch information
jenseng committed Jul 7, 2024
1 parent aa54ea9 commit 94fbf74
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
26 changes: 9 additions & 17 deletions pkg/runner/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,31 +445,23 @@ func getContainerActionPaths(step *model.Step, actionDir string, rc *RunContext)
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 460 in pkg/runner/action.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/action.go#L460

Added line #L460 was not covered by tests
}
return relpath
}
actionName = strings.TrimPrefix(actionName, "/")

return actionName
return filepath.Base(s)

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/action.go#L464

Added line #L464 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 })

Check failure on line 231 in pkg/runner/action_test.go

View workflow job for this annotation

GitHub Actions / lint

cm.On undefined (type *containerMock has no field or method On) (typecheck)

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 @@ -238,6 +238,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

0 comments on commit 94fbf74

Please sign in to comment.