Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.34.7
0.35.0
20 changes: 18 additions & 2 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func Command() *cli.Command {
Value: DefaultCacheTag,
Sources: cli.EnvVars("3LV_CACHE_TAG"),
},
&cli.BoolFlag{
Name: "disable-cache",
Usage: "Disable the use of cache when building the image." +
" Cache produced by the build will still be pushed to the registry if --push is enabled.",
Value: false,
Sources: cli.EnvVars("3LV_DISABLE_CACHE"),
},
&cli.StringFlag{
Name: "azure-tenant-id",
Usage: "The tenant ID to use when authenticating with the Azure Container Registry.",
Expand Down Expand Up @@ -243,6 +250,8 @@ func Build(_ context.Context, c *cli.Command) error {
return cli.Exit(err, 1)
}

disableCache := c.Bool("disable-cache")

additionalTags := utils.RemoveZeroValues(c.StringSlice("additional-tags"))
buildArgs := lo.SliceToMap(utils.RemoveZeroValues(c.StringSlice("build-args")), func(f string) (string, string) {
split := strings.Split(f, "=")
Expand All @@ -255,6 +264,7 @@ func Build(_ context.Context, c *cli.Command) error {
buildContext,
imageName,
cacheTag,
disableCache,
additionalTags,
buildArgs,
nil,
Expand Down Expand Up @@ -356,6 +366,7 @@ func buildImageCommand(
buildContext string,
imageName string,
cacheTag string,
disableCache bool,
additionalTags []string,
buildArgs map[string]string,
options *command.RunOptions,
Expand Down Expand Up @@ -383,10 +394,15 @@ func buildImageCommand(
"--load",
"--cache-to",
"type=inline",
"--cache-from",
imageName+":"+cacheTag,
)

if !disableCache {
buildCmd.Args = append(buildCmd.Args,
"--cache-from",
imageName+":"+cacheTag,
)
}

for key, value := range buildArgs {
buildCmd.Args = append(buildCmd.Args, "--build-arg", key+"="+value)
}
Expand Down
63 changes: 63 additions & 0 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func TestBuildCommand1(t *testing.T) {
buildContext = "src/app"
imageName = "containerregistryelvia.azurecr.io/test-image"
cacheTag = "latest"
disableCache = false
)

imageNameWithCacheTag := imageName + ":" + cacheTag
Expand Down Expand Up @@ -108,6 +109,7 @@ func TestBuildCommand1(t *testing.T) {
buildContext,
imageName,
cacheTag,
disableCache,
additionalTags,
buildArgs,
&command.RunOptions{DryRun: true},
Expand All @@ -127,6 +129,7 @@ func TestBuildCommand2(t *testing.T) {
dockerfilePath = "Dockerfile"
buildContext = "."
imageName = "ghcr.io/test-image"
disableCache = false
)

imageNameWithCacheTag := imageName + ":" + DefaultCacheTag
Expand Down Expand Up @@ -157,6 +160,7 @@ func TestBuildCommand2(t *testing.T) {
buildContext,
imageName,
DefaultCacheTag,
disableCache,
additionalTags,
buildArgs,
&command.RunOptions{DryRun: true},
Expand All @@ -176,6 +180,7 @@ func TestBuildCommand3(t *testing.T) {
dockerfilePath = "Dockerfile"
buildContext = "."
imageName = "ghcr.io/test-image"
disableCache = false
)

imageNameWithCacheTag := imageName + ":" + DefaultCacheTag
Expand Down Expand Up @@ -212,6 +217,7 @@ func TestBuildCommand3(t *testing.T) {
buildContext,
imageName,
DefaultCacheTag,
disableCache,
additionalTags,
buildArgs,
&command.RunOptions{DryRun: true},
Expand All @@ -231,6 +237,7 @@ func TestBuildCommandWithBuildArgs(t *testing.T) {
dockerfilePath = "Dockerfile"
buildContext = "."
imageName = "ghcr.io/test-image"
disableCache = false
)

imageNameWithCacheTag := imageName + ":" + DefaultCacheTag
Expand Down Expand Up @@ -274,6 +281,62 @@ func TestBuildCommandWithBuildArgs(t *testing.T) {
buildContext,
imageName,
DefaultCacheTag,
disableCache,
additionalTags,
buildArgs,
&command.RunOptions{DryRun: true},
)

command.ExpectedCommandStringEqualsActualCommand(
t,
expectedCommandString,
actualCommand,
)
}

func TestBuildCommandWithDisableCache(t *testing.T) {
t.Parallel()

const (
dockerfilePath = "Dockerfile"
buildContext = "."
imageName = "ghcr.io/test-image"
disableCache = true
)

imageNameWithCacheTag := imageName + ":" + DefaultCacheTag
additionalTags := []string{"latest", "v42.0.1", "v420alpha"}
buildArgs := map[string]string{}

expectedCommandString := strings.Join(
[]string{
"docker",
"buildx",
"build",
"-f",
dockerfilePath,
"--load",
"--cache-to",
"type=inline",
"-t",
imageName + ":" + additionalTags[0],
"-t",
imageName + ":" + additionalTags[1],
"-t",
imageName + ":" + additionalTags[2],
"-t",
imageNameWithCacheTag,
buildContext,
},
" ",
)

actualCommand := buildImageCommand(
dockerfilePath,
buildContext,
imageName,
DefaultCacheTag,
disableCache,
additionalTags,
buildArgs,
&command.RunOptions{DryRun: true},
Expand Down
Loading