Merge pull request #1 from shimunn/cache_from

Cache from: Changes requested by @bradrydzewski
This commit is contained in:
Ryan Sullivan 2018-11-07 16:17:53 -05:00 committed by GitHub
commit 3e9b0bb48b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -151,6 +151,11 @@ func main() {
Usage: "build target", Usage: "build target",
EnvVar: "PLUGIN_TARGET", EnvVar: "PLUGIN_TARGET",
}, },
cli.StringSliceFlag{
Name: "cache-from",
Usage: "images to consider as cache sources",
EnvVar: "PLUGIN_CACHE_FROM",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "squash", Name: "squash",
Usage: "squash the layers at build time", Usage: "squash the layers at build time",
@ -245,6 +250,7 @@ func run(c *cli.Context) error {
Target: c.String("target"), Target: c.String("target"),
Squash: c.Bool("squash"), Squash: c.Bool("squash"),
Pull: c.BoolT("pull-image"), Pull: c.BoolT("pull-image"),
CacheFrom: c.StringSlice("cache-from"),
Compress: c.Bool("compress"), Compress: c.Bool("compress"),
Repo: c.String("repo"), Repo: c.String("repo"),
Labels: c.StringSlice("custom-labels"), Labels: c.StringSlice("custom-labels"),

View File

@ -47,6 +47,7 @@ type (
Target string // Docker build target Target string // Docker build target
Squash bool // Docker build squash Squash bool // Docker build squash
Pull bool // Docker build pull Pull bool // Docker build pull
CacheFrom []string // Docker build cache-from
Compress bool // Docker build compress Compress bool // Docker build compress
Repo string // Docker build repository Repo string // Docker build repository
LabelSchema []string // label-schema Label map LabelSchema []string // label-schema Label map
@ -116,6 +117,11 @@ func (p Plugin) Exec() error {
cmds = append(cmds, commandVersion()) // docker version cmds = append(cmds, commandVersion()) // docker version
cmds = append(cmds, commandInfo()) // docker info cmds = append(cmds, commandInfo()) // docker info
// pre-pull cache images
for _, img := range p.Build.CacheFrom {
cmds = append(cmds, commandPull(img))
}
cmds = append(cmds, commandBuild(p.Build)) // docker build cmds = append(cmds, commandBuild(p.Build)) // docker build
for _, tag := range p.Build.Tags { for _, tag := range p.Build.Tags {
@ -138,7 +144,9 @@ func (p Plugin) Exec() error {
trace(cmd) trace(cmd)
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil && isCommandPull(cmd.Args) {
fmt.Printf("Could not pull cache-from image %s. Ignoring...\n", cmd.Args[2])
} else if err != nil {
return err return err
} }
} }
@ -162,6 +170,15 @@ func commandLogin(login Login) *exec.Cmd {
) )
} }
// helper to check if args match "docker pull <image>"
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 { func commandLoginEmail(login Login) *exec.Cmd {
return exec.Command( return exec.Command(
dockerExe, "login", dockerExe, "login",
@ -204,6 +221,9 @@ func commandBuild(build Build) *exec.Cmd {
if build.NoCache { if build.NoCache {
args = append(args, "--no-cache") args = append(args, "--no-cache")
} }
for _, arg := range build.CacheFrom {
args = append(args, "--cache-from", arg)
}
for _, arg := range build.ArgsEnv { for _, arg := range build.ArgsEnv {
addProxyValue(&build, arg) addProxyValue(&build, arg)
} }