1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2025-10-30 23:27:39 +02:00

Make local backend work with cli exec (#4102)

This commit is contained in:
6543
2025-10-27 13:12:26 +01:00
committed by GitHub
parent e4e208d81a
commit 393a598212
2 changed files with 38 additions and 14 deletions

View File

@@ -36,9 +36,9 @@ import (
"go.woodpecker-ci.org/woodpecker/v3/cli/lint"
"go.woodpecker-ci.org/woodpecker/v3/pipeline"
"go.woodpecker-ci.org/woodpecker/v3/pipeline/backend"
"go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/docker"
"go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/kubernetes"
"go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/local"
backend_docker "go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/docker"
backend_kubernetes "go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/kubernetes"
backend_local "go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/local"
backend_types "go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/types"
"go.woodpecker-ci.org/woodpecker/v3/pipeline/frontend/metadata"
"go.woodpecker-ci.org/woodpecker/v3/pipeline/frontend/yaml"
@@ -56,13 +56,13 @@ var Command = &cli.Command{
Usage: "execute a local pipeline",
ArgsUsage: "[path/to/.woodpecker.yaml]",
Action: run,
Flags: utils.MergeSlices(flags, docker.Flags, kubernetes.Flags, local.Flags),
Flags: utils.MergeSlices(flags, backend_docker.Flags, backend_kubernetes.Flags, backend_local.Flags),
}
var backends = []backend_types.Backend{
kubernetes.New(),
docker.New(),
local.New(),
backend_kubernetes.New(),
backend_docker.New(),
backend_local.New(),
}
func run(ctx context.Context, c *cli.Command) error {
@@ -77,7 +77,7 @@ func execDir(ctx context.Context, c *cli.Command, dir string) error {
} else {
repoPath, _ = filepath.Abs(filepath.Dir(dir))
}
if runtime.GOOS == "windows" {
if runtime.GOOS == "windows" && c.String("backend-engine") != "local" {
repoPath = convertPathForWindows(repoPath)
}
@@ -118,7 +118,7 @@ func execFile(ctx context.Context, c *cli.Command, file string) error {
} else {
repoPath, _ = filepath.Abs(filepath.Dir(file))
}
if runtime.GOOS == "windows" {
if runtime.GOOS == "windows" && c.String("backend-engine") != "local" {
repoPath = convertPathForWindows(repoPath)
}
return runExec(ctx, c, file, repoPath, true)
@@ -130,6 +130,11 @@ func runExec(ctx context.Context, c *cli.Command, file, repoPath string, singleE
return err
}
// if we use the local backend we should signal to run at $repoPath
if c.String("backend-engine") == "local" {
backend_local.CLIWorkaroundExecAtDir = repoPath
}
axes, err := matrix.ParseString(string(dat))
if err != nil {
return fmt.Errorf("parse matrix fail")

View File

@@ -52,6 +52,8 @@ type local struct {
os, arch string
}
var CLIWorkaroundExecAtDir string // To handle edge case for running local backend via cli exec
// New returns a new local Backend.
func New() types.Backend {
return &local{
@@ -100,17 +102,34 @@ func (e *local) SetupWorkflow(_ context.Context, _ *types.Config, taskUUID strin
}
state := &workflowState{
baseDir: baseDir,
workspaceDir: filepath.Join(baseDir, "workspace"),
homeDir: filepath.Join(baseDir, "home"),
baseDir: baseDir,
homeDir: filepath.Join(baseDir, "home"),
}
e.workflows.Store(taskUUID, state)
if err := os.Mkdir(state.homeDir, 0o700); err != nil {
return err
}
if err := os.Mkdir(state.workspaceDir, 0o700); err != nil {
return err
// normal workspace setup case
if CLIWorkaroundExecAtDir == "" {
state.workspaceDir = filepath.Join(baseDir, "workspace")
if err := os.Mkdir(state.workspaceDir, 0o700); err != nil {
return err
}
} else
// setup workspace via internal flag signaled from cli exec to a specific dir
{
state.workspaceDir = CLIWorkaroundExecAtDir
if stat, err := os.Stat(CLIWorkaroundExecAtDir); os.IsNotExist(err) {
log.Debug().Msgf("create workspace directory '%s' set by internal flag", CLIWorkaroundExecAtDir)
if err := os.Mkdir(state.workspaceDir, 0o700); err != nil {
return err
}
} else if !stat.IsDir() {
//nolint:forbidigo
log.Fatal().Msg("This should never happen! internalExecDir was set to an non directory path!")
}
}
e.workflows.Store(taskUUID, state)