diff --git a/bake/bake.go b/bake/bake.go index 8fbd93cf..945288ff 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -107,16 +107,17 @@ func mergeConfig(c1, c2 Config) Config { func (c Config) setOverrides(v []string) error { for _, v := range v { + parts := strings.SplitN(v, "=", 2) - if len(parts) != 2 { - return errors.Errorf("invalid override %s, expected target.name=value", v) - } keys := strings.SplitN(parts[0], ".", 3) if len(keys) < 2 { return errors.Errorf("invalid override key %s, expected target.name", parts[0]) } name := keys[0] + if len(parts) != 2 && keys[1] != "args" { + return errors.Errorf("invalid override %s, expected target.name=value", v) + } t, ok := c.Target[name] if !ok { @@ -135,7 +136,14 @@ func (c Config) setOverrides(v []string) error { if t.Args == nil { t.Args = map[string]string{} } - t.Args[keys[2]] = parts[1] + if len(parts) < 2 { + v, ok := os.LookupEnv(keys[2]) + if ok { + t.Args[keys[2]] = v + } + } else { + t.Args[keys[2]] = parts[1] + } case "labels": if len(keys) != 3 { return errors.Errorf("invalid key %s, lanels requires name", parts[0]) diff --git a/bake/bake_test.go b/bake/bake_test.go index 21c114f9..c371a1db 100644 --- a/bake/bake_test.go +++ b/bake/bake_test.go @@ -29,11 +29,50 @@ target "webapp" { ctx := context.TODO() - m, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, nil) - require.NoError(t, err) + t.Run("NoOverrides", func(t *testing.T) { + m, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, nil) + require.NoError(t, err) + + require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile) + require.Equal(t, ".", *m["webapp"].Context) + }) + + t.Run("ArgsOverrides", func(t *testing.T) { + os.Setenv("VAR_FROMENV"+t.Name(), "fromEnv") + defer os.Unsetenv("VAR_FROM_ENV" + t.Name()) + + m, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, []string{ + "webapp.args.VAR_UNSET", + "webapp.args.VAR_EMPTY=", + "webapp.args.VAR_SET=bananas", + "webapp.args.VAR_FROMENV" + t.Name(), + }) + require.NoError(t, err) + + require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile) + require.Equal(t, ".", *m["webapp"].Context) + + _, isSet := m["webapp"].Args["VAR_UNSET"] + require.False(t, isSet, m["webapp"].Args["VAR_UNSET"]) + + _, isSet = m["webapp"].Args["VAR_EMPTY"] + require.True(t, isSet, m["webapp"].Args["VAR_EMPTY"]) + + require.Equal(t, m["webapp"].Args["VAR_SET"], "bananas") + + require.Equal(t, m["webapp"].Args["VAR_FROMENV"+t.Name()], "fromEnv") + }) + + t.Run("ContextOverride", func(t *testing.T) { + _, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, []string{"webapp.context"}) + require.NotNil(t, err) + + m, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, []string{"webapp.context=foo"}) + require.NoError(t, err) + + require.Equal(t, "foo", *m["webapp"].Context) + }) - require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile) - require.Equal(t, ".", *m["webapp"].Context) } func TestReadTargetsCompose(t *testing.T) {