Add option to set default volumes for docker backend (#1203)

This commit is contained in:
Lauris BH
2022-09-26 17:59:26 +03:00
committed by GitHub
parent 9c99406a06
commit 2e08dd2333
2 changed files with 23 additions and 1 deletions

View File

@@ -23,6 +23,7 @@ type docker struct {
client client.APIClient
enableIPv6 bool
network string
volumes []string
}
// make sure docker implements Engine
@@ -59,6 +60,18 @@ func (e *docker) Load() error {
e.network = os.Getenv("WOODPECKER_BACKEND_DOCKER_NETWORK")
volumes := strings.Split(os.Getenv("WOODPECKER_BACKEND_DOCKER_VOLUMES"), ",")
e.volumes = make([]string, 0, len(volumes))
// Validate provided volume definitions
for _, v := range volumes {
parts, err := splitVolumeParts(v)
if err != nil {
log.Error().Err(err).Msgf("invalid volume '%s' provided in WOODPECKER_BACKEND_DOCKER_VOLUMES", v)
continue
}
e.volumes = append(e.volumes, strings.Join(parts, ":"))
}
return nil
}
@@ -117,6 +130,9 @@ func (e *docker) Exec(ctx context.Context, proc *backend.Step) error {
}
}
// add default volumes to the host configuration
hostConfig.Binds = append(hostConfig.Binds, e.volumes...)
_, err := e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, proc.Name)
if client.IsErrNotFound(err) {
// automatically pull and try to re-create the image if the