Option to change temp dir for local backend (#2702)

---
*Sponsored by Kithara Software GmbH*
This commit is contained in:
6543 2023-11-02 15:45:18 +01:00 committed by GitHub
parent da6f39258d
commit 7bc40f20cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 6 deletions

View file

@ -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

View file

@ -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
```

View file

@ -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
}

View file

@ -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(),
},
}

View file

@ -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)
}