From 904641bf10036ee91bbe3d67e290721673158f6c Mon Sep 17 00:00:00 2001 From: Albie Date: Thu, 8 Jul 2021 08:00:51 +0100 Subject: [PATCH] feat: add option to specify buildkit config file path (#32) --- cmd/drone-docker-buildx/config.go | 6 ++++++ plugin/docker.go | 14 +++++++++++-- plugin/impl.go | 35 ++++++++++++++++++------------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/cmd/drone-docker-buildx/config.go b/cmd/drone-docker-buildx/config.go index 82aecec..a561ae7 100644 --- a/cmd/drone-docker-buildx/config.go +++ b/cmd/drone-docker-buildx/config.go @@ -93,6 +93,12 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag { EnvVars: []string{"PLUGIN_DAEMON_OFF"}, Destination: &settings.Daemon.Disabled, }, + &cli.StringFlag{ + Name: "daemon.buildkit-config", + Usage: "location of buildkit config file", + EnvVars: []string{"PLUGIN_BUILDKIT_CONFIG"}, + Destination: &settings.Daemon.BuildkitConfig, + }, &cli.StringFlag{ Name: "dockerfile", Usage: "build dockerfile", diff --git a/plugin/docker.go b/plugin/docker.go index d73e9b0..1a99fe0 100644 --- a/plugin/docker.go +++ b/plugin/docker.go @@ -52,8 +52,18 @@ func commandInfo() *exec.Cmd { return exec.Command(dockerExe, "info") } -func commandBuilder() *exec.Cmd { - return exec.Command(dockerExe, "buildx", "create", "--use") +func commandBuilder(daemon Daemon) *exec.Cmd { + args := []string{ + "buildx", + "create", + "--use", + } + + if daemon.BuildkitConfig != "" { + args = append(args, "--config", daemon.BuildkitConfig) + } + + return exec.Command(dockerExe, args...) } func commandBuildx() *exec.Cmd { diff --git a/plugin/impl.go b/plugin/impl.go index 0cd15eb..a4c8378 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -14,19 +14,20 @@ import ( // Daemon defines Docker daemon parameters. type Daemon struct { - Registry string // Docker registry - Mirror string // Docker registry mirror - Insecure bool // Docker daemon enable insecure registries - StorageDriver string // Docker daemon storage driver - StoragePath string // Docker daemon storage path - Disabled bool // DOcker daemon is disabled (already running) - Debug bool // Docker daemon started in debug mode - Bip string // Docker daemon network bridge IP address - DNS cli.StringSlice // Docker daemon dns server - DNSSearch cli.StringSlice // Docker daemon dns search domain - MTU string // Docker daemon mtu setting - IPv6 bool // Docker daemon IPv6 networking - Experimental bool // Docker daemon enable experimental mode + Registry string // Docker registry + Mirror string // Docker registry mirror + Insecure bool // Docker daemon enable insecure registries + StorageDriver string // Docker daemon storage driver + StoragePath string // Docker daemon storage path + Disabled bool // DOcker daemon is disabled (already running) + Debug bool // Docker daemon started in debug mode + Bip string // Docker daemon network bridge IP address + DNS cli.StringSlice // Docker daemon dns server + DNSSearch cli.StringSlice // Docker daemon dns search domain + MTU string // Docker daemon mtu setting + IPv6 bool // Docker daemon IPv6 networking + Experimental bool // Docker daemon enable experimental mode + BuildkitConfig string // Docker buildkit config file } // Login defines Docker login parameters. @@ -77,6 +78,12 @@ func (p *Plugin) Validate() error { p.settings.Build.Ref = p.pipeline.Commit.Ref p.settings.Daemon.Registry = p.settings.Login.Registry + if p.settings.Daemon.BuildkitConfig != "" { + if _, err := os.Stat(p.settings.Daemon.BuildkitConfig); err != nil && os.IsNotExist(err) { + return fmt.Errorf("given buildkit config file not found") + } + } + if p.settings.Build.TagsAuto { // return true if tag event or default branch if UseDefaultTag( @@ -159,7 +166,7 @@ func (p *Plugin) Execute() error { var cmds []*exec.Cmd cmds = append(cmds, commandVersion()) // docker version cmds = append(cmds, commandInfo()) // docker info - cmds = append(cmds, commandBuilder()) + cmds = append(cmds, commandBuilder(p.settings.Daemon)) cmds = append(cmds, commandBuildx()) // pre-pull cache images