@@ -10,6 +10,7 @@ import (
1010 "strings"
1111 "time"
1212
13+ "github.com/drone-plugins/drone-docker/internal/docker"
1314 "github.com/drone-plugins/drone-plugin-lib/drone"
1415)
1516
@@ -75,13 +76,16 @@ type (
7576
7677 // Plugin defines the Docker plugin parameters.
7778 Plugin struct {
78- Login Login // Docker login configuration
79- Build Build // Docker build configuration
80- Daemon Daemon // Docker daemon configuration
81- Dryrun bool // Docker push is skipped
82- Cleanup bool // Docker purge is enabled
83- CardPath string // Card path to write file to
84- ArtifactFile string // Artifact path to write file to
79+ Login Login // Docker login configuration
80+ Build Build // Docker build configuration
81+ Daemon Daemon // Docker daemon configuration
82+ Dryrun bool // Docker push is skipped
83+ Cleanup bool // Docker purge is enabled
84+ CardPath string // Card path to write file to
85+ ArtifactFile string // Artifact path to write file to
86+ BaseImageRegistry string // Docker registry to pull base image
87+ BaseImageUsername string // Docker registry username to pull base image
88+ BaseImagePassword string // Docker registry password to pull base image
8589 }
8690
8791 Card []struct {
@@ -154,7 +158,32 @@ func (p Plugin) Exec() error {
154158 os .MkdirAll (dockerHome , 0600 )
155159
156160 path := filepath .Join (dockerHome , "config.json" )
157- err := os .WriteFile (path , []byte (p .Login .Config ), 0600 )
161+ file , err := os .OpenFile (path , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0600 )
162+ if err != nil {
163+ return fmt .Errorf ("Error writing config.json: %s" , err )
164+ }
165+ err = os .WriteFile (path , []byte (p .Login .Config ), 0600 )
166+ if err != nil {
167+ return fmt .Errorf ("Error writing config.json: %s" , err )
168+ }
169+ file .Close ()
170+ }
171+
172+ // add base image docker credentials to the existing config file, else create new
173+ if p .BaseImagePassword != "" {
174+ json , err := setDockerAuth (p .Login .Username , p .Login .Password , p .Login .Registry ,
175+ p .BaseImageUsername , p .BaseImagePassword , p .BaseImageRegistry )
176+ if err != nil {
177+ return fmt .Errorf ("Failed to set authentication in docker config %s" , err )
178+ }
179+ os .MkdirAll (dockerHome , 0600 )
180+ path := filepath .Join (dockerHome , "config.json" )
181+ file , err := os .OpenFile (path , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0600 )
182+ if err != nil {
183+ return fmt .Errorf ("Error opening config.json: %s" , err )
184+ }
185+ defer file .Close ()
186+ _ , err = file .Write (json )
158187 if err != nil {
159188 return fmt .Errorf ("Error writing config.json: %s" , err )
160189 }
@@ -270,6 +299,35 @@ func (p Plugin) Exec() error {
270299 return nil
271300}
272301
302+ // helper function to set the credentials
303+ func setDockerAuth (username , password , registry , baseImageUsername ,
304+ baseImagePassword , baseImageRegistry string ) ([]byte , error ) {
305+ var credentials []docker.RegistryCredentials
306+ // add only docker registry to the config
307+ dockerConfig := docker .NewConfig ()
308+ if password != "" {
309+ pushToRegistryCreds := docker.RegistryCredentials {
310+ Registry : registry ,
311+ Username : username ,
312+ Password : password ,
313+ }
314+ // push registry auth
315+ credentials = append (credentials , pushToRegistryCreds )
316+ }
317+
318+ if baseImageRegistry != "" {
319+ pullFromRegistryCreds := docker.RegistryCredentials {
320+ Registry : baseImageRegistry ,
321+ Username : baseImageUsername ,
322+ Password : baseImagePassword ,
323+ }
324+ // base image registry auth
325+ credentials = append (credentials , pullFromRegistryCreds )
326+ }
327+ // Creates docker config for both the registries used for authentication
328+ return dockerConfig .CreateDockerConfigJson (credentials )
329+ }
330+
273331// helper function to create the docker login command.
274332func commandLogin (login Login ) * exec.Cmd {
275333 if login .Email != "" {
0 commit comments