Skip to content

Commit

Permalink
Merge pull request #37 from drone-plugins/CI-13880
Browse files Browse the repository at this point in the history
feat: [CI-13880]: Enable new driver opts
  • Loading branch information
HenAmar authored Sep 12, 2024
2 parents 387a8e4 + d3c499d commit 66ecda5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
15 changes: 11 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ func Run() {
EnvVar: "PLUGIN_BUILDER_DRIVER_OPTS",
Value: new(CustomStringSliceFlag),
},
cli.GenericFlag{
Name: "builder-driver-opts-new",
Usage: "buildx builder driver opts new",
EnvVar: "PLUGIN_BUILDER_DRIVER_OPTS_NEW",
Value: new(CustomStringSliceFlag),
},
cli.StringFlag{
Name: "builder-remote-conn",
EnvVar: "PLUGIN_BUILDER_REMOTE_CONN",
Expand Down Expand Up @@ -421,10 +427,11 @@ func run(c *cli.Context) error {
ArtifactRegistry: c.String("artifact.registry"),
},
Builder: Builder{
Name: c.String("builder-name"),
Driver: c.String("builder-driver"),
DriverOpts: c.Generic("builder-driver-opts").(*CustomStringSliceFlag).GetValue(),
RemoteConn: c.String("builder-remote-conn"),
Name: c.String("builder-name"),
Driver: c.String("builder-driver"),
DriverOpts: c.Generic("builder-driver-opts").(*CustomStringSliceFlag).GetValue(),
DriverOptsNew: c.Generic("builder-driver-opts-new").(*CustomStringSliceFlag).GetValue(),
RemoteConn: c.String("builder-remote-conn"),
},
BaseImageRegistry: c.String("docker.baseimageregistry"),
BaseImageUsername: c.String("docker.baseimageusername"),
Expand Down
4 changes: 2 additions & 2 deletions buildx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ const (
remoteDriver = "remote"
)

func cmdSetupBuildx(builder Builder) *exec.Cmd {
func cmdSetupBuildx(builder Builder, driverOpts []string) *exec.Cmd {
args := []string{"buildx", "create", "--use", "--driver", builder.Driver}
if builder.Name != "" {
args = append(args, "--name", builder.Name)
}
for _, opt := range builder.DriverOpts {
for _, opt := range driverOpts {
args = append(args, "--driver-opt", opt)
}
if builder.RemoteConn != "" && builder.Driver == remoteDriver {
Expand Down
33 changes: 25 additions & 8 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ type (
}

Builder struct {
Name string // Buildx builder name
Driver string // Buildx driver type
DriverOpts []string // Buildx driver opts
RemoteConn string // Buildx remote connection endpoint
Name string // Buildx builder name
Driver string // Buildx driver type
DriverOpts []string // Buildx driver opts
DriverOptsNew []string // Buildx driver opts new
RemoteConn string // Buildx remote connection endpoint
}

// Login defines Docker login parameters.
Expand Down Expand Up @@ -230,10 +231,26 @@ func (p Plugin) Exec() error {
}

if p.Builder.Driver != "" && p.Builder.Driver != defaultDriver {
createCmd := cmdSetupBuildx(p.Builder)
raw, err := createCmd.Output()
if err != nil {
return fmt.Errorf("error while creating buildx builder: %s and err: %s", string(raw), err)
var (
raw []byte
err error
)
shouldFallback := true
if len(p.Builder.DriverOptsNew) != 0 {
createCmd := cmdSetupBuildx(p.Builder, p.Builder.DriverOptsNew)
raw, err = createCmd.Output()
if err == nil {
shouldFallback = false
} else {
fmt.Printf("Unable to setup buildx with new driver opts: %s\n", err)
}
}
if shouldFallback {
createCmd := cmdSetupBuildx(p.Builder, p.Builder.DriverOpts)
raw, err = createCmd.Output()
if err != nil {
return fmt.Errorf("error while creating buildx builder: %s and err: %s", string(raw), err)
}
}
p.Builder.Name = strings.TrimSuffix(string(raw), "\n")

Expand Down

0 comments on commit 66ecda5

Please sign in to comment.