Skip to content

Commit dad52ed

Browse files
authored
make airbyteDocker build cache functional (#9362)
1 parent 858e185 commit dad52ed

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

buildSrc/src/main/groovy/airbyte-docker.gradle

+29-3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ class AirbyteDockerPlugin implements Plugin<Project> {
6464
static def getBaseTaggedImages(File dockerfile) {
6565
def result = [] as Set<String>
6666

67+
// Look for "FROM foo AS bar" directives, and add them to the map with .put("bar", "foo")
68+
Map<String, String> imageAliases = [:]
69+
dockerfile.eachLine { line ->
70+
def parts = line.split()
71+
if (parts.length >= 4 && parts[0].equals("FROM") && parts[parts.length - 2].equals("AS")) {
72+
imageAliases.put(parts[parts.length - 1], parts[1])
73+
}
74+
}
75+
6776
dockerfile.eachLine { line ->
6877
if (line.startsWith("FROM ")) {
6978
def image = line.split()[1]
@@ -72,7 +81,11 @@ class AirbyteDockerPlugin implements Plugin<Project> {
7281
} else if (line.startsWith("COPY --from=")) {
7382
def image = line.substring("COPY --from=".length()).split()[0]
7483
assert !image.isEmpty()
75-
result.add(image)
84+
if (imageAliases[image] != null) {
85+
result.add(imageAliases[image])
86+
} else {
87+
result.add(image)
88+
}
7689
}
7790
}
7891

@@ -93,7 +106,20 @@ class AirbyteDockerPlugin implements Plugin<Project> {
93106
def stdout = new ByteArrayOutputStream()
94107

95108
project.exec {
96-
commandLine "docker", "images", "--no-trunc", "-f", "dangling=false", "--format", "{{.ID}}", taggedImage
109+
commandLine "docker", "images", "--no-trunc", "-f", "dangling=false", "--format", "{{.ID}}", resolveEnvironmentVariables(project, taggedImage)
110+
standardOutput = stdout;
111+
}
112+
113+
return "$stdout".toString().trim()
114+
}
115+
116+
// Some image tags rely on environment variables (e.g. "FROM openjdk:${JDK_VERSION}-slim").
117+
// dump those into a "sh -c 'echo ...'" command to resolve them (e.g. "openjdk:17-slim")
118+
static String resolveEnvironmentVariables(Project project, String str) {
119+
def stdout = new ByteArrayOutputStream()
120+
121+
project.exec {
122+
commandLine "sh", "-c", "echo " + str
97123
standardOutput = stdout;
98124
}
99125

@@ -112,7 +138,7 @@ class AirbyteDockerPlugin implements Plugin<Project> {
112138

113139
def notUpToDate = new ArrayList<String>(getBaseTaggedImages(dockerPath.toFile())).any { baseImage ->
114140
logger.debug "checking base image " + baseImage
115-
def storedBase = (String) project.rootProject.imageToHash.get(baseImage)
141+
def storedBase = (String) project.rootProject.imageToHash.get(resolveEnvironmentVariables(project, baseImage))
116142
def currentBase = getImageHash(project, baseImage)
117143

118144
logger.debug "storedBase " + storedBase

0 commit comments

Comments
 (0)