Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit d264446

Browse files
authored
feat: add cache_to option and remove manual image pull for cache_from images (#124)
1 parent 9af3f0c commit d264446

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

_docs/data/data.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ properties:
143143
type: list
144144
required: false
145145

146+
cache_to:
147+
description: [Cache destination](https://docs.docker.com/engine/reference/commandline/buildx_build/#cache-to) for the build cache.
148+
type: string
149+
required: false
150+
146151
pull_image:
147152
description: Enforce to pull the base image at build time.
148153
defaultValue: true

cmd/drone-docker-buildx/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
201201
Destination: &settings.Build.CacheFrom,
202202
Category: category,
203203
},
204+
&cli.StringFlag{
205+
Name: "cache-to",
206+
EnvVars: []string{"PLUGIN_CACHE_TO"},
207+
Usage: "cache destination for the build cache",
208+
Destination: &settings.Build.CacheTo,
209+
Category: category,
210+
},
204211
&cli.BoolFlag{
205212
Name: "pull-image",
206213
EnvVars: []string{"PLUGIN_PULL_IMAGE"},

plugin/docker.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ func commandLogin(login Login) *exec.Cmd {
2323
)
2424
}
2525

26-
// helper to check if args match "docker pull <image>"
27-
func isCommandPull(args []string) bool {
28-
return len(args) > 2 && args[1] == "pull"
29-
}
30-
31-
func commandPull(repo string) *exec.Cmd {
32-
return exec.Command(dockerExe, "pull", repo)
33-
}
34-
3526
func commandLoginEmail(login Login) *exec.Cmd {
3627
return exec.Command(
3728
dockerExe, "login",
@@ -99,6 +90,9 @@ func commandBuild(build Build, dryrun bool) *exec.Cmd {
9990
for _, arg := range build.CacheFrom.Value() {
10091
args = append(args, "--cache-from", arg)
10192
}
93+
if build.CacheTo != "" {
94+
args = append(args, "--cache-to", build.CacheTo)
95+
}
10296
for _, arg := range build.ArgsEnv.Value() {
10397
addProxyValue(&build, arg)
10498
}

plugin/impl.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type Build struct {
5353
Target string // Docker build target
5454
Pull bool // Docker build pull
5555
CacheFrom cli.StringSlice // Docker build cache-from
56+
CacheTo string // Docker build cache-to
5657
Compress bool // Docker build compress
5758
Repo string // Docker build repository
5859
NoCache bool // Docker build no-cache
@@ -166,11 +167,6 @@ func (p *Plugin) Execute() error {
166167
cmds = append(cmds, commandBuilder(p.settings.Daemon))
167168
cmds = append(cmds, commandBuildx())
168169

169-
// pre-pull cache images
170-
for _, img := range p.settings.Build.CacheFrom.Value() {
171-
cmds = append(cmds, commandPull(img))
172-
}
173-
174170
cmds = append(cmds, commandBuild(p.settings.Build, p.settings.Dryrun)) // docker build
175171

176172
// execute all commands in batch mode.
@@ -180,9 +176,7 @@ func (p *Plugin) Execute() error {
180176
trace(cmd)
181177

182178
err := cmd.Run()
183-
if err != nil && isCommandPull(cmd.Args) {
184-
fmt.Printf("Could not pull cache-from image %s. Ignoring...\n", cmd.Args[2])
185-
} else if err != nil {
179+
if err != nil {
186180
return err
187181
}
188182
}

0 commit comments

Comments
 (0)