rationalize pull build

Signed-off-by: Avi Deitcher <avi@deitcher.net>
This commit is contained in:
Avi Deitcher 2022-11-01 12:51:20 +02:00
parent b17c93a635
commit 9ea2d6dcd9
2 changed files with 28 additions and 2 deletions

View File

@ -38,6 +38,7 @@ func pkgBuildPush(args []string, withPush bool) {
} }
force := flags.Bool("force", false, "Force rebuild even if image is in local cache") force := flags.Bool("force", false, "Force rebuild even if image is in local cache")
pull := flags.Bool("pull", false, "Pull image if in registry but not in local cache; conflicts with --force")
ignoreCache := flags.Bool("ignore-cached", false, "Ignore cached intermediate images, always pulling from registry") ignoreCache := flags.Bool("ignore-cached", false, "Ignore cached intermediate images, always pulling from registry")
docker := flags.Bool("docker", false, "Store the built image in the docker image cache instead of the default linuxkit cache") docker := flags.Bool("docker", false, "Store the built image in the docker image cache instead of the default linuxkit cache")
platforms := flags.String("platforms", "", "Which platforms to build for, defaults to all of those for which the package can be built") platforms := flags.String("platforms", "", "Which platforms to build for, defaults to all of those for which the package can be built")
@ -49,12 +50,15 @@ func pkgBuildPush(args []string, withPush bool) {
flags.Var(&cacheDir, "cache", fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir)) flags.Var(&cacheDir, "cache", fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir))
// some logic clarification: // some logic clarification:
// pkg build - always builds unless is in cache // pkg build - builds unless is in cache or published in registry
// pkg build --force - always builds even if is in cache // pkg build --pull - builds unless is in cache or published in registry; pulls from registry if not in cache
// pkg build --force - always builds even if is in cache or published in registry
// pkg build --force --pull - always builds even if is in cache or published in registry; --pull ignored
// pkg push - always builds unless is in cache // pkg push - always builds unless is in cache
// pkg push --force - always builds even if is in cache // pkg push --force - always builds even if is in cache
// pkg push --nobuild - skips build; if not in cache, fails // pkg push --nobuild - skips build; if not in cache, fails
// pkg push --nobuild --force - nonsensical // pkg push --nobuild --force - nonsensical
// pkg pull - pull from registry if available, otherwise fail
var ( var (
release *string release *string
@ -78,6 +82,10 @@ func pkgBuildPush(args []string, withPush bool) {
fmt.Fprint(os.Stderr, "flags -force and -nobuild conflict") fmt.Fprint(os.Stderr, "flags -force and -nobuild conflict")
os.Exit(1) os.Exit(1)
} }
if *pull && *force {
fmt.Fprint(os.Stderr, "flags -force and -pull conflict")
os.Exit(1)
}
var opts []pkglib.BuildOpt var opts []pkglib.BuildOpt
if *force { if *force {
@ -86,6 +94,9 @@ func pkgBuildPush(args []string, withPush bool) {
if *ignoreCache { if *ignoreCache {
opts = append(opts, pkglib.WithBuildIgnoreCache()) opts = append(opts, pkglib.WithBuildIgnoreCache())
} }
if *pull {
opts = append(opts, pkglib.WithBuildPull())
}
opts = append(opts, pkglib.WithBuildCacheDir(cacheDir.String())) opts = append(opts, pkglib.WithBuildCacheDir(cacheDir.String()))

View File

@ -27,6 +27,7 @@ import (
type buildOpts struct { type buildOpts struct {
skipBuild bool skipBuild bool
force bool force bool
pull bool
ignoreCache bool ignoreCache bool
push bool push bool
release string release string
@ -61,6 +62,14 @@ func WithBuildForce() BuildOpt {
} }
} }
// WithBuildPull pull down the image to cache if it already exists in registry
func WithBuildPull() BuildOpt {
return func(bo *buildOpts) error {
bo.pull = true
return nil
}
}
// WithBuildPush pushes the result of the build to the registry // WithBuildPush pushes the result of the build to the registry
func WithBuildPush() BuildOpt { func WithBuildPush() BuildOpt {
return func(bo *buildOpts) error { return func(bo *buildOpts) error {
@ -225,6 +234,12 @@ func (p Pkg) Build(bos ...BuildOpt) error {
} }
var platformsToBuild []imagespec.Platform var platformsToBuild []imagespec.Platform
switch {
case bo.force:
case bo.pull:
case !bo.skipBuild:
}
if bo.force { if bo.force {
platformsToBuild = bo.platforms platformsToBuild = bo.platforms
} else if !bo.skipBuild { } else if !bo.skipBuild {