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"
|
2020-11-30 12:03:13 +01:00
|
|
|
|
2021-03-02 17:37:49 +01:00
|
|
|
"github.com/compose-spec/compose-go/types"
|
2020-11-30 12:03:13 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-03-02 09:14:09 +01:00
|
|
|
"github.com/docker/compose-cli/api/compose"
|
2021-01-15 15:57:24 +01:00
|
|
|
"github.com/docker/compose-cli/api/progress"
|
2020-11-30 12:03:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type buildOptions struct {
|
2021-01-20 11:02:46 +01:00
|
|
|
*projectOptions
|
2020-11-30 12:03:13 +01:00
|
|
|
composeOptions
|
2021-03-02 09:14:09 +01:00
|
|
|
quiet bool
|
|
|
|
pull bool
|
|
|
|
progress string
|
2021-03-02 17:37:49 +01:00
|
|
|
args []string
|
2021-03-26 18:13:39 +01:00
|
|
|
noCache bool
|
|
|
|
memory string
|
2020-11-30 12:03:13 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 11:47:02 +02:00
|
|
|
func buildCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
2021-01-20 11:02:46 +01:00
|
|
|
opts := buildOptions{
|
|
|
|
projectOptions: p,
|
|
|
|
}
|
2021-02-16 12:10:35 +01:00
|
|
|
cmd := &cobra.Command{
|
2020-12-10 15:43:20 +01:00
|
|
|
Use: "build [SERVICE...]",
|
|
|
|
Short: "Build or rebuild services",
|
2021-04-15 12:43:18 +02:00
|
|
|
RunE: Adapt(func(ctx context.Context, args []string) error {
|
2021-03-30 15:24:36 +02:00
|
|
|
if opts.memory != "" {
|
|
|
|
fmt.Println("WARNING --memory is ignored as not supported in buildkit.")
|
|
|
|
}
|
2021-02-16 12:10:35 +01:00
|
|
|
if opts.quiet {
|
|
|
|
devnull, err := os.Open(os.DevNull)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
os.Stdout = devnull
|
|
|
|
}
|
2021-04-15 12:43:18 +02:00
|
|
|
return runBuild(ctx, backend, opts, args)
|
|
|
|
}),
|
2020-11-30 12:03:13 +01:00
|
|
|
}
|
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-03-02 09:14:09 +01:00
|
|
|
cmd.Flags().StringVar(&opts.progress, "progress", "auto", `Set type of progress output ("auto", "plain", "tty")`)
|
2021-03-02 17:37:49 +01:00
|
|
|
cmd.Flags().StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services.")
|
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-04-14 11:47:02 +02:00
|
|
|
func runBuild(ctx context.Context, backend compose.Service, opts buildOptions, services []string) error {
|
2021-02-02 11:42:19 +01:00
|
|
|
project, err := opts.toProject(services)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-30 12:03:13 +01:00
|
|
|
|
2021-02-02 11:42:19 +01:00
|
|
|
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
2021-04-14 11:47:02 +02:00
|
|
|
return "", backend.Build(ctx, project, compose.BuildOptions{
|
2021-03-02 09:14:09 +01:00
|
|
|
Pull: opts.pull,
|
|
|
|
Progress: opts.progress,
|
2021-04-28 18:17:37 +02:00
|
|
|
Args: types.NewMappingWithEquals(opts.args),
|
2021-03-26 18:13:39 +01:00
|
|
|
NoCache: opts.noCache,
|
2021-04-13 13:53:07 +02:00
|
|
|
Quiet: opts.quiet,
|
2021-03-02 09:03:04 +01:00
|
|
|
})
|
2020-11-30 12:03:13 +01:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|