@@ -39,6 +39,7 @@ type buildOptions struct {
3939 folder string
4040 imageIDFile string
4141 args []string
42+ quiet bool
4243}
4344
4445func Cmd (dockerCli command.Cli ) * cobra.Command {
@@ -60,6 +61,7 @@ func Cmd(dockerCli command.Cli) *cobra.Command {
6061 flags .StringVarP (& opts .folder , "folder" , "f" , "" , "Docker app folder containing application definition" )
6162 flags .BoolVar (& opts .pull , "pull" , false , "Always attempt to pull a newer version of the image" )
6263 flags .StringArrayVar (& opts .args , "build-arg" , []string {}, "Set build-time variables" )
64+ flags .BoolVarP (& opts .quiet , "quiet" , "q" , false , "Suppress the build output and print app image ID on success" )
6365 flags .StringVar (& opts .imageIDFile , "iidfile" , "" , "Write the app image ID to the file" )
6466
6567 return cmd
@@ -110,14 +112,18 @@ func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) error
110112 }
111113
112114 if opt .imageIDFile != "" {
113- if err = ioutil .WriteFile (opt .imageIDFile , []byte (id .String ()), 0644 ); err != nil {
115+ if err = ioutil .WriteFile (opt .imageIDFile , []byte (id .Digest (). String ()), 0644 ); err != nil {
114116 fmt .Fprintf (dockerCli .Err (), "Failed to write application image id in %s: %s" , opt .imageIDFile , err )
115117 }
116118 }
117119
118- fmt .Printf ("Successfully built %s\n " , id )
120+ if opt .quiet {
121+ fmt .Fprintln (dockerCli .Out (), id .Digest ().String ())
122+ return err
123+ }
124+ fmt .Fprintf (dockerCli .Out (), "Successfully built %s\n " , id .String ())
119125 if ref != nil {
120- fmt .Printf ( "Successfully tagged %s\n " , ref .String ())
126+ fmt .Fprintf ( dockerCli . Out (), "Successfully tagged %s\n " , ref .String ())
121127 }
122128 return err
123129}
@@ -145,13 +151,23 @@ func buildImageUsingBuildx(app *types.App, contextPath string, opt buildOptions,
145151 Driver : d ,
146152 },
147153 }
148- pw := progress .NewPrinter (ctx , os .Stderr , opt .progress )
154+
155+ var out * os.File
156+ if opt .quiet {
157+ if out , err = os .Create (os .DevNull ); err != nil {
158+ return nil , err
159+ }
160+ } else {
161+ out = os .NewFile (dockerCli .Out ().FD (), "/dev/stdout" )
162+ }
163+
164+ pw := progress .NewPrinter (ctx , out , opt .progress )
165+
149166 // We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
150167 resp , err := build .Build (ctx , driverInfo , buildopts , nil , dockerCli .ConfigFile (), pw )
151168 if err != nil {
152169 return nil , err
153170 }
154- fmt .Fprintln (dockerCli .Out (), "Successfully built service images" ) //nolint:errcheck
155171
156172 bundle , err := packager .MakeBundleFromApp (dockerCli , app , nil )
157173 if err != nil {
0 commit comments