From d264446eb5e28d5a4ca2f4513cffb433e3bf6a1e Mon Sep 17 00:00:00 2001 From: Martin Honermeyer Date: Mon, 8 Aug 2022 13:36:23 +0200 Subject: [PATCH] feat: add `cache_to` option and remove manual image pull for `cache_from` images (#124) --- _docs/data/data.yaml | 5 +++++ cmd/drone-docker-buildx/config.go | 7 +++++++ plugin/docker.go | 12 +++--------- plugin/impl.go | 10 ++-------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/_docs/data/data.yaml b/_docs/data/data.yaml index 7621dcd..7c2cda7 100644 --- a/_docs/data/data.yaml +++ b/_docs/data/data.yaml @@ -143,6 +143,11 @@ properties: type: list required: false + cache_to: + description: [Cache destination](https://docs.docker.com/engine/reference/commandline/buildx_build/#cache-to) for the build cache. + type: string + required: false + pull_image: description: Enforce to pull the base image at build time. defaultValue: true diff --git a/cmd/drone-docker-buildx/config.go b/cmd/drone-docker-buildx/config.go index f92c4c8..4bcb574 100644 --- a/cmd/drone-docker-buildx/config.go +++ b/cmd/drone-docker-buildx/config.go @@ -201,6 +201,13 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Destination: &settings.Build.CacheFrom, Category: category, }, + &cli.StringFlag{ + Name: "cache-to", + EnvVars: []string{"PLUGIN_CACHE_TO"}, + Usage: "cache destination for the build cache", + Destination: &settings.Build.CacheTo, + Category: category, + }, &cli.BoolFlag{ Name: "pull-image", EnvVars: []string{"PLUGIN_PULL_IMAGE"}, diff --git a/plugin/docker.go b/plugin/docker.go index dc56d9a..557dae2 100644 --- a/plugin/docker.go +++ b/plugin/docker.go @@ -23,15 +23,6 @@ func commandLogin(login Login) *exec.Cmd { ) } -// helper to check if args match "docker pull " -func isCommandPull(args []string) bool { - return len(args) > 2 && args[1] == "pull" -} - -func commandPull(repo string) *exec.Cmd { - return exec.Command(dockerExe, "pull", repo) -} - func commandLoginEmail(login Login) *exec.Cmd { return exec.Command( dockerExe, "login", @@ -99,6 +90,9 @@ func commandBuild(build Build, dryrun bool) *exec.Cmd { for _, arg := range build.CacheFrom.Value() { args = append(args, "--cache-from", arg) } + if build.CacheTo != "" { + args = append(args, "--cache-to", build.CacheTo) + } for _, arg := range build.ArgsEnv.Value() { addProxyValue(&build, arg) } diff --git a/plugin/impl.go b/plugin/impl.go index d2921df..809a232 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -53,6 +53,7 @@ type Build struct { Target string // Docker build target Pull bool // Docker build pull CacheFrom cli.StringSlice // Docker build cache-from + CacheTo string // Docker build cache-to Compress bool // Docker build compress Repo string // Docker build repository NoCache bool // Docker build no-cache @@ -166,11 +167,6 @@ func (p *Plugin) Execute() error { cmds = append(cmds, commandBuilder(p.settings.Daemon)) cmds = append(cmds, commandBuildx()) - // pre-pull cache images - for _, img := range p.settings.Build.CacheFrom.Value() { - cmds = append(cmds, commandPull(img)) - } - cmds = append(cmds, commandBuild(p.settings.Build, p.settings.Dryrun)) // docker build // execute all commands in batch mode. @@ -180,9 +176,7 @@ func (p *Plugin) Execute() error { trace(cmd) 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 { + if err != nil { return err } }