From 5d715ada964c8579b37d041e77eb6b38478fe5da Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 6 Jan 2022 11:59:41 +0100 Subject: [PATCH] compose: resolve build args from service environment Signed-off-by: CrazyMax --- bake/compose.go | 3 +++ bake/compose_test.go | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/bake/compose.go b/bake/compose.go index 2ea88850..2872c72d 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -76,6 +76,9 @@ func ParseCompose(dt []byte) (*Config, error) { Dockerfile: dockerfilePathP, Labels: s.Build.Labels, Args: flatten(s.Build.Args.Resolve(func(val string) (string, bool) { + if val, ok := s.Environment[val]; ok && val != nil { + return *val, true + } val, ok := cfg.Environment[val] return val, ok })), diff --git a/bake/compose_test.go b/bake/compose_test.go index 01991b81..a5389007 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -280,7 +280,7 @@ services: require.Equal(t, c.Targets[1].NoCache, newBool(true)) } -func TestEnvFile(t *testing.T) { +func TestEnv(t *testing.T) { envf, err := os.CreateTemp("", "env") require.NoError(t, err) defer os.Remove(envf.Name()) @@ -293,12 +293,21 @@ services: scratch: build: context: . + args: + CT_ECR: foo + FOO: + NODE_ENV: + environment: + - NODE_ENV=test + - AWS_ACCESS_KEY_ID=dummy + - AWS_SECRET_ACCESS_KEY=dummy env_file: - ` + envf.Name() + ` `) - _, err = ParseCompose(dt) + c, err := ParseCompose(dt) require.NoError(t, err) + require.Equal(t, c.Targets[0].Args, map[string]string{"CT_ECR": "foo", "FOO": "bsdf -csdf", "NODE_ENV": "test"}) } func newBool(val bool) *bool {