From 5d06406f265d207f37a48c3d6a114a9c3472a7dc Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Wed, 10 May 2023 09:39:00 +0100 Subject: [PATCH 1/2] bake: support compose inline dockerfile compose-go v1.13.0 supports the new dockerfile_inline to allow including a dockerfile inline in the compose definition, so we should map this to bake's dockerfile-inline property. Signed-off-by: Justin Chadwell --- bake/compose.go | 16 +++++++++++----- bake/compose_test.go | 13 +++++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/bake/compose.go b/bake/compose.go index 697fe653..fb5f031b 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -69,6 +69,11 @@ func ParseCompose(cfgs []compose.ConfigFile, envs map[string]string) (*Config, e dockerfilePath := s.Build.Dockerfile dockerfilePathP = &dockerfilePath } + var dockerfileInlineP *string + if s.Build.DockerfileInline != "" { + dockerfileInline := s.Build.DockerfileInline + dockerfileInlineP = &dockerfileInline + } var secrets []string for _, bs := range s.Build.Secrets { @@ -88,11 +93,12 @@ func ParseCompose(cfgs []compose.ConfigFile, envs map[string]string) (*Config, e g.Targets = append(g.Targets, targetName) t := &Target{ - Name: targetName, - Context: contextPathP, - Dockerfile: dockerfilePathP, - Tags: s.Build.Tags, - Labels: labels, + Name: targetName, + Context: contextPathP, + Dockerfile: dockerfilePathP, + DockerfileInline: dockerfileInlineP, + Tags: s.Build.Tags, + Labels: labels, Args: flatten(s.Build.Args.Resolve(func(val string) (string, bool) { if val, ok := s.Environment[val]; ok && val != nil { return *val, true diff --git a/bake/compose_test.go b/bake/compose_test.go index 537065a5..7dda4b21 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -33,6 +33,11 @@ services: secrets: - token - aws + webapp2: + build: + context: ./dir + dockerfile_inline: | + FROM alpine secrets: token: environment: ENV_TOKEN @@ -46,9 +51,9 @@ secrets: require.Equal(t, 1, len(c.Groups)) require.Equal(t, "default", c.Groups[0].Name) sort.Strings(c.Groups[0].Targets) - require.Equal(t, []string{"db", "webapp"}, c.Groups[0].Targets) + require.Equal(t, []string{"db", "webapp", "webapp2"}, c.Groups[0].Targets) - require.Equal(t, 2, len(c.Targets)) + require.Equal(t, 3, len(c.Targets)) sort.Slice(c.Targets, func(i, j int) bool { return c.Targets[i].Name < c.Targets[j].Name }) @@ -68,6 +73,10 @@ secrets: "id=token,env=ENV_TOKEN", "id=aws,src=/root/.aws/credentials", }, c.Targets[1].Secrets) + + require.Equal(t, "webapp2", c.Targets[2].Name) + require.Equal(t, "./dir", *c.Targets[2].Context) + require.Equal(t, "FROM alpine\n", *c.Targets[2].DockerfileInline) } func TestNoBuildOutOfTreeService(t *testing.T) { From e228c398f4f6b177ff66eaf01094f9d84ae6b147 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Wed, 10 May 2023 09:41:37 +0100 Subject: [PATCH 2/2] bake: support compose additional contexts compose-go v1.13.0 supports the new additional_contexts to allow passing additional build context during build, so we should map this to bake's contexts property. Signed-off-by: Justin Chadwell --- bake/compose.go | 9 +++++++++ bake/compose_test.go | 3 +++ 2 files changed, 12 insertions(+) diff --git a/bake/compose.go b/bake/compose.go index fb5f031b..457fe2c1 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -75,6 +75,14 @@ func ParseCompose(cfgs []compose.ConfigFile, envs map[string]string) (*Config, e dockerfileInlineP = &dockerfileInline } + var additionalContexts map[string]string + if s.Build.AdditionalContexts != nil { + additionalContexts = map[string]string{} + for k, v := range s.Build.AdditionalContexts { + additionalContexts[k] = v + } + } + var secrets []string for _, bs := range s.Build.Secrets { secret, err := composeToBuildkitSecret(bs, cfg.Secrets[bs.Source]) @@ -95,6 +103,7 @@ func ParseCompose(cfgs []compose.ConfigFile, envs map[string]string) (*Config, e t := &Target{ Name: targetName, Context: contextPathP, + Contexts: additionalContexts, Dockerfile: dockerfilePathP, DockerfileInline: dockerfileInlineP, Tags: s.Build.Tags, diff --git a/bake/compose_test.go b/bake/compose_test.go index 7dda4b21..5074248b 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -21,6 +21,8 @@ services: webapp: build: context: ./dir + additional_contexts: + foo: /bar dockerfile: Dockerfile-alternate network: none @@ -63,6 +65,7 @@ secrets: require.Equal(t, "webapp", c.Targets[1].Name) require.Equal(t, "./dir", *c.Targets[1].Context) + require.Equal(t, map[string]string{"foo": "/bar"}, c.Targets[1].Contexts) require.Equal(t, "Dockerfile-alternate", *c.Targets[1].Dockerfile) require.Equal(t, 1, len(c.Targets[1].Args)) require.Equal(t, ptrstr("123"), c.Targets[1].Args["buildno"])