From 2e0e0dd7d7e4317ff57144d334b187c5d0cf3122 Mon Sep 17 00:00:00 2001 From: Maarten De Wispelaere <50638541+BitProcessor@users.noreply.github.com> Date: Fri, 2 Jul 2021 18:58:55 +0200 Subject: [PATCH] Possible fix for https://github.com/thegeeklab/drone-docker-buildx/issues/28 (#30) --- plugin/docker.go | 51 ++++++++---------------------------------------- plugin/impl.go | 23 +--------------------- 2 files changed, 9 insertions(+), 65 deletions(-) diff --git a/plugin/docker.go b/plugin/docker.go index 8350d48..d73e9b0 100644 --- a/plugin/docker.go +++ b/plugin/docker.go @@ -61,13 +61,12 @@ func commandBuildx() *exec.Cmd { } // helper function to create the docker build command. -func commandBuild(build Build) *exec.Cmd { +func commandBuild(build Build, dryrun bool) *exec.Cmd { args := []string{ "buildx", "build", "--rm=true", "-f", build.Dockerfile, - "-t", build.Name, } defaultBuildArgs := []string{ @@ -75,6 +74,9 @@ func commandBuild(build Build) *exec.Cmd { } args = append(args, build.Context) + if ! dryrun { + args = append(args, "--push") + } if build.Squash { args = append(args, "--squash") } @@ -106,16 +108,14 @@ func commandBuild(build Build) *exec.Cmd { args = append(args, "--quiet") } - if len(build.Platforms.Value()) > 1 { - args = append(args, "--push") - } else { - args = append(args, "--load") - } - if len(build.Platforms.Value()) > 0 { args = append(args, "--platform", strings.Join(build.Platforms.Value()[:], ",")) } + for _, arg := range build.Tags.Value() { + args = append(args, "-t", fmt.Sprintf("%s:%s", build.Repo, arg)) + } + return exec.Command(dockerExe, args...) } @@ -162,23 +162,6 @@ func hasProxyBuildArg(build *Build, key string) bool { return false } -// helper function to create the docker tag command. -func commandTag(build Build, tag string) *exec.Cmd { - var ( - source = build.Name - target = fmt.Sprintf("%s:%s", build.Repo, tag) - ) - return exec.Command( - dockerExe, "tag", source, target, - ) -} - -// helper function to create the docker push command. -func commandPush(build Build, tag string) *exec.Cmd { - target := fmt.Sprintf("%s:%s", build.Repo, tag) - return exec.Command(dockerExe, "push", target) -} - // helper function to create the docker daemon command. func commandDaemon(daemon Daemon) *exec.Cmd { args := []string{ @@ -216,24 +199,6 @@ func commandDaemon(daemon Daemon) *exec.Cmd { return exec.Command(dockerdExe, args...) } -// helper to check if args match "docker prune" -func isCommandPrune(args []string) bool { - return len(args) > 3 && args[2] == "prune" -} - -func commandPrune() *exec.Cmd { - return exec.Command(dockerExe, "system", "prune", "-f") -} - -// helper to check if args match "docker rmi" -func isCommandRmi(args []string) bool { - return len(args) > 2 && args[1] == "rmi" -} - -func commandRmi(tag string) *exec.Cmd { - return exec.Command(dockerExe, "rmi", tag) -} - // trace writes each command to stdout with the command wrapped in an xml // tag so that it can be extracted and displayed in the logs. func trace(cmd *exec.Cmd) { diff --git a/plugin/impl.go b/plugin/impl.go index 6e0464e..d8b6f52 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -83,10 +83,6 @@ func (p *Plugin) Validate() error { p.settings.Build.Ref = p.pipeline.Commit.Ref p.settings.Daemon.Registry = p.settings.Login.Registry - if len(p.settings.Build.Platforms.Value()) > 1 && p.settings.Dryrun { - return fmt.Errorf("dryrun is not supported on multi-platform builds") - } - if p.settings.Build.TagsAuto { // return true if tag event or default branch if UseDefaultTag( @@ -177,20 +173,7 @@ func (p *Plugin) Execute() error { cmds = append(cmds, commandPull(img)) } - cmds = append(cmds, commandBuild(p.settings.Build)) // docker build - - for _, tag := range p.settings.Build.Tags.Value() { - cmds = append(cmds, commandTag(p.settings.Build, tag)) // docker tag - - if !p.settings.Dryrun { - cmds = append(cmds, commandPush(p.settings.Build, tag)) // docker push - } - } - - if p.settings.Cleanup { - cmds = append(cmds, commandRmi(p.settings.Build.Name)) // docker rmi - cmds = append(cmds, commandPrune()) // docker system prune -f - } + cmds = append(cmds, commandBuild(p.settings.Build, p.settings.Dryrun)) // docker build // execute all commands in batch mode. for _, cmd := range cmds { @@ -201,10 +184,6 @@ func (p *Plugin) Execute() error { err := cmd.Run() if err != nil && isCommandPull(cmd.Args) { fmt.Printf("Could not pull cache-from image %s. Ignoring...\n", cmd.Args[2]) - } else if err != nil && isCommandPrune(cmd.Args) { - fmt.Printf("Could not prune system containers. Ignoring...\n") - } else if err != nil && isCommandRmi(cmd.Args) { - fmt.Printf("Could not remove image %s. Ignoring...\n", cmd.Args[2]) } else if err != nil { return err }