Skip to content

Commit

Permalink
Merge pull request #2 from alexandreh2ag/change_docker_cache_control
Browse files Browse the repository at this point in the history
Change config to manage docker build cache control
  • Loading branch information
alexandreh2ag authored Jul 11, 2024
2 parents f67ee6f + d14500c commit 52b056e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ func (g *Config) Get() *Config {

type Build struct {
ExtensionExclude string `mapstructure:"extensionExclude" validate:"required"`
Docker Docker `mapstructure:"docker"`
}

type Docker struct {
CacheToEnable bool `mapstructure:"cacheToEnable"`
CacheFromEnable bool `mapstructure:"cacheFromEnable"`
BuildExtraOpts map[string]string `mapstructure:"buildExtraOpts"`
}

type Template struct {
Expand Down
19 changes: 17 additions & 2 deletions container/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,25 @@ func (b BuilderDocker) BuildImages(images types.Images, pushImages bool) error {
}

func (b BuilderDocker) Build(image *types.Image, pushImages bool) error {
dockerCfg := b.ctx.Config.Build.Docker
b.ctx.Logger.Info(fmt.Sprintf("Start building %s", image.GetFullName()))
logger := b.ctx.Logger.With("image", image.Name)
//cmdArgs := []string{"buildx", "build", "--progress", "plain", "--provenance", "false"}
cmdArgs := []string{"build", "--no-cache", "--progress", "plain", "--provenance", "false"}

cmdArgs := []string{"build", "--progress", "plain"}

if dockerCfg.CacheToEnable {
cmdArgs = append(cmdArgs, "--cache-to", "type=inline,mode=max")
}

if dockerCfg.CacheFromEnable {
cmdArgs = append(cmdArgs, "--cache-from", image.GetFullName())
}

if len(dockerCfg.BuildExtraOpts) > 0 {
for optKey, optValue := range dockerCfg.BuildExtraOpts {
cmdArgs = append(cmdArgs, fmt.Sprintf("--%s", optKey), optValue)
}
}

labels := []string{
fmt.Sprintf("%s=%s", "mib.version", version.GetFormattedVersion()),
Expand Down
35 changes: 31 additions & 4 deletions container/docker/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,34 @@ func TestBuilderDocker_Build_Success(t *testing.T) {
cmd.EXPECT().SetStdout(gomock.Any()).Times(1)
cmd.EXPECT().SetStderr(gomock.Any()).Times(1)
cmd.EXPECT().Run().Times(1).Return(nil)
defaultsArgs := []string{"build", "--no-cache", "--progress", "plain", "--provenance", "false"}
defaultsArgs := []string{"build", "--progress", "plain"}
testArgs := []string{"--tag", "registry.example.com/foo:0.1", "--tag", "registry2.example.com/foo:0.1", "--label", "mib.version=develop-SNAPSHOT", "."}
wantArgs := append(defaultsArgs, testArgs...)
exec.NewCmd = func(name string, arg ...string) exec.Executable {
assert.Equal(t, "docker", name)
assert.Equal(t, wantArgs, arg)
return cmd
}
b := BuilderDocker{ctx: ctx, AuthConfig: &auth}
err := b.Build(image, false)
assert.NoError(t, err)
}

func TestBuilderDocker_Build_SuccessWithBuildOpt(t *testing.T) {
ctx := context.TestContext(nil)
ctx.Config.Build.Docker.CacheToEnable = true
ctx.Config.Build.Docker.CacheFromEnable = true
ctx.Config.Build.Docker.BuildExtraOpts = map[string]string{"provenance": "true"}
auth := AuthConfig{AuthConfigs: map[string]registry.AuthConfig{}}
image := &types.Image{ImageName: types.ImageName{Name: "registry.example.com/foo", Tag: "0.1"}, Path: "/app", Alias: []types.ImageName{{Name: "registry2.example.com/foo", Tag: "0.1"}}}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
cmd := mock_exec.NewMockExecutable(ctrl)
cmd.EXPECT().SetDir(gomock.Eq("/app")).Times(1)
cmd.EXPECT().SetStdout(gomock.Any()).Times(1)
cmd.EXPECT().SetStderr(gomock.Any()).Times(1)
cmd.EXPECT().Run().Times(1).Return(nil)
defaultsArgs := []string{"build", "--progress", "plain", "--cache-to", "type=inline,mode=max", "--cache-from", "registry.example.com/foo:0.1", "--provenance", "true"}
testArgs := []string{"--tag", "registry.example.com/foo:0.1", "--tag", "registry2.example.com/foo:0.1", "--label", "mib.version=develop-SNAPSHOT", "."}
wantArgs := append(defaultsArgs, testArgs...)
exec.NewCmd = func(name string, arg ...string) exec.Executable {
Expand All @@ -218,7 +245,7 @@ func TestBuilderDocker_Build_SuccessWithPush(t *testing.T) {
cmd.EXPECT().SetStdout(gomock.Any()).Times(1)
cmd.EXPECT().SetStderr(gomock.Any()).Times(1)
cmd.EXPECT().Run().Times(1).Return(nil)
defaultsArgs := []string{"build", "--no-cache", "--progress", "plain", "--provenance", "false"}
defaultsArgs := []string{"build", "--progress", "plain"}
testArgs := []string{"--tag", "registry.example.com/foo:0.1", "--tag", "registry2.example.com/foo:0.1", "--label", "mib.version=develop-SNAPSHOT", "--push", "."}
wantArgs := append(defaultsArgs, testArgs...)
exec.NewCmd = func(name string, arg ...string) exec.Executable {
Expand All @@ -242,7 +269,7 @@ func TestBuilderDocker_Build_SuccessMultiPlatforms(t *testing.T) {
cmd.EXPECT().SetStdout(gomock.Any()).Times(1)
cmd.EXPECT().SetStderr(gomock.Any()).Times(1)
cmd.EXPECT().Run().Times(1).Return(nil)
defaultsArgs := []string{"build", "--no-cache", "--progress", "plain", "--provenance", "false"}
defaultsArgs := []string{"build", "--progress", "plain"}
testArgs := []string{"--tag", "registry.example.com/foo:0.1", "--label", "mib.version=develop-SNAPSHOT", "--platform", "linux/amd64,linux/arm64/v8", "."}
wantArgs := append(defaultsArgs, testArgs...)
exec.NewCmd = func(name string, arg ...string) exec.Executable {
Expand All @@ -266,7 +293,7 @@ func TestBuilderDocker_Build_Error(t *testing.T) {
cmd.EXPECT().SetStdout(gomock.Any()).Times(1)
cmd.EXPECT().SetStderr(gomock.Any()).Times(1)
cmd.EXPECT().Run().Times(1).Return(errors.New("fail build"))
defaultsArgs := []string{"build", "--no-cache", "--progress", "plain", "--provenance", "false"}
defaultsArgs := []string{"build", "--progress", "plain"}
testArgs := []string{"--tag", "registry.example.com/foo:0.1", "--label", "mib.version=develop-SNAPSHOT", "."}
wantArgs := append(defaultsArgs, testArgs...)
exec.NewCmd = func(name string, arg ...string) exec.Executable {
Expand Down
5 changes: 5 additions & 0 deletions doc/examples/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
build:
extensionExclude: ".md,.txt"
docker:
cacheToEnable: true
cacheFromEnable: true
buildExtraOpts:
provenance: "true"
template:
imagePath: "my-custom-image.tmpl"
indexPath: "my-custom-index.tmpl"

0 comments on commit 52b056e

Please sign in to comment.