diff --git a/src/main/java/io/fabric8/maven/docker/service/BuildXService.java b/src/main/java/io/fabric8/maven/docker/service/BuildXService.java index 42b50a1fb..27b81036b 100644 --- a/src/main/java/io/fabric8/maven/docker/service/BuildXService.java +++ b/src/main/java/io/fabric8/maven/docker/service/BuildXService.java @@ -170,6 +170,9 @@ protected void buildX(List buildX, String builderName, BuildDirs buildDi cmdLine.add(key + '=' + value); }); } + if (checkForNocache(imageConfig)) { + cmdLine.add("--no-cache"); + } AttestationConfiguration attestations = buildConfiguration.getBuildX().getAttestations(); if (attestations != null) { @@ -243,6 +246,18 @@ protected void createDirectory(Path cachePath) { throw new IllegalArgumentException("Cannot create " + cachePath); } } + private boolean checkForNocache(ImageConfiguration imageConfig) { + String noCache = System.getProperty("docker.noCache"); + if (noCache == null) { + noCache = System.getProperty("docker.nocache"); + } + if (noCache != null) { + return noCache.length() == 0 || Boolean.valueOf(noCache); + } else { + BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration(); + return buildConfig.noCache(); + } + } protected String createBuilder(Path configPath, List buildX, ImageConfiguration imageConfig, BuildDirs buildDirs) throws MojoExecutionException { BuildXConfiguration buildXConfiguration = imageConfig.getBuildConfiguration().getBuildX(); diff --git a/src/test/java/io/fabric8/maven/docker/service/BuildXServiceTest.java b/src/test/java/io/fabric8/maven/docker/service/BuildXServiceTest.java index be4823e6b..6034c91ef 100644 --- a/src/test/java/io/fabric8/maven/docker/service/BuildXServiceTest.java +++ b/src/test/java/io/fabric8/maven/docker/service/BuildXServiceTest.java @@ -36,6 +36,7 @@ import io.fabric8.maven.docker.config.ImageConfiguration; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; @@ -117,6 +118,34 @@ void testBuildNativePlatformWithForeign() throws Exception { verifyBuildXPlatforms(NATIVE); } + @Test + void testNoCacheIsPropagatedToBuildx(@TempDir File temporaryFolder) throws Exception { + + //Given + buildNoCacheConfigUsingBuildx(temporaryFolder); + + // When + buildx.build(projectPaths, imageConfig, configuredRegistry, authConfigList, buildArchive); + + //Then + verifyBuildXArgumentPresentInExec("--no-cache"); + } + + private void buildNoCacheConfigUsingBuildx(File temporaryFolder) { + BuildXConfiguration buildXConfiguration = new BuildXConfiguration.Builder() + .dockerStateDir(temporaryFolder.getAbsolutePath()) + .platforms(Arrays.asList(NATIVE)) + .build(); + final BuildImageConfiguration buildImageConfig = new BuildImageConfiguration.Builder() + .buildx(buildXConfiguration) + .noCache(true) + .build(); + imageConfig = new ImageConfiguration.Builder() + .name("build-image") + .buildConfig(buildImageConfig) + .build(); + } + @Test void testBuildForeignPlatforms() throws Exception { givenAnImageConfiguration(FOREIGN1, FOREIGN2); @@ -198,4 +227,12 @@ private void verifyBuildXPlatforms(String... platforms) throws Exception { Mockito.verify(buildx).buildX(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.argThat(l -> expect.equals(l)), Mockito.any(), Mockito.any()); } + + private void verifyBuildXArgumentPresentInExec(String... args) throws Exception{ + ArgumentCaptor> buildXArgCaptor = ArgumentCaptor.forClass(List.class); + Mockito.verify(exec).process(buildXArgCaptor.capture()); + for (String arg: args) { + assertTrue(buildXArgCaptor.getValue().stream().anyMatch(passedArgument -> passedArgument.equalsIgnoreCase(arg))); + } + } }