2020-11-30 12:03:13 +01:00
/ *
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package compose
import (
"context"
2021-03-30 15:24:36 +02:00
"fmt"
2021-02-16 12:10:35 +01:00
"os"
2021-11-17 08:21:55 +01:00
"strings"
2020-11-30 12:03:13 +01:00
2021-09-03 10:13:13 +02:00
"github.com/compose-spec/compose-go/cli"
2022-03-30 11:47:34 +02:00
"github.com/compose-spec/compose-go/loader"
2021-03-02 17:37:49 +01:00
"github.com/compose-spec/compose-go/types"
2021-11-17 08:21:55 +01:00
buildx "github.com/docker/buildx/util/progress"
2022-11-24 15:42:28 +01:00
"github.com/docker/compose/v2/pkg/progress"
2021-11-17 08:21:55 +01:00
"github.com/docker/compose/v2/pkg/utils"
2020-11-30 12:03:13 +01:00
"github.com/spf13/cobra"
2021-08-31 18:53:24 +02:00
"github.com/docker/compose/v2/pkg/api"
2020-11-30 12:03:13 +01:00
)
type buildOptions struct {
2022-12-19 16:38:36 +00:00
* ProjectOptions
2020-11-30 12:03:13 +01:00
composeOptions
2021-03-02 09:14:09 +01:00
quiet bool
pull bool
2023-01-06 17:30:55 +01:00
push bool
2021-03-02 09:14:09 +01:00
progress string
2021-03-02 17:37:49 +01:00
args [ ] string
2021-03-26 18:13:39 +01:00
noCache bool
memory string
2022-03-30 11:47:34 +02:00
ssh string
}
func ( opts buildOptions ) toAPIBuildOptions ( services [ ] string ) ( api . BuildOptions , error ) {
var SSHKeys [ ] types . SSHKey
var err error
if opts . ssh != "" {
SSHKeys , err = loader . ParseShortSSHSyntax ( opts . ssh )
if err != nil {
return api . BuildOptions { } , err
}
}
return api . BuildOptions {
Pull : opts . pull ,
2023-01-06 17:30:55 +01:00
Push : opts . push ,
2022-03-30 11:47:34 +02:00
Progress : opts . progress ,
Args : types . NewMappingWithEquals ( opts . args ) ,
NoCache : opts . noCache ,
Quiet : opts . quiet ,
Services : services ,
SSHs : SSHKeys ,
} , nil
2020-11-30 12:03:13 +01:00
}
2021-11-17 08:21:55 +01:00
var printerModes = [ ] string {
buildx . PrinterModeAuto ,
buildx . PrinterModeTty ,
buildx . PrinterModePlain ,
buildx . PrinterModeQuiet ,
}
2022-12-15 09:35:26 +01:00
func buildCommand ( p * ProjectOptions , streams api . Streams , backend api . Service ) * cobra . Command {
2021-01-20 11:02:46 +01:00
opts := buildOptions {
2022-12-19 16:38:36 +00:00
ProjectOptions : p ,
2021-01-20 11:02:46 +01:00
}
2021-02-16 12:10:35 +01:00
cmd := & cobra . Command {
2022-08-02 13:11:57 +02:00
Use : "build [OPTIONS] [SERVICE...]" ,
2020-12-10 15:43:20 +01:00
Short : "Build or rebuild services" ,
2021-05-19 18:49:33 +02:00
PreRunE : Adapt ( func ( ctx context . Context , args [ ] string ) error {
2021-03-30 15:24:36 +02:00
if opts . memory != "" {
2022-12-15 09:35:26 +01:00
fmt . Fprintln ( streams . Err ( ) , "WARNING --memory is ignored as not supported in buildkit." )
2021-03-30 15:24:36 +02:00
}
2021-02-16 12:10:35 +01:00
if opts . quiet {
2021-11-17 08:22:52 +01:00
opts . progress = buildx . PrinterModeQuiet
2021-02-16 12:10:35 +01:00
devnull , err := os . Open ( os . DevNull )
if err != nil {
return err
}
os . Stdout = devnull
}
2021-11-17 08:21:55 +01:00
if ! utils . StringContains ( printerModes , opts . progress ) {
return fmt . Errorf ( "unsupported --progress value %q" , opts . progress )
}
2021-05-19 18:49:33 +02:00
return nil
} ) ,
2022-03-30 11:47:34 +02:00
RunE : AdaptCmd ( func ( ctx context . Context , cmd * cobra . Command , args [ ] string ) error {
if cmd . Flags ( ) . Changed ( "ssh" ) && opts . ssh == "" {
opts . ssh = "default"
}
2022-11-24 15:42:28 +01:00
if progress . Mode == progress . ModePlain && ! cmd . Flags ( ) . Changed ( "progress" ) {
opts . progress = buildx . PrinterModePlain
}
2021-04-15 12:43:18 +02:00
return runBuild ( ctx , backend , opts , args )
} ) ,
2022-09-26 19:21:45 +02:00
ValidArgsFunction : completeServiceNames ( p ) ,
2020-11-30 12:03:13 +01:00
}
2023-01-06 17:30:55 +01:00
cmd . Flags ( ) . BoolVar ( & opts . push , "push" , false , "Push service images." )
2021-02-16 12:10:35 +01:00
cmd . Flags ( ) . BoolVarP ( & opts . quiet , "quiet" , "q" , false , "Don't print anything to STDOUT" )
2021-03-02 09:03:04 +01:00
cmd . Flags ( ) . BoolVar ( & opts . pull , "pull" , false , "Always attempt to pull a newer version of the image." )
2021-11-17 08:21:55 +01:00
cmd . Flags ( ) . StringVar ( & opts . progress , "progress" , buildx . PrinterModeAuto , fmt . Sprintf ( ` Set type of progress output (%s) ` , strings . Join ( printerModes , ", " ) ) )
2021-03-02 17:37:49 +01:00
cmd . Flags ( ) . StringArrayVar ( & opts . args , "build-arg" , [ ] string { } , "Set build-time variables for services." )
2022-03-31 10:12:00 -04:00
cmd . Flags ( ) . StringVar ( & opts . ssh , "ssh" , "" , "Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent)" )
2021-03-19 17:37:08 +01:00
cmd . Flags ( ) . Bool ( "parallel" , true , "Build images in parallel. DEPRECATED" )
cmd . Flags ( ) . MarkHidden ( "parallel" ) //nolint:errcheck
cmd . Flags ( ) . Bool ( "compress" , true , "Compress the build context using gzip. DEPRECATED" )
cmd . Flags ( ) . MarkHidden ( "compress" ) //nolint:errcheck
cmd . Flags ( ) . Bool ( "force-rm" , true , "Always remove intermediate containers. DEPRECATED" )
cmd . Flags ( ) . MarkHidden ( "force-rm" ) //nolint:errcheck
2021-03-26 18:13:39 +01:00
cmd . Flags ( ) . BoolVar ( & opts . noCache , "no-cache" , false , "Do not use cache when building the image" )
cmd . Flags ( ) . Bool ( "no-rm" , false , "Do not remove intermediate containers after a successful build. DEPRECATED" )
cmd . Flags ( ) . MarkHidden ( "no-rm" ) //nolint:errcheck
2021-03-30 15:24:36 +02:00
cmd . Flags ( ) . StringVarP ( & opts . memory , "memory" , "m" , "" , "Set memory limit for the build container. Not supported on buildkit yet." )
2021-03-26 18:13:39 +01:00
cmd . Flags ( ) . MarkHidden ( "memory" ) //nolint:errcheck
2021-03-19 17:37:08 +01:00
2021-02-16 12:10:35 +01:00
return cmd
2020-11-30 12:03:13 +01:00
}
2021-06-14 16:26:14 +02:00
func runBuild ( ctx context . Context , backend api . Service , opts buildOptions , services [ ] string ) error {
2022-12-19 16:38:36 +00:00
project , err := opts . ToProject ( services , cli . WithResolvedPaths ( true ) )
2021-02-02 11:42:19 +01:00
if err != nil {
return err
}
2020-11-30 12:03:13 +01:00
2022-03-30 11:47:34 +02:00
apiBuildOptions , err := opts . toAPIBuildOptions ( services )
if err != nil {
return err
}
return backend . Build ( ctx , project , apiBuildOptions )
2020-11-30 12:03:13 +01:00
}