Allow to change directory for steps (#1329)

Add `directory` YAML key that changes the workdir. Can replace a `cd`
before your commands start or make it possible to run plugins in a
subdirectory.
This commit is contained in:
qwerty287 2022-10-24 16:31:06 +02:00 committed by GitHub
parent 27ee8ef97d
commit f6cac78119
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 1 deletions

View file

@ -502,6 +502,10 @@ Woodpecker gives the ability to detach steps to run them in background until the
For more details check the [service docs](./60-services.md#detachment).
### `directory`
Using `directory`, you can set a subdirectory of your repository or an absolute path inside the Docker container in which your commands will run.
## `services`
Woodpecker can provide service containers. They can for example be used to run databases or cache containers during the execution of pipeline.

View file

@ -3,6 +3,7 @@ package compiler
import (
"fmt"
"path"
"path/filepath"
"strings"
"github.com/rs/zerolog/log"
@ -68,7 +69,7 @@ func (c *Compiler) createProcess(name string, container *yaml.Container, section
}
if !detached || len(container.Commands) != 0 {
workingdir = path.Join(c.base, c.path)
workingdir = c.stepWorkdir(container)
}
if !detached {
@ -184,3 +185,10 @@ func (c *Compiler) createProcess(name string, container *yaml.Container, section
IpcMode: ipcMode,
}
}
func (c *Compiler) stepWorkdir(container *yaml.Container) string {
if filepath.IsAbs(container.Directory) {
return container.Directory
}
return filepath.Join(c.base, c.path, container.Directory)
}

View file

@ -37,6 +37,7 @@ type (
Tmpfs []string `yaml:"tmpfs,omitempty"`
DNS types.Stringorslice `yaml:"dns,omitempty"`
DNSSearch types.Stringorslice `yaml:"dns_search,omitempty"`
Directory string `yaml:"directory,omitempty"`
Entrypoint types.Command `yaml:"entrypoint,omitempty"`
Environment types.SliceorMap `yaml:"environment,omitempty"`
ExtraHosts []string `yaml:"extra_hosts,omitempty"`

View file

@ -27,6 +27,7 @@ cpu_shares: 99
detach: true
devices:
- /dev/ttyUSB0:/dev/ttyUSB0
directory: example/
dns: 8.8.8.8
dns_search: example.com
entrypoint: /code/entrypoint.sh
@ -81,6 +82,7 @@ func TestUnmarshalContainer(t *testing.T) {
CPUShares: types.StringorInt(99),
Detached: true,
Devices: []string{"/dev/ttyUSB0:/dev/ttyUSB0"},
Directory: "example/",
DNS: types.Stringorslice{"8.8.8.8"},
DNSSearch: types.Stringorslice{"example.com"},
Entrypoint: types.Command{"/code/entrypoint.sh"},

View file

@ -215,6 +215,9 @@
"environment": {
"$ref": "#/definitions/step_environment"
},
"directory": {
"$ref": "#/definitions/step_directory"
},
"secrets": {
"$ref": "#/definitions/step_secrets"
},
@ -467,6 +470,10 @@
"items": { "type": "string" },
"minLength": 1
},
"step_directory": {
"description": "Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#directory",
"type": "string"
},
"services": {
"description": "Read more: https://woodpecker-ci.org/docs/usage/services",
"type": "object",