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

Make local backend work with cli exec #4102

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
7 changes: 7 additions & 0 deletions cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@
return err
}

// if we use the local backend we should signal to run at $repoPath
if c.String("backend-engine") == "local" && !c.IsSet("backend-local-exec-dir") {
if err := c.Set("backend-local-exec-dir", repoPath); err != nil {
return err
}

Check warning on line 118 in cli/exec/exec.go

View check run for this annotation

Codecov / codecov/patch

cli/exec/exec.go#L115-L118

Added lines #L115 - L118 were not covered by tests
}

axes, err := matrix.ParseString(string(dat))
if err != nil {
return fmt.Errorf("parse matrix fail")
Expand Down
8 changes: 8 additions & 0 deletions docs/docs/30-administration/22-backends/20-local.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,11 @@ In the context of the local backend, plugins are simply executable binaries, whi
> Default: default temp directory

Directory to create folders for workflows.

#### `WOODPECKER_BACKEND_LOCAL_EXEC_DIR`
6543 marked this conversation as resolved.
Show resolved Hide resolved

> Default: _empty_

:::warning
Only set it if you know why
:::
7 changes: 6 additions & 1 deletion pipeline/backend/local/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ var Flags = []cli.Flag{
&cli.StringFlag{
Name: "backend-local-temp-dir",
Sources: cli.EnvVars("WOODPECKER_BACKEND_LOCAL_TEMP_DIR"),
Usage: "set a different temp dir to clone workflows into",
Usage: "Set a different temp dir to clone workflows into.",
Value: os.TempDir(),
},
&cli.StringFlag{
Name: "backend-local-exec-dir",
Sources: cli.EnvVars("WOODPECKER_BACKEND_LOCAL_EXEC_DIR"),
Usage: "Instead of using an tmp dir, we use this directory to exec the workflow in (mostly used by 'woodpecker-cli exec ...').",
},
}
29 changes: 23 additions & 6 deletions pipeline/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

type local struct {
tempDir string
execDir string
workflows sync.Map
output io.ReadCloser
pluginGitBinary string
Expand Down Expand Up @@ -80,6 +81,7 @@
c, ok := ctx.Value(types.CliCommand).(*cli.Command)
if ok {
e.tempDir = c.String("backend-local-temp-dir")
e.execDir = c.String("backend-local-exec-dir")

Check warning on line 84 in pipeline/backend/local/local.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/local/local.go#L84

Added line #L84 was not covered by tests
}

e.loadClone()
Expand All @@ -99,18 +101,33 @@
}

state := &workflowState{
stepCMDs: make(map[string]*exec.Cmd),
baseDir: baseDir,
workspaceDir: filepath.Join(baseDir, "workspace"),
homeDir: filepath.Join(baseDir, "home"),
stepCMDs: make(map[string]*exec.Cmd),
baseDir: baseDir,
homeDir: filepath.Join(baseDir, "home"),

Check warning on line 106 in pipeline/backend/local/local.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/local/local.go#L104-L106

Added lines #L104 - L106 were not covered by tests
}
e.saveState(taskUUID, state)

Check warning on line 108 in pipeline/backend/local/local.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/local/local.go#L108

Added line #L108 was not covered by tests

if err := os.Mkdir(state.homeDir, 0o700); err != nil {
return err
}

if err := os.Mkdir(state.workspaceDir, 0o700); err != nil {
return err
if e.execDir == "" {
state.workspaceDir = filepath.Join(baseDir, "workspace")
if err := os.Mkdir(state.workspaceDir, 0o700); err != nil {
return err
}
} else {
state.workspaceDir = e.execDir
if stat, err := os.Stat(e.execDir); os.IsNotExist(err) {
log.Debug().Msgf("create workspace directory set by WOODPECKER_BACKEND_LOCAL_EXEC_DIR")
if err := os.Mkdir(state.workspaceDir, 0o700); err != nil {
return err
}
} else if !stat.IsDir() {
err := fmt.Errorf("backend option 'WOODPECKER_BACKEND_LOCAL_EXEC_DIR' was set to an existing file")
log.Error().Err(err).Msg("")
return err
}

Check warning on line 130 in pipeline/backend/local/local.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/local/local.go#L114-L130

Added lines #L114 - L130 were not covered by tests
}

e.saveState(taskUUID, state)
Expand Down