diff --git a/config/config.go b/config/config.go index cf077156..e0d03f63 100644 --- a/config/config.go +++ b/config/config.go @@ -57,6 +57,8 @@ type ( Environ []string Volumes []string Labels map[string]string `envconfig:"DRONE_AGENT_LABELS"` + Dockerusername string `envconfig:"DRONE_AGENT_DOCKER_USERNAME"` + Dockerpassword string `envconfig:"DRONE_AGENT_DOCKER_PASSWORD"` } Runner Runner diff --git a/engine/engine.go b/engine/engine.go index 3203fb3a..7c039cc8 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -78,6 +78,8 @@ func New( watchtowerImage: config.Watchtower.Image, watchtowerTimeout: config.Watchtower.Timeout, watchtowerInterval: config.Watchtower.Interval, + dockerUsername: config.Agent.Dockerusername, + dockerPassword: config.Agent.Dockerpassword, }, pinger: &pinger{ servers: servers, diff --git a/engine/install.go b/engine/install.go index a8f22e09..82c892a2 100644 --- a/engine/install.go +++ b/engine/install.go @@ -5,6 +5,8 @@ package engine import ( + "encoding/base64" + "encoding/json" "context" "fmt" "io" @@ -58,6 +60,8 @@ type installer struct { servers autoscaler.ServerStore client clientFunc + dockerUsername string + dockerPassword string } func (i *installer) Install(ctx context.Context) error { @@ -143,9 +147,23 @@ poller: logger.Debug(). Str("image", i.image). - Msg("pull docker image") + Msg("pull docker image") - rc, err := client.ImagePull(ctx, i.image, types.ImagePullOptions{}) + options := types.ImagePullOptions{} + if i.dockerUsername != "" && i.dockerPassword != "" { + authConfig := types.AuthConfig{ + Username: i.dockerUsername, + Password: i.dockerPassword, + } + encodedJSON, err := json.Marshal(authConfig) + if err != nil { + panic(err) + } + authStr := base64.URLEncoding.EncodeToString(encodedJSON) + options = types.ImagePullOptions{RegistryAuth: authStr} + } + + rc, err := client.ImagePull(ctx, i.image, options) if err != nil { logger.Error().Err(err). Str("image", i.image).