Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit b46015f

Browse files
authored
Merge pull request #1610 from ndeloof/build_args_from_env
2 parents 634ade5 + e433777 commit b46015f

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

api/compose/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type BuildOptions struct {
8383
// Progress set type of progress output ("auto", "plain", "tty")
8484
Progress string
8585
// Args set build-time args
86-
Args types.Mapping
86+
Args types.MappingWithEquals
8787
// NoCache disables cache use
8888
NoCache bool
8989
// Quiet make the build process not output to the console

cli/cmd/compose/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func runBuild(ctx context.Context, backend compose.Service, opts buildOptions, s
8989
return "", backend.Build(ctx, project, compose.BuildOptions{
9090
Pull: opts.pull,
9191
Progress: opts.progress,
92-
Args: types.NewMapping(opts.args),
92+
Args: types.NewMappingWithEquals(opts.args),
9393
NoCache: opts.noCache,
9494
Quiet: opts.quiet,
9595
})

local/compose/build.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
4242
opts := map[string]build.Options{}
4343
imagesToBuild := []string{}
4444

45+
args := map[string]string{}
46+
for k, v := range options.Args.Resolve(func(s string) (string, bool) {
47+
s, ok := project.Environment[s]
48+
return s, ok
49+
}).RemoveEmpty() {
50+
args[k] = *v
51+
}
52+
4553
for _, service := range project.Services {
4654
if service.Build != nil {
4755
imageName := getImageName(service, project.Name)
@@ -51,7 +59,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
5159
return err
5260
}
5361
buildOptions.Pull = options.Pull
54-
buildOptions.BuildArgs = options.Args
62+
buildOptions.BuildArgs = args
5563
buildOptions.NoCache = options.NoCache
5664
opts[imageName] = buildOptions
5765
buildOptions.CacheFrom, err = build.ParseCacheEntry(service.Build.CacheFrom)

local/e2e/compose/compose_build_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ func TestLocalComposeBuild(t *testing.T) {
5454
res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
5555
})
5656

57+
t.Run("build with build-arg set by env", func(t *testing.T) {
58+
// ensure local test run does not reuse previously build image
59+
c.RunDockerOrExitError("rmi", "build-test_nginx")
60+
c.RunDockerOrExitError("rmi", "custom-nginx")
61+
62+
icmd.RunCmd(c.NewDockerCmd("compose", "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO"),
63+
func(cmd *icmd.Cmd) {
64+
cmd.Env = append(cmd.Env, "FOO=BAR")
65+
})
66+
67+
res := c.RunDockerCmd("image", "inspect", "build-test_nginx")
68+
res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
69+
})
70+
5771
t.Run("build as part of up", func(t *testing.T) {
5872
c.RunDockerOrExitError("rmi", "build-test_nginx")
5973
c.RunDockerOrExitError("rmi", "custom-nginx")

0 commit comments

Comments
 (0)