diff --git a/docs/docs/30-administration/15-agent-config.md b/docs/docs/30-administration/15-agent-config.md index d9088e3aa..e34348b04 100644 --- a/docs/docs/30-administration/15-agent-config.md +++ b/docs/docs/30-administration/15-agent-config.md @@ -188,6 +188,10 @@ See [Docker backend configuration](./22-backends/10-docker.md#configuration) See [Kubernetes backend configuration](./22-backends/40-kubernetes.md#configuration) +### `WOODPECKER_BACKEND_LOCAL_*` + +See [Local backend configuration](./22-backends/20-local.md#further-configuration) + ## Advanced Settings :::warning diff --git a/docs/docs/30-administration/22-backends/20-local.md b/docs/docs/30-administration/22-backends/20-local.md index 90202f1ef..9b096672a 100644 --- a/docs/docs/30-administration/22-backends/20-local.md +++ b/docs/docs/30-administration/22-backends/20-local.md @@ -123,3 +123,12 @@ labels: steps: [...] ``` + +### Change temp directory + +We use the default temp directory to create folders for workflows. +This directory can be changed by: + +```env +WOODPECKER_BACKEND_LOCAL_TEMP_DIR=/some/other/dir +``` diff --git a/pipeline/backend/local/command.go b/pipeline/backend/local/command.go index cfa36ad54..2f6715ca2 100644 --- a/pipeline/backend/local/command.go +++ b/pipeline/backend/local/command.go @@ -22,7 +22,7 @@ import ( "github.com/alessio/shellescape" ) -func genCmdByShell(shell string, cmds []string) (args []string, err error) { +func (e *local) genCmdByShell(shell string, cmds []string) (args []string, err error) { script := "" for _, cmd := range cmds { script += fmt.Sprintf("echo %s\n%s\n", strings.TrimSpace(shellescape.Quote("+ "+cmd)), cmd) @@ -37,7 +37,7 @@ func genCmdByShell(shell string, cmds []string) (args []string, err error) { script += fmt.Sprintf("@%s\n", cmd) script += "@IF NOT %ERRORLEVEL% == 0 exit %ERRORLEVEL%\n" } - cmd, err := os.CreateTemp(os.TempDir(), "*.cmd") + cmd, err := os.CreateTemp(e.tempDir, "*.cmd") if err != nil { return nil, err } diff --git a/pipeline/backend/local/flags.go b/pipeline/backend/local/flags.go index 9c67ac89b..64ec61fb9 100644 --- a/pipeline/backend/local/flags.go +++ b/pipeline/backend/local/flags.go @@ -15,7 +15,16 @@ package local import ( + "os" + "github.com/urfave/cli/v2" ) -var Flags = []cli.Flag{} +var Flags = []cli.Flag{ + &cli.StringFlag{ + Name: "backend-local-temp-dir", + EnvVars: []string{"WOODPECKER_BACKEND_LOCAL_TEMP_DIR"}, + Usage: "set a different temp dir to clone workflows into", + Value: os.TempDir(), + }, +} diff --git a/pipeline/backend/local/local.go b/pipeline/backend/local/local.go index 89ef203ce..f66ac0443 100644 --- a/pipeline/backend/local/local.go +++ b/pipeline/backend/local/local.go @@ -27,6 +27,7 @@ import ( "sync" "github.com/rs/zerolog/log" + "github.com/urfave/cli/v2" "golang.org/x/text/encoding/unicode" "golang.org/x/text/transform" @@ -42,6 +43,7 @@ type workflowState struct { } type local struct { + tempDir string workflows sync.Map output io.ReadCloser pluginGitBinary string @@ -64,7 +66,12 @@ func (e *local) IsAvailable(context.Context) bool { return true } -func (e *local) Load(context.Context) (*types.EngineInfo, error) { +func (e *local) Load(ctx context.Context) (*types.EngineInfo, error) { + c, ok := ctx.Value(types.CliContext).(*cli.Context) + if ok { + e.tempDir = c.String("backend-local-temp-dir") + } + e.loadClone() return &types.EngineInfo{ @@ -76,7 +83,7 @@ func (e *local) Load(context.Context) (*types.EngineInfo, error) { func (e *local) SetupWorkflow(_ context.Context, _ *types.Config, taskUUID string) error { log.Trace().Str("taskUUID", taskUUID).Msg("create workflow environment") - baseDir, err := os.MkdirTemp("", "woodpecker-local-*") + baseDir, err := os.MkdirTemp(e.tempDir, "woodpecker-local-*") if err != nil { return err } @@ -139,7 +146,7 @@ func (e *local) StartStep(ctx context.Context, step *types.Step, taskUUID string // execCommands use step.Image as shell and run the commands in it func (e *local) execCommands(ctx context.Context, step *types.Step, state *workflowState, env []string) error { // Prepare commands - args, err := genCmdByShell(step.Image, step.Commands) + args, err := e.genCmdByShell(step.Image, step.Commands) if err != nil { return fmt.Errorf("could not convert commands into args: %w", err) }