Skip to content

Commit

Permalink
Add no-cache functionality into Buildx.
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Graham <[email protected]>
  • Loading branch information
tadgh committed Nov 4, 2023
1 parent f228f64 commit 5cc04b1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/io/fabric8/maven/docker/service/BuildXService.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ protected void buildX(List<String> buildX, String builderName, BuildDirs buildDi
cmdLine.add(key + '=' + value);
});
}
if (checkForNocache(imageConfig)) {
cmdLine.add("--no-cache");
}

AttestationConfiguration attestations = buildConfiguration.getBuildX().getAttestations();
if (attestations != null) {
Expand Down Expand Up @@ -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<String> buildX, ImageConfiguration imageConfig, BuildDirs buildDirs) throws MojoExecutionException {
BuildXConfiguration buildXConfiguration = imageConfig.getBuildConfiguration().getBuildX();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<List<String>> buildXArgCaptor = ArgumentCaptor.forClass(List.class);
Mockito.verify(exec).process(buildXArgCaptor.capture());
for (String arg: args) {
assertTrue(buildXArgCaptor.getValue().stream().anyMatch(passedArgument -> passedArgument.equalsIgnoreCase(arg)));
}
}
}

0 comments on commit 5cc04b1

Please sign in to comment.