Quellcode durchsuchen

Merge pull request #1380 from docker/build_args

introduce --build-args option
Guillaume Tardif vor 4 Jahren
Ursprung
Commit
91b39d0772

+ 1 - 1
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 {

+ 2 - 0
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

+ 4 - 0
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

+ 1 - 1
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

+ 2 - 2
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=

+ 1 - 0
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
 		}
 	}

+ 11 - 0
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")

+ 2 - 0
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