From b438032a60e7595dc0fef904ec19ff807afb4e8a Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 23 Jun 2022 21:44:06 +0200 Subject: [PATCH 1/3] bake: support compose build cache_to Signed-off-by: CrazyMax --- bake/compose.go | 1 + bake/compose_test.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/bake/compose.go b/bake/compose.go index 3ce1eb8e..801b2255 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -100,6 +100,7 @@ func ParseCompose(dt []byte) (*Config, error) { return val, ok })), CacheFrom: s.Build.CacheFrom, + CacheTo: s.Build.CacheTo, NetworkMode: &s.Build.Network, Secrets: secrets, } diff --git a/bake/compose_test.go b/bake/compose_test.go index 2ef51a05..c4e3dd1d 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -23,6 +23,10 @@ services: none args: buildno: 123 + cache_from: + - type=local,src=path/to/cache + cache_to: + - type=local,dest=path/to/cache secrets: - token - aws @@ -54,6 +58,8 @@ secrets: require.Equal(t, "Dockerfile-alternate", *c.Targets[1].Dockerfile) require.Equal(t, 1, len(c.Targets[1].Args)) require.Equal(t, "123", c.Targets[1].Args["buildno"]) + require.Equal(t, c.Targets[1].CacheFrom, []string{"type=local,src=path/to/cache"}) + require.Equal(t, c.Targets[1].CacheTo, []string{"type=local,dest=path/to/cache"}) require.Equal(t, "none", *c.Targets[1].NetworkMode) require.Equal(t, []string{ "id=token,env=ENV_TOKEN", @@ -253,6 +259,8 @@ services: dockerfile: ./Dockerfile cache_from: - user/app:cache + cache_to: + - user/app:cache tags: - ct-addon:baz args: @@ -267,7 +275,8 @@ services: - linux/arm64 cache-from: - type=local,src=path/to/cache - cache-to: type=local,dest=path/to/cache + cache-to: + - type=local,dest=path/to/cache pull: true aws: @@ -297,7 +306,7 @@ services: require.Equal(t, c.Targets[0].Tags, []string{"ct-addon:baz", "ct-addon:foo", "ct-addon:alp"}) require.Equal(t, c.Targets[0].Platforms, []string{"linux/amd64", "linux/arm64"}) require.Equal(t, c.Targets[0].CacheFrom, []string{"type=local,src=path/to/cache"}) - require.Equal(t, c.Targets[0].CacheTo, []string{"type=local,dest=path/to/cache"}) + require.Equal(t, c.Targets[0].CacheTo, []string{"user/app:cache", "type=local,dest=path/to/cache"}) require.Equal(t, c.Targets[0].Pull, newBool(true)) require.Equal(t, c.Targets[1].Tags, []string{"ct-fake-aws:bar"}) require.Equal(t, c.Targets[1].Secrets, []string{"id=mysecret,src=/local/secret", "id=mysecret2,src=/local/secret2"}) From a0f92829a7cb21335ccb44d2dc604200a9923a5a Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 23 Jun 2022 21:47:05 +0200 Subject: [PATCH 2/3] bake: merge cache-from field from compose and x-bake Signed-off-by: CrazyMax --- bake/compose.go | 2 +- bake/compose_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bake/compose.go b/bake/compose.go index 801b2255..44bfe5dc 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -190,7 +190,7 @@ func (t *Target) composeExtTarget(exts map[string]interface{}) error { t.Tags = append(t.Tags, xb.Tags...) } if len(xb.CacheFrom) > 0 { - t.CacheFrom = xb.CacheFrom // override main field + t.CacheFrom = append(t.CacheFrom, xb.CacheFrom...) } if len(xb.CacheTo) > 0 { t.CacheTo = append(t.CacheTo, xb.CacheTo...) diff --git a/bake/compose_test.go b/bake/compose_test.go index c4e3dd1d..7ab3ad9f 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -305,7 +305,7 @@ services: require.Equal(t, c.Targets[0].Args, map[string]string{"CT_ECR": "foo", "CT_TAG": "bar"}) require.Equal(t, c.Targets[0].Tags, []string{"ct-addon:baz", "ct-addon:foo", "ct-addon:alp"}) require.Equal(t, c.Targets[0].Platforms, []string{"linux/amd64", "linux/arm64"}) - require.Equal(t, c.Targets[0].CacheFrom, []string{"type=local,src=path/to/cache"}) + require.Equal(t, c.Targets[0].CacheFrom, []string{"user/app:cache", "type=local,src=path/to/cache"}) require.Equal(t, c.Targets[0].CacheTo, []string{"user/app:cache", "type=local,dest=path/to/cache"}) require.Equal(t, c.Targets[0].Pull, newBool(true)) require.Equal(t, c.Targets[1].Tags, []string{"ct-fake-aws:bar"}) From 12fde33d9bd46887b080360ae4d9e5a8105997ca Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 23 Jun 2022 21:57:51 +0200 Subject: [PATCH 3/3] bake: dedup compose main and extension fields values Signed-off-by: CrazyMax --- bake/compose.go | 16 ++++++++-------- bake/compose_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/bake/compose.go b/bake/compose.go index 44bfe5dc..37e7782e 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -187,25 +187,25 @@ func (t *Target) composeExtTarget(exts map[string]interface{}) error { } if len(xb.Tags) > 0 { - t.Tags = append(t.Tags, xb.Tags...) + t.Tags = dedupString(append(t.Tags, xb.Tags...)) } if len(xb.CacheFrom) > 0 { - t.CacheFrom = append(t.CacheFrom, xb.CacheFrom...) + t.CacheFrom = dedupString(append(t.CacheFrom, xb.CacheFrom...)) } if len(xb.CacheTo) > 0 { - t.CacheTo = append(t.CacheTo, xb.CacheTo...) + t.CacheTo = dedupString(append(t.CacheTo, xb.CacheTo...)) } if len(xb.Secrets) > 0 { - t.Secrets = append(t.Secrets, xb.Secrets...) + t.Secrets = dedupString(append(t.Secrets, xb.Secrets...)) } if len(xb.SSH) > 0 { - t.SSH = append(t.SSH, xb.SSH...) + t.SSH = dedupString(append(t.SSH, xb.SSH...)) } if len(xb.Platforms) > 0 { - t.Platforms = append(t.Platforms, xb.Platforms...) + t.Platforms = dedupString(append(t.Platforms, xb.Platforms...)) } if len(xb.Outputs) > 0 { - t.Outputs = append(t.Outputs, xb.Outputs...) + t.Outputs = dedupString(append(t.Outputs, xb.Outputs...)) } if xb.Pull != nil { t.Pull = xb.Pull @@ -214,7 +214,7 @@ func (t *Target) composeExtTarget(exts map[string]interface{}) error { t.NoCache = xb.NoCache } if len(xb.NoCacheFilter) > 0 { - t.NoCacheFilter = append(t.NoCacheFilter, xb.NoCacheFilter...) + t.NoCacheFilter = dedupString(append(t.NoCacheFilter, xb.NoCacheFilter...)) } return nil diff --git a/bake/compose_test.go b/bake/compose_test.go index 7ab3ad9f..a3af03b9 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -316,6 +316,37 @@ services: require.Equal(t, c.Targets[1].NoCache, newBool(true)) } +func TestComposeExtDedup(t *testing.T) { + var dt = []byte(` +services: + webapp: + image: app:bar + build: + cache_from: + - user/app:cache + cache_to: + - user/app:cache + tags: + - ct-addon:foo + x-bake: + tags: + - ct-addon:foo + - ct-addon:baz + cache-from: + - user/app:cache + - type=local,src=path/to/cache + cache-to: + - type=local,dest=path/to/cache +`) + + c, err := ParseCompose(dt) + require.NoError(t, err) + require.Equal(t, 1, len(c.Targets)) + require.Equal(t, c.Targets[0].Tags, []string{"ct-addon:foo", "ct-addon:baz"}) + require.Equal(t, c.Targets[0].CacheFrom, []string{"user/app:cache", "type=local,src=path/to/cache"}) + require.Equal(t, c.Targets[0].CacheTo, []string{"user/app:cache", "type=local,dest=path/to/cache"}) +} + func TestEnv(t *testing.T) { envf, err := os.CreateTemp("", "env") require.NoError(t, err)