From 08e4222fbb86ff1de1d8ef6954e0779e5e23ae74 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 2 Mar 2021 17:37:49 +0100 Subject: [PATCH] introduce --build-args option Signed-off-by: Nicolas De Loof --- aci/convert/container.go | 2 +- api/compose/api.go | 2 ++ cli/cmd/compose/build.go | 4 ++++ go.mod | 2 +- go.sum | 4 ++-- local/compose/build.go | 1 + local/e2e/compose/compose_build_test.go | 11 +++++++++++ .../fixtures/build-test/nginx-build/Dockerfile | 2 ++ 8 files changed, 24 insertions(+), 4 deletions(-) diff --git a/aci/convert/container.go b/aci/convert/container.go index ab168e9ef..179de8466 100644 --- a/aci/convert/container.go +++ b/aci/convert/container.go @@ -81,7 +81,7 @@ func ContainerToComposeProject(r containers.ContainerConfig) (types.Project, err } func toComposeEnvs(opts []string) types.MappingWithEquals { - result := map[string]*string{} + result := types.MappingWithEquals{} for _, env := range opts { tokens := strings.SplitN(env, "=", 2) if len(tokens) > 1 { diff --git a/api/compose/api.go b/api/compose/api.go index a7156497b..397007abd 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -71,6 +71,8 @@ type BuildOptions struct { Pull bool // Progress set type of progress output ("auto", "plain", "tty") Progress string + // Args set build-time args + Args types.Mapping } // CreateOptions group options of the Create API diff --git a/cli/cmd/compose/build.go b/cli/cmd/compose/build.go index b81c17100..3bb7c3157 100644 --- a/cli/cmd/compose/build.go +++ b/cli/cmd/compose/build.go @@ -20,6 +20,7 @@ import ( "context" "os" + "github.com/compose-spec/compose-go/types" "github.com/spf13/cobra" "github.com/docker/compose-cli/api/client" @@ -33,6 +34,7 @@ type buildOptions struct { quiet bool pull bool progress string + args []string } func buildCommand(p *projectOptions) *cobra.Command { @@ -56,6 +58,7 @@ func buildCommand(p *projectOptions) *cobra.Command { cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT") cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.") cmd.Flags().StringVar(&opts.progress, "progress", "auto", `Set type of progress output ("auto", "plain", "tty")`) + cmd.Flags().StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services.") return cmd } @@ -74,6 +77,7 @@ func runBuild(ctx context.Context, opts buildOptions, services []string) error { return "", c.ComposeService().Build(ctx, project, compose.BuildOptions{ Pull: opts.pull, Progress: opts.progress, + Args: types.NewMapping(opts.args), }) }) return err diff --git a/go.mod b/go.mod index 808e446ea..649e0bec9 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/awslabs/goformation/v4 v4.15.6 github.com/buger/goterm v0.0.0-20200322175922-2f3e71b85129 github.com/cnabio/cnab-to-oci v0.3.1-beta1 - github.com/compose-spec/compose-go v0.0.0-20210218184709-a75bbdcff7f3 + github.com/compose-spec/compose-go v0.0.0-20210302161709-a66de601f6e2 github.com/containerd/console v1.0.1 github.com/containerd/containerd v1.4.3 github.com/containerd/continuity v0.0.0-20200928162600-f2cc35102c2a // indirect diff --git a/go.sum b/go.sum index 4ac8ac243..9bb54c6b3 100644 --- a/go.sum +++ b/go.sum @@ -305,8 +305,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= -github.com/compose-spec/compose-go v0.0.0-20210218184709-a75bbdcff7f3 h1:cGJa3EMDcclDU21e/CVQJnDf3ZjnB6HN9TvxkFHpGq8= -github.com/compose-spec/compose-go v0.0.0-20210218184709-a75bbdcff7f3/go.mod h1:flNthwF3kg+JioxATZWSsuuA2N3zGGwggDNNGoE9PHA= +github.com/compose-spec/compose-go v0.0.0-20210302161709-a66de601f6e2 h1:gQHeYKe3H5IvvLeWsWjOlCxpo0DV6uGdCt4Q3Gr3fTg= +github.com/compose-spec/compose-go v0.0.0-20210302161709-a66de601f6e2/go.mod h1:flNthwF3kg+JioxATZWSsuuA2N3zGGwggDNNGoE9PHA= github.com/containerd/cgroups v0.0.0-20190919134610-bf292b21730f/go.mod h1:OApqhQ4XNSNC13gXIwDjhOQxjWa/NxkwZXJ1EvqT0ko= github.com/containerd/cgroups v0.0.0-20200531161412-0dbf7f05ba59/go.mod h1:pA0z1pT8KYB3TCXK/ocprsh7MAkoW8bZVzPdih9snmM= github.com/containerd/cgroups v0.0.0-20200710171044-318312a37340 h1:9atoWyI9RtXFwf7UDbme/6M8Ud0rFrx+Q3ZWgSnsxtw= diff --git a/local/compose/build.go b/local/compose/build.go index 80e8ae991..cfccebf4f 100644 --- a/local/compose/build.go +++ b/local/compose/build.go @@ -43,6 +43,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti imagesToBuild = append(imagesToBuild, imageName) buildOptions := s.toBuildOptions(service, project.WorkingDir, imageName) buildOptions.Pull = options.Pull + buildOptions.BuildArgs = options.Args opts[imageName] = buildOptions } } diff --git a/local/e2e/compose/compose_build_test.go b/local/e2e/compose/compose_build_test.go index 707f7562c..ac78aebd2 100644 --- a/local/e2e/compose/compose_build_test.go +++ b/local/e2e/compose/compose_build_test.go @@ -43,6 +43,17 @@ func TestLocalComposeBuild(t *testing.T) { c.RunDockerCmd("image", "inspect", "custom-nginx") }) + t.Run("build with build-arg", func(t *testing.T) { + // ensure local test run does not reuse previously build image + c.RunDockerOrExitError("rmi", "build-test_nginx") + c.RunDockerOrExitError("rmi", "custom-nginx") + + c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO=BAR") + + res := c.RunDockerCmd("image", "inspect", "build-test_nginx") + res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`}) + }) + t.Run("build as part of up", func(t *testing.T) { c.RunDockerOrExitError("rmi", "build-test_nginx") c.RunDockerOrExitError("rmi", "custom-nginx") diff --git a/local/e2e/compose/fixtures/build-test/nginx-build/Dockerfile b/local/e2e/compose/fixtures/build-test/nginx-build/Dockerfile index 766ec01e2..87925203e 100644 --- a/local/e2e/compose/fixtures/build-test/nginx-build/Dockerfile +++ b/local/e2e/compose/fixtures/build-test/nginx-build/Dockerfile @@ -14,4 +14,6 @@ FROM nginx +ARG FOO +LABEL FOO=$FOO COPY static /usr/share/nginx/html