From dadaaca4e03e22b68773ac903cc8101710c5f811 Mon Sep 17 00:00:00 2001 From: Neil Skrypuch Date: Wed, 6 Apr 2022 20:20:49 -0400 Subject: [PATCH 1/2] With Buildkit, don't pull docker images needlessly. When using Docker with Buildkit and inline cache information enabled, docker build will pull image layers to use as cache on demand. As a result, unconditionally pulling the cache is both superfluous and slower, so let's not do that. --- docker.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/docker.go b/docker.go index 9d53c24f..7366f1f8 100644 --- a/docker.go +++ b/docker.go @@ -171,9 +171,20 @@ func (p Plugin) Exec() error { cmds = append(cmds, commandVersion()) // docker version cmds = append(cmds, commandInfo()) // docker info - // pre-pull cache images - for _, img := range p.Build.CacheFrom { - cmds = append(cmds, commandPull(img)) + var inlineCache = false + for _, v := range p.Build.Args { + if v == "BUILDKIT_INLINE_CACHE=1" { + inlineCache = true + break + } + } + if os.Getenv("DOCKER_BUILDKIT") != "1" || !inlineCache { + // pre-pull cache images for non-buildkit + for _, img := range p.Build.CacheFrom { + cmds = append(cmds, commandPull(img)) + } + } else { + fmt.Println("Detected Buildkit with inline cache, will not pre-pull cache image.") } cmds = append(cmds, commandBuild(p.Build)) // docker build From e25cde0604dccc06994788c8671a09203b1ad048 Mon Sep 17 00:00:00 2001 From: Neil Skrypuch Date: Fri, 10 Jun 2022 19:19:55 -0400 Subject: [PATCH 2/2] Extract some logic into a helper function --- docker.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/docker.go b/docker.go index 7366f1f8..fbe910ab 100644 --- a/docker.go +++ b/docker.go @@ -171,15 +171,8 @@ func (p Plugin) Exec() error { cmds = append(cmds, commandVersion()) // docker version cmds = append(cmds, commandInfo()) // docker info - var inlineCache = false - for _, v := range p.Build.Args { - if v == "BUILDKIT_INLINE_CACHE=1" { - inlineCache = true - break - } - } - if os.Getenv("DOCKER_BUILDKIT") != "1" || !inlineCache { - // pre-pull cache images for non-buildkit + if wantPrepull(p) { + // pre-pull cache images for _, img := range p.Build.CacheFrom { cmds = append(cmds, commandPull(img)) } @@ -238,6 +231,22 @@ func (p Plugin) Exec() error { return nil } +// helper function to determine if we should prepull a cache image +func wantPrepull(p Plugin) bool { + var inlineCache = false + for _, v := range p.Build.Args { + if v == "BUILDKIT_INLINE_CACHE=1" { + inlineCache = true + break + } + } + + // if we have both buildkit and buildkit inline cache available, then + // that is preferable to prepulling a cache image, for more details: + // https://github.com/drone-plugins/drone-docker/pull/360 + return os.Getenv("DOCKER_BUILDKIT") != "1" || !inlineCache +} + // helper function to create the docker login command. func commandLogin(login Login) *exec.Cmd { if login.Email != "" {