Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build dependencies when building with BuildKit. #2

Merged
merged 3 commits into from
May 28, 2020
Merged
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
46 changes: 24 additions & 22 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ data class BindMount(val from: String, val source: String, val target: String) {
fun toCopyInstruction() = GenericInstruction("COPY --from=${from} $source $target")
}

fun extractBindMountFlagFromInstruction() {

}
//--mount=type=bind,from=imagemagick,source=/home/builder/packages/x86_64,target=/packages
// Generate a list of image tages for the given image, using the project, version and tag properties.
fun imagesTags(image: String, project: Project): Set<String> {
Expand Down Expand Up @@ -184,27 +181,32 @@ subprojects {
}
}

inline fun <reified T: DefaultTask> getBuildDependencies(childTask: T) = childTask.project.run {
val contents = projectDir.resolve("Dockerfile").readText()
// Extract the image name without the prefix 'local' it should match an existing project.
val matches = extractProjectDependenciesFromDockerfileRegex.findAll(contents)

// If the project is found and it has a build task, it is a dependency.
matches.mapNotNull {
rootProject.findProject(it.groupValues[2])?.tasks?.withType<T>()
}.flatten()
}

// This used to replace the FROM statements such that the referred to the Image ID rather
// than "latest". Though this is currently broken when BuildKit is enabled:
// https://github.com/moby/moby/issues/39769
// Now it uses whatever repository we're building / latest since that is variable.
subprojects {
tasks.withType<DockerBuildImage> {
val contents = projectDir.resolve("Dockerfile").readText()
// Extract the image name without the prefix 'local' it should match an existing project.
val matches = extractProjectDependenciesFromDockerfileRegex.findAll(contents)

// If the project is found and it has a build task, link the dependency.
matches.forEach {
rootProject.findProject(it.groupValues[2])
?.tasks
?.withType<DockerBuildImage>()
?.first()
?.let { buildTask ->
// If generated image id changes, rebuild.
inputs.file(buildTask.imageIdFile.asFile)
dependsOn(buildTask)
// This used to replace the FROM statements such that the referred to the Image ID rather
// than "latest". Though this is currently broken when BuildKit is enabled:
// https://github.com/moby/moby/issues/39769
// Now it uses whatever repository we're building / latest since that is variable.
}
getBuildDependencies(this).forEach { parentTask ->
inputs.file(parentTask.imageIdFile.asFile) // If generated image id changes, rebuild.
dependsOn(parentTask)
}
}
tasks.withType<DockerBuildKitBuildImage> {
getBuildDependencies(this).forEach { parentTask ->
inputs.file(parentTask.imageIdFile.asFile) // If generated image id changes, rebuild.
dependsOn(parentTask)
}
}
}
Expand Down