Skip to content

Commit

Permalink
Support for "--plain" option to show plain build container output (#765)
Browse files Browse the repository at this point in the history
* Support for "--plain" option to show plain build container output

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>

* Review comments

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>
  • Loading branch information
pchico83 committed Mar 17, 2020
1 parent 4f3aad1 commit ff393d2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
5 changes: 4 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Build() *cobra.Command {
var tag string
var target string
var noCache bool
var progress string
var buildArgs []string

cmd := &cobra.Command{
Expand All @@ -45,10 +46,11 @@ func Build() *cobra.Command {
log.Information("Your image won't be pushed. To push your image specify the flag '-t'.")
}

if _, err := build.Run(buildKitHost, isOktetoCluster, args[0], file, tag, target, noCache, buildArgs); err != nil {
if _, err := build.Run(buildKitHost, isOktetoCluster, args[0], file, tag, target, noCache, buildArgs, progress); err != nil {
analytics.TrackBuild(false)
return err
}
log.Success("Build succeeded")
analytics.TrackBuild(true)
return nil
},
Expand All @@ -64,6 +66,7 @@ func Build() *cobra.Command {
cmd.Flags().StringVarP(&tag, "tag", "t", "", "name and optionally a tag in the 'name:tag' format (it is automatically pushed)")
cmd.Flags().StringVarP(&target, "target", "", "", "set the target build stage to build")
cmd.Flags().BoolVarP(&noCache, "no-cache", "", false, "do not use cache when building the image")
cmd.Flags().StringVarP(&progress, "progress", "", "tty", "show plain/tty build output")
cmd.Flags().StringArrayVar(&buildArgs, "build-arg", nil, "set build-time variables")
return cmd
}
8 changes: 5 additions & 3 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Push() *cobra.Command {
var namespace string
var imageTag string
var autoDeploy bool
var progress string
var deploymentName string

cmd := &cobra.Command{
Expand Down Expand Up @@ -80,7 +81,7 @@ func Push() *cobra.Command {
}
}

if err := runPush(dev, autoDeploy, imageTag, oktetoRegistryURL, c); err != nil {
if err := runPush(dev, autoDeploy, imageTag, oktetoRegistryURL, progress, c); err != nil {
analytics.TrackPush(false, oktetoRegistryURL)
return err
}
Expand All @@ -98,11 +99,12 @@ func Push() *cobra.Command {
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "namespace where the push command is executed")
cmd.Flags().StringVarP(&imageTag, "tag", "t", "", "image tag to build, push and redeploy")
cmd.Flags().BoolVarP(&autoDeploy, "deploy", "d", false, "create deployment when it doesn't exist in a namespace")
cmd.Flags().StringVarP(&progress, "progress", "", "tty", "show plain/tty build output")
cmd.Flags().StringVar(&deploymentName, "name", "", "name of the deployment to push to")
return cmd
}

func runPush(dev *model.Dev, autoDeploy bool, imageTag, oktetoRegistryURL string, c *kubernetes.Clientset) error {
func runPush(dev *model.Dev, autoDeploy bool, imageTag, oktetoRegistryURL, progress string, c *kubernetes.Clientset) error {
create := false
d, err := deployments.Get(dev, dev.Namespace, c)
if err != nil {
Expand Down Expand Up @@ -132,7 +134,7 @@ func runPush(dev *model.Dev, autoDeploy bool, imageTag, oktetoRegistryURL string
log.Infof("pushing with image tag %s", imageTag)

var imageDigest string
imageDigest, err = build.Run(buildKitHost, isOktetoCluster, ".", "Dockerfile", imageTag, "", false, nil)
imageDigest, err = build.Run(buildKitHost, isOktetoCluster, ".", "Dockerfile", imageTag, "", false, nil, progress)
if err != nil {
return fmt.Errorf("error building image '%s': %s", imageTag, err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// Run runs the build sequence
func Run(buildKitHost string, isOktetoCluster bool, path, dockerFile, tag, target string, noCache bool, buildArgs []string) (string, error) {
func Run(buildKitHost string, isOktetoCluster bool, path, dockerFile, tag, target string, noCache bool, buildArgs []string, progress string) (string, error) {
ctx := context.Background()

buildkitClient, err := getBuildkitClient(ctx, isOktetoCluster, buildKitHost)
Expand All @@ -43,5 +43,5 @@ func Run(buildKitHost string, isOktetoCluster bool, path, dockerFile, tag, targe
return "", errors.Wrap(err, "failed to create build solver")
}

return solveBuild(ctx, buildkitClient, opt)
return solveBuild(ctx, buildkitClient, opt, progress)
}
8 changes: 5 additions & 3 deletions pkg/cmd/build/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func getClientForOktetoCluster(ctx context.Context, buildKitHost string) (*clien
return c, nil
}

func solveBuild(ctx context.Context, c *client.Client, opt *client.SolveOpt) (string, error) {
func solveBuild(ctx context.Context, c *client.Client, opt *client.SolveOpt, progress string) (string, error) {
var solveResp *client.SolveResponse
ch := make(chan *client.SolveStatus)
eg, ctx := errgroup.WithContext(ctx)
Expand All @@ -209,8 +209,10 @@ func solveBuild(ctx context.Context, c *client.Client, opt *client.SolveOpt) (st

eg.Go(func() error {
var c console.Console
if cn, err := console.ConsoleFromFile(os.Stderr); err == nil {
c = cn
if progress == "tty" {
if cn, err := console.ConsoleFromFile(os.Stderr); err == nil {
c = cn
}
}
// not using shared context to not disrupt display but let it finish reporting errors
return progressui.DisplaySolveStatus(context.TODO(), "", c, os.Stdout, ch)
Expand Down

0 comments on commit ff393d2

Please sign in to comment.