Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --push and --load support to build cmd #367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func newBuildCommand() *cobra.Command {
fs.Var(build.labels, "label", "Set metadata for an image")
fs.BoolVar(&build.noConsole, "no-console", false, "Use non-console progress UI")
fs.BoolVar(&build.noCache, "no-cache", false, "Do not use cache when building the image")
fs.BoolVar(&build.push, "push", false, "Shorthand for \"--output=type=registry\"")
fs.BoolVar(&build.load, "load", false, "Shorthand for \"--output=type=docker\"") // no-op flag
fs.StringVarP(&build.output, "output", "o", "", "BuildKit output specification (e.g. type=tar,dest=build.tar)")
fs.Var(build.cacheFrom, "cache-from", "Buildkit import-cache or Buildx cache-from specification")
fs.Var(build.cacheTo, "cache-to", "Buildx cache-to specification")
Expand All @@ -93,6 +95,8 @@ type buildCommand struct {
contextDir string
noConsole bool
noCache bool
push bool
load bool // no-op
}

// validateTag checks if the given image name can be resolved, and ensures the latest tag is added if it is missing.
Expand All @@ -113,6 +117,11 @@ func (cmd *buildCommand) ValidateArgs(c *cobra.Command, args []string) error {
return fmt.Errorf("must pass a path to build")
}

if cmd.push {
c.Flag("output").Changed = true
cmd.output = fmt.Sprintf("type=image,push=true,name=%s", cmd.tags.values[0])
}

if c.Flag("output").Changed {
out, err := build.ParseOutput([]string{cmd.output})
if err != nil || len(out) != 1 {
Expand All @@ -125,6 +134,11 @@ func (cmd *buildCommand) ValidateArgs(c *cobra.Command, args []string) error {
}
out[0].Attrs["name"] = validated
}
if out[0].Type == "registry" {
out[0].Type = "image"
out[0].Attrs["push"] = "true"
cmd.push = true
}
cmd.bkoutput = out[0]
} else if cmd.tags.Len() < 1 {
return errors.New("please specify an image tag with `-t` or an output spec with `-o`")
Expand Down Expand Up @@ -256,6 +270,9 @@ func (cmd *buildCommand) Run(args []string) (err error) {
},
}
}
if cmd.push {
out.Attrs["name"] = strings.Join(cmd.tags.GetAll(), ",")
}

ch := make(chan *controlapi.StatusResponse)
eg.Go(func() error {
Expand Down