From a5a561fd91fb919197f9a16507311caa6f68cee6 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sun, 25 Jul 2021 14:28:33 +0200 Subject: [PATCH] feat: load buildkit config from string instead of file (#37) --- cmd/drone-docker-buildx/config.go | 2 +- go.sum | 3 --- plugin/daemon.go | 7 ++++--- plugin/docker.go | 10 +++++----- plugin/impl.go | 18 +++++++++--------- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/cmd/drone-docker-buildx/config.go b/cmd/drone-docker-buildx/config.go index a561ae7..581962b 100644 --- a/cmd/drone-docker-buildx/config.go +++ b/cmd/drone-docker-buildx/config.go @@ -95,7 +95,7 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag { }, &cli.StringFlag{ Name: "daemon.buildkit-config", - Usage: "location of buildkit config file", + Usage: "docker buildkit json config content", EnvVars: []string{"PLUGIN_BUILDKIT_CONFIG"}, Destination: &settings.Daemon.BuildkitConfig, }, diff --git a/go.sum b/go.sum index 2b9338f..4612d64 100644 --- a/go.sum +++ b/go.sum @@ -12,7 +12,6 @@ github.com/drone-plugins/drone-plugin-lib v0.4.0/go.mod h1:EgqogX38GoJFtckeSQyhB github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= -github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -71,7 +70,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= -honnef.co/go/tools v0.1.4 h1:SadWOkti5uVN1FAMgxn165+Mw00fuQKyk4Gyn/inxNQ= -honnef.co/go/tools v0.1.4/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las= honnef.co/go/tools v0.2.0 h1:ws8AfbgTX3oIczLPNPCu5166oBg9ST2vNs0rcht+mDE= honnef.co/go/tools v0.2.0/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY= diff --git a/plugin/daemon.go b/plugin/daemon.go index 0705fb5..d851e5e 100644 --- a/plugin/daemon.go +++ b/plugin/daemon.go @@ -1,13 +1,14 @@ package plugin import ( - "io/ioutil" + "io" "os" ) const dockerExe = "/usr/local/bin/docker" const dockerdExe = "/usr/local/bin/dockerd" const dockerHome = "/root/.docker/" +const buildkitConfig = "/tmp/buildkit.json" func (p Plugin) startDaemon() { cmd := commandDaemon(p.settings.Daemon) @@ -15,8 +16,8 @@ func (p Plugin) startDaemon() { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr } else { - cmd.Stdout = ioutil.Discard - cmd.Stderr = ioutil.Discard + cmd.Stdout = io.Discard + cmd.Stderr = io.Discard } go func() { trace(cmd) diff --git a/plugin/docker.go b/plugin/docker.go index 1a99fe0..903c6cc 100644 --- a/plugin/docker.go +++ b/plugin/docker.go @@ -54,13 +54,13 @@ func commandInfo() *exec.Cmd { func commandBuilder(daemon Daemon) *exec.Cmd { args := []string{ - "buildx", - "create", + "buildx", + "create", "--use", } if daemon.BuildkitConfig != "" { - args = append(args, "--config", daemon.BuildkitConfig) + args = append(args, "--config", buildkitConfig) } return exec.Command(dockerExe, args...) @@ -84,7 +84,7 @@ func commandBuild(build Build, dryrun bool) *exec.Cmd { } args = append(args, build.Context) - if ! dryrun { + if !dryrun { args = append(args, "--push") } if build.Squash { @@ -124,7 +124,7 @@ func commandBuild(build Build, dryrun bool) *exec.Cmd { for _, arg := range build.Tags.Value() { args = append(args, "-t", fmt.Sprintf("%s:%s", build.Repo, arg)) - } + } return exec.Command(dockerExe, args...) } diff --git a/plugin/impl.go b/plugin/impl.go index a4c8378..bfdadd7 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -2,7 +2,6 @@ package plugin import ( "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -27,7 +26,7 @@ type Daemon struct { 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 + BuildkitConfig string // Docker buildkit config } // Login defines Docker login parameters. @@ -78,12 +77,6 @@ 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( @@ -131,7 +124,7 @@ func (p *Plugin) Execute() error { os.MkdirAll(dockerHome, 0600) path := filepath.Join(dockerHome, "config.json") - err := ioutil.WriteFile(path, []byte(p.settings.Login.Config), 0600) + err := os.WriteFile(path, []byte(p.settings.Login.Config), 0600) if err != nil { return fmt.Errorf("error writing config.json: %s", err) } @@ -146,6 +139,13 @@ func (p *Plugin) Execute() error { } } + if p.settings.Daemon.BuildkitConfig != "" { + err := os.WriteFile(buildkitConfig, []byte(p.settings.Daemon.BuildkitConfig), 0600) + if err != nil { + return fmt.Errorf("error writing buildkit.json: %s", err) + } + } + switch { case p.settings.Login.Password != "": fmt.Println("Detected registry credentials")