Skip to content

Commit c9a04de

Browse files
authored
Merge pull request #279 from xdev-software/buildx-native-adapter
Buildx native adapter
2 parents 1fc207c + e5bf846 commit c9a04de

21 files changed

Lines changed: 1192 additions & 596 deletions

File tree

‎.github/workflows/check-build.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,24 @@ jobs:
4848
restore-keys: |
4949
${{ runner.os }}-mvn-build-
5050
51+
# These are required for caching
52+
# https://docs.docker.com/build/cache/backends/gha/#authentication
53+
- name: Get Actions Runtime credentials
54+
id: actions-vars
55+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
56+
with:
57+
script: |
58+
core.setOutput('ACTIONS_RUNTIME_TOKEN', process.env['ACTIONS_RUNTIME_TOKEN'])
59+
core.setOutput('ACTIONS_RESULTS_URL', process.env['ACTIONS_RESULTS_URL'])
60+
61+
- uses: docker/setup-buildx-action@v4
62+
if: matrix.os != 'windows-latest'
63+
5164
- name: Build with Maven
5265
run: ./mvnw -B clean package -P run-integration-tests -T2C
66+
env:
67+
ACTIONS_RUNTIME_TOKEN: ${{ steps.actions-vars.outputs.ACTIONS_RUNTIME_TOKEN }}
68+
ACTIONS_RESULTS_URL: ${{ steps.actions-vars.outputs.ACTIONS_RESULTS_URL }}
5369

5470
- name: Check for uncommited changes
5571
shell: bash

‎CHANGELOG.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# 4.0.0
2+
* Add `NativeAdvancedImageFromDockerfile`
3+
* Builds the image using a new `docker build` process
4+
* Runs isolated of Testcontainers / does not directly utilize Docker API
5+
* Comparison to `AdvancedImageFromDockerFile`
6+
* Advantages
7+
* Full buildx/buildkit support
8+
* Caching can easily be implemented (using --cache-to and --cache-from)
9+
* Disadvantages
10+
* buildx/buildkit is required locally
11+
* Authentication (if required) needs to be done manually
12+
* External process might not be fully controllable e.g. on JVM crash
13+
* No (type-safe) API
14+
* Abstracted common code of `NativeAdvancedImageFromDockerfile` and `AdvancedImageFromDockerfile`
15+
* Minor performance improvements
16+
117
# 3.0.1
218
* Add overload for `copyForIntermediateTag`
319

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>software.xdev</groupId>
88
<artifactId>testcontainers-advanced-imagebuilder-root</artifactId>
9-
<version>3.0.2-SNAPSHOT</version>
9+
<version>4.0.0-SNAPSHOT</version>
1010
<packaging>pom</packaging>
1111

1212
<organization>

‎testcontainers-advanced-imagebuilder-demo/Dockerfile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
ARG JAVA_VERSION=25
44
FROM eclipse-temurin:$JAVA_VERSION-jdk-alpine AS builder
55

6+
ARG CACHE_BUSTER=
7+
RUN echo "$CACHE_BUSTER"
8+
69
RUN apk add --no-cache bash
710

811
WORKDIR /build

‎testcontainers-advanced-imagebuilder-demo/pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<parent>
88
<groupId>software.xdev</groupId>
99
<artifactId>testcontainers-advanced-imagebuilder-root</artifactId>
10-
<version>3.0.2-SNAPSHOT</version>
10+
<version>4.0.0-SNAPSHOT</version>
1111
</parent>
1212

1313
<artifactId>testcontainers-advanced-imagebuilder-demo</artifactId>
14-
<version>3.0.2-SNAPSHOT</version>
14+
<version>4.0.0-SNAPSHOT</version>
1515
<packaging>jar</packaging>
1616

1717
<organization>

‎testcontainers-advanced-imagebuilder-demo/src/main/java/software/xdev/Application.java‎

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
import java.nio.file.Path;
44
import java.nio.file.Paths;
5+
import java.time.Duration;
56
import java.util.List;
6-
import java.util.concurrent.TimeUnit;
7-
import java.util.concurrent.TimeoutException;
87

98
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
109
import org.slf4j.LoggerFactory;
@@ -16,75 +15,79 @@
1615

1716
public final class Application
1817
{
19-
@SuppressWarnings({"java:S106", "PMD.SystemPrintln"}) // Just a demo
18+
@SuppressWarnings({
19+
"java:S106",
20+
"java:S4507",
21+
"PMD.SystemPrintln",
22+
"PMD.AvoidPrintStackTrace"
23+
}) // Just a demo
2024
public static void main(final String[] args)
2125
{
2226
final AdvancedImageFromDockerFile builder = new AdvancedImageFromDockerFile("dynamically-built")
2327
.withLoggerForBuild(LoggerFactory.getLogger("container.build"))
24-
.withPostGitIgnoreLines(
25-
// Ignore files that aren't related to the built code
26-
".git/**",
27-
".config/**",
28-
".github/**",
29-
".idea/**",
30-
".run/**",
31-
".md",
32-
".cmd",
33-
"/renovate.json5",
34-
"testcontainers-advanced-imagebuilder/**",
35-
"testcontainers-advanced-imagebuilder-demo/**"
36-
)
3728
.withDockerFilePath(Paths.get("../testcontainers-advanced-imagebuilder-demo/Dockerfile"))
3829
.withBaseDir(Paths.get("../"))
39-
.withDockerFileLinesModifier(new DockerfileCOPYParentsEmulator())
40-
// Only copy the required maven modules and remove the not required ones
41-
.withTransferArchiveTARCompressorCustomizer(c -> c.withContentModifier(
42-
new FileLinesContentModifier()
43-
{
44-
@Override
45-
public boolean shouldApply(
46-
final Path sourcePath,
47-
final String targetPath,
48-
final TarArchiveEntry tarArchiveEntry)
30+
.configureFilesToTransferHandler(h -> h
31+
.withDockerFileLinesModifier(new DockerfileCOPYParentsEmulator())
32+
.withPostGitIgnoreLines(
33+
// Ignore files that aren't related to the built code
34+
".git/**",
35+
".config/**",
36+
".github/**",
37+
".idea/**",
38+
".run/**",
39+
".md",
40+
".cmd",
41+
"/renovate.json5",
42+
"testcontainers-advanced-imagebuilder/**",
43+
"testcontainers-advanced-imagebuilder-demo/**"
44+
)
45+
// Only copy the required maven modules and remove the not required ones
46+
.withTransferArchiveTARCompressorCustomizer(c -> c.withContentModifier(
47+
new FileLinesContentModifier()
4948
{
50-
return "pom.xml".equals(targetPath);
49+
@Override
50+
public boolean shouldApply(
51+
final Path sourcePath,
52+
final String targetPath,
53+
final TarArchiveEntry tarArchiveEntry)
54+
{
55+
return "pom.xml".equals(targetPath);
56+
}
57+
58+
@Override
59+
public List<String> modify(
60+
final List<String> lines,
61+
final Path sourcePath,
62+
final String targetPath,
63+
final TarArchiveEntry tarArchiveEntry)
64+
{
65+
return lines.stream()
66+
// Only keep the dummy-app submodule as this is only needed for building
67+
.filter(s -> !(s.contains("<module>testcontainers-advanced-imagebuilder")
68+
&& !s.contains("<module>testcontainers-advanced-imagebuilder-dummy-app")))
69+
.toList();
70+
}
71+
72+
@Override
73+
public boolean isIdentical(final List<String> original, final List<String> created)
74+
{
75+
return original.size() == created.size();
76+
}
5177
}
52-
53-
@Override
54-
public List<String> modify(
55-
final List<String> lines,
56-
final Path sourcePath,
57-
final String targetPath,
58-
final TarArchiveEntry tarArchiveEntry)
59-
{
60-
return lines.stream()
61-
// Only keep the dummy-app submodule as this is only needed for building
62-
.filter(s -> !(s.contains("<module>testcontainers-advanced-imagebuilder")
63-
&& !s.contains("<module>testcontainers-advanced-imagebuilder-dummy-app")))
64-
.toList();
65-
}
66-
67-
@Override
68-
public boolean isIdentical(final List<String> original, final List<String> created)
69-
{
70-
return original.size() == created.size();
71-
}
72-
}
73-
));
78+
))
79+
);
7480

75-
final String imageName;
7681
try
7782
{
78-
imageName = builder.get(5, TimeUnit.MINUTES);
83+
final String imageName = builder.build(Duration.ofMinutes(5));
84+
System.out.println("Successfully build " + imageName);
7985
}
80-
catch(final TimeoutException e)
86+
catch(final Exception e)
8187
{
82-
System.out.println("Timed out: " + e.getMessage());
88+
e.printStackTrace();
8389
System.exit(1);
84-
return;
8590
}
86-
87-
System.out.println("Successfully build " + imageName);
8891
}
8992

9093
private Application()

‎testcontainers-advanced-imagebuilder-dummy-app/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>software.xdev</groupId>
88
<artifactId>testcontainers-advanced-imagebuilder-dummy-app</artifactId>
9-
<version>3.0.2-SNAPSHOT</version>
9+
<version>4.0.0-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<organization>

‎testcontainers-advanced-imagebuilder/pom.xml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>software.xdev</groupId>
88
<artifactId>testcontainers-advanced-imagebuilder</artifactId>
9-
<version>3.0.2-SNAPSHOT</version>
9+
<version>4.0.0-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>testcontainers-advanced-imagebuilder</name>
@@ -205,6 +205,10 @@
205205
<version>3.5.6</version>
206206
<configuration>
207207
<skipTests>${skipTests}</skipTests>
208+
<!-- Only required on < Java 26 on Windows NTFS -->
209+
<argLine>
210+
--add-exports java.base/sun.nio.fs=ALL-UNNAMED
211+
</argLine>
208212
</configuration>
209213
</plugin>
210214
</plugins>

0 commit comments

Comments
 (0)