From a187589df98c64000b5b999d5714febf370d696a Mon Sep 17 00:00:00 2001 From: miniduikboot Date: Wed, 23 Nov 2022 15:55:12 +0000 Subject: [PATCH] add `cache_to` option and remove manual image pull (#46) Port from https://github.com/thegeeklab/drone-docker-buildx/pull/124 Basically docker cache got a bit fancier and now includes a cache manifest. Manually pulling these containers isn't sufficient enough, so remove the manual pull. cache_to allows you to push your cache including a manifest to a remote location: https://docs.docker.com/engine/reference/commandline/buildx_build/#cache-to Unfortunately this option isn't quite that useful: to fully use this feature, you need to supply your arguments in CSV format, which woodpecker unhelpfully splits into multiple completely separate arguments, breaking it :( With a further change to fix that issue I managed to turn a multistage rust build from ~3 minutes down to 20 seconds. Co-authored-by: miniduikboot Reviewed-on: https://codeberg.org/woodpecker-plugins/plugin-docker-buildx/pulls/46 Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: miniduikboot Co-committed-by: miniduikboot --- cmd/docker-buildx/config.go | 6 ++++++ plugin/docker.go | 12 +++--------- plugin/impl.go | 11 ++--------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/cmd/docker-buildx/config.go b/cmd/docker-buildx/config.go index b6e7ff3..980a036 100644 --- a/cmd/docker-buildx/config.go +++ b/cmd/docker-buildx/config.go @@ -183,6 +183,12 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag { Usage: "sets images to consider as cache sources", Destination: &settings.Build.CacheFrom, }, + &cli.StringSliceFlag{ + Name: "cache-to", + EnvVars: []string{"PLUGIN_CACHE_TO"}, + Usage: "cache destination for the build cache", + Destination: &settings.Build.CacheTo, + }, &cli.BoolFlag{ Name: "pull-image", EnvVars: []string{"PLUGIN_PULL_IMAGE"}, diff --git a/plugin/docker.go b/plugin/docker.go index 7a18b86..d914692 100644 --- a/plugin/docker.go +++ b/plugin/docker.go @@ -10,15 +10,6 @@ import ( "github.com/urfave/cli/v2" ) -// 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", @@ -86,6 +77,9 @@ func commandBuild(build Build, dryrun bool) *exec.Cmd { for _, arg := range build.CacheFrom.Value() { args = append(args, "--cache-from", arg) } + for _, arg := range build.CacheTo.Value() { + args = append(args, "--cache-to", arg) + } for _, arg := range build.ArgsEnv.Value() { addProxyValue(&build, arg) } diff --git a/plugin/impl.go b/plugin/impl.go index 7ed723e..30fb0e8 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -61,6 +61,7 @@ type Build struct { Output string // Docker build output Pull bool // Docker build pull CacheFrom cli.StringSlice // Docker build cache-from + CacheTo cli.StringSlice // Docker build cache-to Compress bool // Docker build compress Repo cli.StringSlice // Docker build repository NoCache bool // Docker build no-cache @@ -271,12 +272,6 @@ func (p *Plugin) Execute() error { cmds = append(cmds, commandInfo()) // docker info 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. @@ -286,9 +281,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 } }