mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-03 14:18:42 +00:00
Deduplicate step docker container volumes (#1571)
Try to fix #1495 It's very hard to reproduce it and only way to fix when it gets in this state is woodpecker agent restart. This anyway fixes problem if step mounts and `WOODPECKER_BACKEND_DOCKER_VOLUMES` conflict
This commit is contained in:
parent
4c97a0104e
commit
f26a87acce
2 changed files with 18 additions and 14 deletions
|
@ -31,6 +31,7 @@ import (
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
||||||
|
"github.com/woodpecker-ci/woodpecker/shared/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type docker struct {
|
type docker struct {
|
||||||
|
@ -133,12 +134,11 @@ func (e *docker) Exec(ctx context.Context, step *backend.Step) error {
|
||||||
if step.Pull {
|
if step.Pull {
|
||||||
responseBody, perr := e.client.ImagePull(ctx, config.Image, pullopts)
|
responseBody, perr := e.client.ImagePull(ctx, config.Image, pullopts)
|
||||||
if perr == nil {
|
if perr == nil {
|
||||||
defer responseBody.Close()
|
|
||||||
|
|
||||||
fd, isTerminal := term.GetFdInfo(os.Stdout)
|
fd, isTerminal := term.GetFdInfo(os.Stdout)
|
||||||
if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil {
|
if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil {
|
||||||
log.Error().Err(err).Msg("DisplayJSONMessagesStream")
|
log.Error().Err(err).Msg("DisplayJSONMessagesStream")
|
||||||
}
|
}
|
||||||
|
responseBody.Close()
|
||||||
}
|
}
|
||||||
// Fix "Show warning when fail to auth to docker registry"
|
// Fix "Show warning when fail to auth to docker registry"
|
||||||
// (https://web.archive.org/web/20201023145804/https://github.com/drone/drone/issues/1917)
|
// (https://web.archive.org/web/20201023145804/https://github.com/drone/drone/issues/1917)
|
||||||
|
@ -148,7 +148,7 @@ func (e *docker) Exec(ctx context.Context, step *backend.Step) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// add default volumes to the host configuration
|
// add default volumes to the host configuration
|
||||||
hostConfig.Binds = append(hostConfig.Binds, e.volumes...)
|
hostConfig.Binds = utils.DedupStrings(append(hostConfig.Binds, e.volumes...))
|
||||||
|
|
||||||
_, err := e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, step.Name)
|
_, err := e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, step.Name)
|
||||||
if client.IsErrNotFound(err) {
|
if client.IsErrNotFound(err) {
|
||||||
|
@ -158,11 +158,11 @@ func (e *docker) Exec(ctx context.Context, step *backend.Step) error {
|
||||||
if perr != nil {
|
if perr != nil {
|
||||||
return perr
|
return perr
|
||||||
}
|
}
|
||||||
defer responseBody.Close()
|
|
||||||
fd, isTerminal := term.GetFdInfo(os.Stdout)
|
fd, isTerminal := term.GetFdInfo(os.Stdout)
|
||||||
if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil {
|
if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil {
|
||||||
log.Error().Err(err).Msg("DisplayJSONMessagesStream")
|
log.Error().Err(err).Msg("DisplayJSONMessagesStream")
|
||||||
}
|
}
|
||||||
|
responseBody.Close()
|
||||||
|
|
||||||
_, err = e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, step.Name)
|
_, err = e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, step.Name)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,20 +15,24 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
// DedupStrings deduplicate string list, empty items are dropped
|
// DedupStrings deduplicate string list, empty items are dropped
|
||||||
func DedupStrings(list []string) []string {
|
func DedupStrings(src []string) []string {
|
||||||
m := make(map[string]struct{}, len(list))
|
m := make(map[string]struct{}, len(src))
|
||||||
|
dst := make([]string, 0, len(src))
|
||||||
|
|
||||||
for i := range list {
|
for _, v := range src {
|
||||||
if s := list[i]; len(s) > 0 {
|
// Skip empty items
|
||||||
m[list[i]] = struct{}{}
|
if len(v) == 0 {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
// Skip duplicates
|
||||||
|
if _, ok := m[v]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
m[v] = struct{}{}
|
||||||
|
dst = append(dst, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
newList := make([]string, 0, len(m))
|
return dst
|
||||||
for k := range m {
|
|
||||||
newList = append(newList, k)
|
|
||||||
}
|
|
||||||
return newList
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EqualStringSlice compare two string slices if they have equal values independent of how they are sorted
|
// EqualStringSlice compare two string slices if they have equal values independent of how they are sorted
|
||||||
|
|
Loading…
Reference in a new issue